View Javadoc
1   package org.apache.maven.plugin.assembly.format;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 /*        expect( configSource.getFilters()).andReturn( filters );
110 
111         expect( configSource.isIncludeProjectBuildFilters()).andReturn( includeProjectBuildFilters );
112 
113         expect( configSource.getDelimiters()).andReturn( delimiters );
114 */
115         return cfg;
116     }
117 
118     private PlexusIoVirtualFileResource dummyResource()
119     {
120         return new PlexusIoVirtualFileResource( new File( "fud" ), "fud" )
121         {
122         };
123     }
124 }