View Javadoc
1   package org.apache.maven.plugin.war;
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 java.io.BufferedReader;
23  import java.io.File;
24  import java.io.StringReader;
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  import org.apache.maven.plugin.war.stub.MavenProjectBasicStub;
29  import org.apache.maven.plugin.war.stub.ResourceStub;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * @author Olivier Lamy
34   * @since 21 juil. 2008
35   * @version $Id: WarExplodedMojoFilteringTest.html 935522 2015-01-08 20:15:45Z khmarbaise $
36   */
37  public class WarExplodedMojoFilteringTest
38      extends AbstractWarExplodedMojoTest
39  {
40  
41      protected File getPomFile()
42      {
43          return new File( getBasedir(), "/target/test-classes/unit/warexplodedmojo/plugin-config.xml" );
44      }
45  
46      protected File getTestDirectory()
47      {
48          return new File( getBasedir(), "target/test-classes/unit/warexplodedmojo/test-dir" );
49      }
50  
51      /**
52       * @throws Exception
53       */
54      public void testExplodedWar_WithResourceFiltering()
55          throws Exception
56      {
57          // setup test data
58          String testId = "ExplodedWar_WithResourceFiltering";
59          MavenProjectBasicStub project = new MavenProjectBasicStub();
60          File webAppDirectory = new File( getTestDirectory(), testId );
61          File webAppSource = createWebAppSource( testId );
62          File classesDir = createClassesDir( testId, false );
63          File webAppResource = new File( getTestDirectory(), testId + "-test-data/resources" );
64          File sampleResource = new File( webAppResource, "custom-setting.cfg" );
65          File sampleResourceWDir = new File( webAppResource, "custom-config/custom-setting.cfg" );
66          List<String> filterList = new LinkedList<String>();
67          ResourceStub[] resources = new ResourceStub[] { new ResourceStub() };
68  
69          createFile( sampleResource );
70          createFile( sampleResourceWDir );
71  
72          String ls = System.getProperty( "line.separator" );
73          final String comment = "# this is comment created by author@somewhere@";
74          // prepare web resources
75          String content = comment + ls;
76          content += "system_key_1=${user.dir}" + ls;
77          content += "system_key_2=@user.dir@" + ls;
78          content += "project_key_1=${is_this_simple}" + ls;
79          content += "project_key_2=@is_this_simple@" + ls;
80          content += "project_name_1=${project.name}" + ls;
81          content += "project_name_2=@project.name@" + ls;
82          content += "system_property_1=${system.property}" + ls;
83          content += "system_property_2=@system.property@" + ls;
84          FileUtils.fileWrite( sampleResourceWDir.getAbsolutePath(), content );
85          FileUtils.fileWrite( sampleResource.getAbsolutePath(), content );
86  
87          System.setProperty( "system.property", "system-property-value" );
88  
89          // configure mojo
90          project.addProperty( "is_this_simple", "i_think_so" );
91          resources[0].setDirectory( webAppResource.getAbsolutePath() );
92          resources[0].setFiltering( true );
93          this.configureMojo( mojo, filterList, classesDir, webAppSource, webAppDirectory, project );
94          setVariableValueToObject( mojo, "webResources", resources );
95          mojo.execute();
96  
97          // validate operation
98          File expectedWebSourceFile = new File( webAppDirectory, "pansit.jsp" );
99          File expectedWebSource2File = new File( webAppDirectory, "org/web/app/last-exile.jsp" );
100         File expectedResourceFile = new File( webAppDirectory, "custom-setting.cfg" );
101         File expectedResourceWDirFile = new File( webAppDirectory, "custom-config/custom-setting.cfg" );
102 
103         assertTrue( "source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists() );
104         assertTrue( "source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists() );
105         assertTrue( "resource file not found:" + expectedResourceFile.toString(), expectedResourceFile.exists() );
106         assertTrue( "resource file with dir not found:" + expectedResourceWDirFile.toString(),
107                     expectedResourceWDirFile.exists() );
108 
109         // validate filtered file
110         content = FileUtils.fileRead( expectedResourceWDirFile );
111         BufferedReader reader = new BufferedReader( new StringReader( content ) );
112 
113         assertEquals( "error in filtering using System Properties", comment, reader.readLine() );
114 
115         String line = reader.readLine();
116         System.out.println( " line " + line );
117         System.out.println( " need " + System.getProperty( "user.dir" ) );
118         assertEquals( "error in filtering using System properties", "system_key_1=" + System.getProperty( "user.dir" ),
119                       line );
120         line = reader.readLine();
121         System.out.println( " line " + line );
122         assertEquals( "error in filtering using System properties", "system_key_2=" + System.getProperty( "user.dir" ),
123                       line );
124 
125         assertEquals( "error in filtering using project properties", "project_key_1=i_think_so", reader.readLine() );
126         assertEquals( "error in filtering using project properties", "project_key_2=i_think_so", reader.readLine() );
127 
128         assertEquals( "error in filtering using project properties", "project_name_1=Test Project ", reader.readLine() );
129         assertEquals( "error in filtering using project properties", "project_name_2=Test Project ", reader.readLine() );
130 
131         assertEquals( "error in filtering using System properties", "system_property_1=system-property-value",
132                       reader.readLine() );
133         assertEquals( "error in filtering using System properties", "system_property_2=system-property-value",
134                       reader.readLine() );
135 
136         // update property, and generate again
137         System.setProperty( "system.property", "new-system-property-value" );
138 
139         mojo.execute();
140 
141         // validate filtered file
142         content = FileUtils.fileRead( expectedResourceWDirFile );
143         reader = new BufferedReader( new StringReader( content ) );
144 
145         assertEquals( "error in filtering using System Properties", comment, reader.readLine() );
146 
147         assertEquals( "error in filtering using System properties", "system_key_1=" + System.getProperty( "user.dir" ),
148                       reader.readLine() );
149         assertEquals( "error in filtering using System properties", "system_key_2=" + System.getProperty( "user.dir" ),
150                       reader.readLine() );
151 
152         assertEquals( "error in filtering using project properties", "project_key_1=i_think_so", reader.readLine() );
153         assertEquals( "error in filtering using project properties", "project_key_2=i_think_so", reader.readLine() );
154 
155         assertEquals( "error in filtering using project properties", "project_name_1=Test Project ", reader.readLine() );
156         assertEquals( "error in filtering using project properties", "project_name_2=Test Project ", reader.readLine() );
157 
158         assertEquals( "error in filtering using System properties", "system_property_1=new-system-property-value",
159                       reader.readLine() );
160         assertEquals( "error in filtering using System properties", "system_property_2=new-system-property-value",
161                       reader.readLine() );
162 
163         // update property, and generate again
164         File filterFile = new File( getTestDirectory(), testId + "-test-data/filters/filter.properties" );
165         createFile( filterFile );
166         filterList.add( filterFile.getAbsolutePath() );
167         content += "resource_key_1=${resource_value_1}\n";
168         content += "resource_key_2=@resource_value_2@\n" + content;
169         FileUtils.fileWrite( sampleResourceWDir.getAbsolutePath(), content );
170         FileUtils.fileWrite( sampleResource.getAbsolutePath(), content );
171         String filterContent = "resource_value_1=this_is_filtered\n";
172         filterContent += "resource_value_2=this_is_filtered";
173         FileUtils.fileWrite( filterFile.getAbsolutePath(), filterContent );
174 
175         mojo.execute();
176 
177         // validate filtered file
178         content = FileUtils.fileRead( expectedResourceWDirFile );
179         reader = new BufferedReader( new StringReader( content ) );
180 
181         assertEquals( "error in filtering using System Properties", comment, reader.readLine() );
182 
183         assertEquals( "error in filtering using System properties", "system_key_1=" + System.getProperty( "user.dir" ),
184                       reader.readLine() );
185         assertEquals( "error in filtering using System properties", "system_key_2=" + System.getProperty( "user.dir" ),
186                       reader.readLine() );
187 
188         assertEquals( "error in filtering using project properties", "project_key_1=i_think_so", reader.readLine() );
189         assertEquals( "error in filtering using project properties", "project_key_2=i_think_so", reader.readLine() );
190 
191         assertEquals( "error in filtering using project properties", "project_name_1=Test Project ", reader.readLine() );
192         assertEquals( "error in filtering using project properties", "project_name_2=Test Project ", reader.readLine() );
193 
194         assertEquals( "error in filtering using System properties", "system_property_1=new-system-property-value",
195                       reader.readLine() );
196         assertEquals( "error in filtering using System properties", "system_property_2=new-system-property-value",
197                       reader.readLine() );
198 
199         assertEquals( "error in filtering using filter files", "resource_key_1=this_is_filtered", reader.readLine() );
200         assertEquals( "error in filtering using filter files", "resource_key_2=this_is_filtered", reader.readLine() );
201 
202         // house keeping
203         expectedWebSourceFile.delete();
204         expectedWebSource2File.delete();
205         expectedResourceFile.delete();
206         expectedResourceWDirFile.delete();
207     }
208 
209 }