1 package org.apache.maven.plugin.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.plugin.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.apache.maven.plugin.assembly.format.ReaderFormatter.getFileSetTransformers;
38 import static org.junit.Assert.assertEquals;
39
40
41 @SuppressWarnings( "ConstantConditions" )
42 public class ReaderFormatterTest
43 {
44 @Test
45 public void lineDosFeed()
46 throws IOException, AssemblyFormattingException
47 {
48 final PojoConfigSource cfg = getPojoConfigSource();
49 InputStreamTransformer fileSetTransformers = getFileSetTransformers( cfg, true, "dos" );
50 InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
51 assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
52 }
53
54 @Test
55 public void lineDosFeed_withoutFiltering()
56 throws IOException, AssemblyFormattingException
57 {
58 final PojoConfigSource cfg = getPojoConfigSource();
59 InputStreamTransformer fileSetTransformers = getFileSetTransformers( cfg, false, "dos" );
60 InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
61 assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
62 }
63
64 @Test
65 public void lineUnixFeedWithInterpolation()
66 throws IOException, AssemblyFormattingException
67 {
68 final PojoConfigSource cfg = getPojoConfigSource();
69 InputStreamTransformer fileSetTransformers = getFileSetTransformers( cfg, true, "unix" );
70 InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a test for project: ${artifactId} @artifactId@.") );
71 assertEquals( "This is a test for project: anArtifact anArtifact.", readResultStream( fud ) );
72 }
73
74
75 private MavenProject createBasicMavenProject()
76 {
77 final Model model = new Model();
78 model.setArtifactId( "anArtifact" );
79 model.setGroupId( "group" );
80 model.setVersion( "version" );
81
82 return new MavenProject( model );
83 }
84
85
86 private String readResultStream( InputStream fud )
87 throws IOException
88 {
89 byte[] actual = new byte[100];
90 int read = IOUtils.read( fud, actual );
91 return new String( actual, 0, read);
92 }
93
94 private ByteArrayInputStream payload( String payload )
95 {
96 return new ByteArrayInputStream( payload.getBytes() );
97 }
98
99 private PojoConfigSource getPojoConfigSource()
100 {
101 final PojoConfigSource cfg = new PojoConfigSource();
102 cfg.setEncoding( "UTF-8" );
103 DefaultMavenReaderFilter mavenReaderFilter = new DefaultMavenReaderFilter();
104 mavenReaderFilter.enableLogging( new ConsoleLogger( 2, "fud" ) );
105 cfg.setMavenReaderFilter( mavenReaderFilter );
106 cfg.setEscapeString( null );
107 cfg.setMavenProject( createBasicMavenProject() );
108
109
110
111
112
113
114
115 return cfg;
116 }
117
118 private PlexusIoVirtualFileResource dummyResource()
119 {
120 return new PlexusIoVirtualFileResource( new File( "fud" ), "fud" )
121 {
122 };
123 }
124 }