1 package org.apache.maven.plugins.assembly.format;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.IOUtils;
23 import org.apache.maven.model.Model;
24 import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
25 import org.apache.maven.project.MavenProject;
26 import org.apache.maven.shared.filtering.DefaultMavenReaderFilter;
27 import org.codehaus.plexus.archiver.resources.PlexusIoVirtualFileResource;
28 import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
29 import org.codehaus.plexus.logging.console.ConsoleLogger;
30 import org.junit.Test;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStream;
36
37 import static org.junit.Assert.assertEquals;
38
39
40 public class ReaderFormatterTest
41 {
42 @Test
43 public void lineDosFeed()
44 throws IOException, AssemblyFormattingException
45 {
46 final PojoConfigSource cfg = getPojoConfigSource();
47 InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, "dos" );
48 InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
49 assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
50 }
51
52 @Test
53 public void lineDosFeed_withoutFiltering()
54 throws IOException, AssemblyFormattingException
55 {
56 final PojoConfigSource cfg = getPojoConfigSource();
57 InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, false, "dos" );
58 InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
59 assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
60 }
61
62 @Test
63 public void lineUnixFeedWithInterpolation()
64 throws IOException, AssemblyFormattingException
65 {
66 final PojoConfigSource cfg = getPojoConfigSource();
67 InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, "unix" );
68 InputStream fud = fileSetTransformers.transform( dummyResource(), payload(
69 "This is a test for project: ${artifactId} @artifactId@." ) );
70 assertEquals( "This is a test for project: anArtifact anArtifact.", readResultStream( fud ) );
71 }
72
73
74 private MavenProject createBasicMavenProject()
75 {
76 final Model model = new Model();
77 model.setArtifactId( "anArtifact" );
78 model.setGroupId( "group" );
79 model.setVersion( "version" );
80
81 return new MavenProject( model );
82 }
83
84
85 private String readResultStream( InputStream fud )
86 throws IOException
87 {
88 byte[] actual = new byte[100];
89 int read = IOUtils.read( fud, actual );
90 return new String( actual, 0, read );
91 }
92
93 private ByteArrayInputStream payload( String payload )
94 {
95 return new ByteArrayInputStream( payload.getBytes() );
96 }
97
98 private PojoConfigSource getPojoConfigSource()
99 {
100 final PojoConfigSource cfg = new PojoConfigSource();
101 cfg.setEncoding( "UTF-8" );
102 DefaultMavenReaderFilter mavenReaderFilter = new DefaultMavenReaderFilter();
103 mavenReaderFilter.enableLogging( new ConsoleLogger( 2, "fud" ) );
104 cfg.setMavenReaderFilter( mavenReaderFilter );
105 cfg.setEscapeString( null );
106 cfg.setMavenProject( createBasicMavenProject() );
107
108
109
110
111
112
113
114 return cfg;
115 }
116
117 private PlexusIoVirtualFileResource dummyResource()
118 {
119 return new PlexusIoVirtualFileResource( new File( "fud" ), "fud" )
120 {
121 };
122 }
123 }