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