View Javadoc
1   package org.apache.maven.plugins.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.plugins.war.stub.MavenProjectBasicStub;
29  import org.apache.maven.plugins.war.stub.ResourceStub;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * @author Olivier Lamy
34   * @since 21 juil. 2008
35   */
36  public class WarExplodedMojoFilteringTest
37      extends AbstractWarExplodedMojoTest
38  {
39  
40      protected File getPomFile()
41      {
42          return new File( getBasedir(), "/target/test-classes/unit/warexplodedmojo/plugin-config.xml" );
43      }
44  
45      protected File getTestDirectory()
46      {
47          return new File( getBasedir(), "target/test-classes/unit/warexplodedmojo/test-dir" );
48      }
49  
50      /**
51       * @throws Exception in case of an error.
52       */
53      public void testExplodedWar_WithResourceFiltering()
54          throws Exception
55      {
56          // setup test data
57          String testId = "ExplodedWar_WithResourceFiltering";
58          MavenProjectBasicStub project = new MavenProjectBasicStub();
59          File webAppDirectory = new File( getTestDirectory(), testId );
60          File webAppSource = createWebAppSource( testId );
61          File classesDir = createClassesDir( testId, false );
62          File webAppResource = new File( getTestDirectory(), testId + "-test-data/resources" );
63          File sampleResource = new File( webAppResource, "custom-setting.cfg" );
64          File sampleResourceWDir = new File( webAppResource, "custom-config/custom-setting.cfg" );
65          List<String> filterList = new LinkedList<>();
66          ResourceStub[] resources = new ResourceStub[] { new ResourceStub() };
67  
68          createFile( sampleResource );
69          createFile( sampleResourceWDir );
70  
71          String ls = System.getProperty( "line.separator" );
72          final String comment = "# this is comment created by author@somewhere@";
73          // prepare web resources
74          String content = comment + ls;
75          content += "system_key_1=${user.dir}" + ls;
76          content += "system_key_2=@user.dir@" + ls;
77          content += "project_key_1=${is_this_simple}" + ls;
78          content += "project_key_2=@is_this_simple@" + ls;
79          content += "project_name_1=${project.name}" + ls;
80          content += "project_name_2=@project.name@" + ls;
81          content += "system_property_1=${system.property}" + ls;
82          content += "system_property_2=@system.property@" + ls;
83          FileUtils.fileWrite( sampleResourceWDir.getAbsolutePath(), content );
84          FileUtils.fileWrite( sampleResource.getAbsolutePath(), content );
85  
86          System.setProperty( "system.property", "system-property-value" );
87  
88          // configure mojo
89          project.addProperty( "is_this_simple", "i_think_so" );
90          resources[0].setDirectory( webAppResource.getAbsolutePath() );
91          resources[0].setFiltering( true );
92          this.configureMojo( mojo, filterList, classesDir, webAppSource, webAppDirectory, project );
93          setVariableValueToObject( mojo, "webResources", resources );
94          mojo.execute();
95  
96          // validate operation
97          File expectedWebSourceFile = new File( webAppDirectory, "pansit.jsp" );
98          File expectedWebSource2File = new File( webAppDirectory, "org/web/app/last-exile.jsp" );
99          File expectedResourceFile = new File( webAppDirectory, "custom-setting.cfg" );
100         File expectedResourceWDirFile = new File( webAppDirectory, "custom-config/custom-setting.cfg" );
101 
102         assertTrue( "source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists() );
103         assertTrue( "source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists() );
104         assertTrue( "resource file not found:" + expectedResourceFile.toString(), expectedResourceFile.exists() );
105         assertTrue( "resource file with dir not found:" + expectedResourceWDirFile.toString(),
106                     expectedResourceWDirFile.exists() );
107 
108         // validate filtered file
109         content = FileUtils.fileRead( expectedResourceWDirFile );
110         BufferedReader reader = new BufferedReader( new StringReader( content ) );
111 
112         assertEquals( "error in filtering using System Properties", comment, reader.readLine() );
113 
114         String line = reader.readLine();
115         System.out.println( " line " + line );
116         System.out.println( " need " + System.getProperty( "user.dir" ) );
117         assertEquals( "error in filtering using System properties", "system_key_1=" + System.getProperty( "user.dir" ),
118                       line );
119         line = reader.readLine();
120         System.out.println( " line " + line );
121         assertEquals( "error in filtering using System properties", "system_key_2=" + System.getProperty( "user.dir" ),
122                       line );
123 
124         assertEquals( "error in filtering using project properties", "project_key_1=i_think_so", reader.readLine() );
125         assertEquals( "error in filtering using project properties", "project_key_2=i_think_so", reader.readLine() );
126 
127         assertEquals( "error in filtering using project properties", "project_name_1=Test Project ", reader.readLine() );
128         assertEquals( "error in filtering using project properties", "project_name_2=Test Project ", reader.readLine() );
129 
130         assertEquals( "error in filtering using System properties", "system_property_1=system-property-value",
131                       reader.readLine() );
132         assertEquals( "error in filtering using System properties", "system_property_2=system-property-value",
133                       reader.readLine() );
134 
135         // update property, and generate again
136         System.setProperty( "system.property", "new-system-property-value" );
137         this.configureMojo( mojo, filterList, classesDir, webAppSource, webAppDirectory, project );
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 }