View Javadoc
1   package org.apache.maven.plugins.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.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 /*        expect( configSource.getFilters()).andReturn( filters );
109 
110         expect( configSource.isIncludeProjectBuildFilters()).andReturn( includeProjectBuildFilters );
111 
112         expect( configSource.getDelimiters()).andReturn( delimiters );
113 */
114         return cfg;
115     }
116 
117     private PlexusIoVirtualFileResource dummyResource()
118     {
119         return new PlexusIoVirtualFileResource( new File( "fud" ), "fud" )
120         {
121         };
122     }
123 }