View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.war;
20  
21  /*
22   * Licensed to the Apache Software Foundation (ASF) under one
23   * or more contributor license agreements.  See the NOTICE file
24   * distributed with this work for additional information
25   * regarding copyright ownership.  The ASF licenses this file
26   * to you under the Apache License, Version 2.0 (the
27   * "License"); you may not use this file except in compliance
28   * with the License.  You may obtain a copy of the License at
29   *
30   *    http://www.apache.org/licenses/LICENSE-2.0
31   *
32   * Unless required by applicable law or agreed to in writing,
33   * software distributed under the License is distributed on an
34   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35   * KIND, either express or implied.  See the License for the
36   * specific language governing permissions and limitations
37   * under the License.
38   */
39  
40  import java.io.BufferedReader;
41  import java.io.File;
42  import java.io.StringReader;
43  import java.util.LinkedList;
44  import java.util.List;
45  
46  import org.apache.maven.plugins.war.stub.MavenProjectBasicStub;
47  import org.apache.maven.plugins.war.stub.ResourceStub;
48  import org.codehaus.plexus.util.FileUtils;
49  
50  /**
51   * @author Olivier Lamy
52   * @since 21 juil. 2008
53   */
54  public class WarExplodedMojoFilteringTest extends AbstractWarExplodedMojoTest {
55  
56      protected File getPomFile() {
57          return new File(getBasedir(), "/target/test-classes/unit/warexplodedmojo/plugin-config.xml");
58      }
59  
60      protected File getTestDirectory() {
61          return new File(getBasedir(), "target/test-classes/unit/warexplodedmojo/test-dir");
62      }
63  
64      /**
65       * @throws Exception in case of an error.
66       */
67      public void testExplodedWar_WithResourceFiltering() throws Exception {
68          // setup test data
69          String testId = "ExplodedWar_WithResourceFiltering";
70          MavenProjectBasicStub project = new MavenProjectBasicStub();
71          File webAppDirectory = new File(getTestDirectory(), testId);
72          File webAppSource = createWebAppSource(testId);
73          File classesDir = createClassesDir(testId, false);
74          File webAppResource = new File(getTestDirectory(), testId + "-test-data/resources");
75          File sampleResource = new File(webAppResource, "custom-setting.cfg");
76          File sampleResourceWDir = new File(webAppResource, "custom-config/custom-setting.cfg");
77          List<String> filterList = new LinkedList<>();
78          ResourceStub[] resources = new ResourceStub[] {new ResourceStub()};
79  
80          createFile(sampleResource);
81          createFile(sampleResourceWDir);
82  
83          String ls = System.getProperty("line.separator");
84          final String comment = "# this is comment created by author@somewhere@";
85          // prepare web resources
86          String content = comment + ls;
87          content += "system_key_1=${user.dir}" + ls;
88          content += "system_key_2=@user.dir@" + ls;
89          content += "project_key_1=${is_this_simple}" + ls;
90          content += "project_key_2=@is_this_simple@" + ls;
91          content += "project_name_1=${project.name}" + ls;
92          content += "project_name_2=@project.name@" + ls;
93          content += "system_property_1=${system.property}" + ls;
94          content += "system_property_2=@system.property@" + ls;
95          FileUtils.fileWrite(sampleResourceWDir.getAbsolutePath(), content);
96          FileUtils.fileWrite(sampleResource.getAbsolutePath(), content);
97  
98          System.setProperty("system.property", "system-property-value");
99  
100         // configure mojo
101         project.addProperty("is_this_simple", "i_think_so");
102         resources[0].setDirectory(webAppResource.getAbsolutePath());
103         resources[0].setFiltering(true);
104         this.configureMojo(mojo, filterList, classesDir, webAppSource, webAppDirectory, project);
105         setVariableValueToObject(mojo, "webResources", resources);
106         mojo.execute();
107 
108         // validate operation
109         File expectedWebSourceFile = new File(webAppDirectory, "pansit.jsp");
110         File expectedWebSource2File = new File(webAppDirectory, "org/web/app/last-exile.jsp");
111         File expectedResourceFile = new File(webAppDirectory, "custom-setting.cfg");
112         File expectedResourceWDirFile = new File(webAppDirectory, "custom-config/custom-setting.cfg");
113 
114         assertTrue("source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists());
115         assertTrue("source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists());
116         assertTrue("resource file not found:" + expectedResourceFile.toString(), expectedResourceFile.exists());
117         assertTrue(
118                 "resource file with dir not found:" + expectedResourceWDirFile.toString(),
119                 expectedResourceWDirFile.exists());
120 
121         // validate filtered file
122         content = FileUtils.fileRead(expectedResourceWDirFile);
123         BufferedReader reader = new BufferedReader(new StringReader(content));
124 
125         assertEquals("error in filtering using System Properties", comment, reader.readLine());
126 
127         String line = reader.readLine();
128         System.out.println(" line " + line);
129         System.out.println(" need " + System.getProperty("user.dir"));
130         assertEquals(
131                 "error in filtering using System properties", "system_key_1=" + System.getProperty("user.dir"), line);
132         line = reader.readLine();
133         System.out.println(" line " + line);
134         assertEquals(
135                 "error in filtering using System properties", "system_key_2=" + System.getProperty("user.dir"), line);
136 
137         assertEquals("error in filtering using project properties", "project_key_1=i_think_so", reader.readLine());
138         assertEquals("error in filtering using project properties", "project_key_2=i_think_so", reader.readLine());
139 
140         assertEquals("error in filtering using project properties", "project_name_1=Test Project ", reader.readLine());
141         assertEquals("error in filtering using project properties", "project_name_2=Test Project ", reader.readLine());
142 
143         assertEquals(
144                 "error in filtering using System properties",
145                 "system_property_1=system-property-value",
146                 reader.readLine());
147         assertEquals(
148                 "error in filtering using System properties",
149                 "system_property_2=system-property-value",
150                 reader.readLine());
151 
152         // update property, and generate again
153         System.setProperty("system.property", "new-system-property-value");
154         this.configureMojo(mojo, filterList, classesDir, webAppSource, webAppDirectory, project);
155 
156         mojo.execute();
157 
158         // validate filtered file
159         content = FileUtils.fileRead(expectedResourceWDirFile);
160         reader = new BufferedReader(new StringReader(content));
161 
162         assertEquals("error in filtering using System Properties", comment, reader.readLine());
163 
164         assertEquals(
165                 "error in filtering using System properties",
166                 "system_key_1=" + System.getProperty("user.dir"),
167                 reader.readLine());
168         assertEquals(
169                 "error in filtering using System properties",
170                 "system_key_2=" + System.getProperty("user.dir"),
171                 reader.readLine());
172 
173         assertEquals("error in filtering using project properties", "project_key_1=i_think_so", reader.readLine());
174         assertEquals("error in filtering using project properties", "project_key_2=i_think_so", reader.readLine());
175 
176         assertEquals("error in filtering using project properties", "project_name_1=Test Project ", reader.readLine());
177         assertEquals("error in filtering using project properties", "project_name_2=Test Project ", reader.readLine());
178 
179         assertEquals(
180                 "error in filtering using System properties",
181                 "system_property_1=new-system-property-value",
182                 reader.readLine());
183         assertEquals(
184                 "error in filtering using System properties",
185                 "system_property_2=new-system-property-value",
186                 reader.readLine());
187 
188         // update property, and generate again
189         File filterFile = new File(getTestDirectory(), testId + "-test-data/filters/filter.properties");
190         createFile(filterFile);
191         filterList.add(filterFile.getAbsolutePath());
192         content += "resource_key_1=${resource_value_1}\n";
193         content += "resource_key_2=@resource_value_2@\n" + content;
194         FileUtils.fileWrite(sampleResourceWDir.getAbsolutePath(), content);
195         FileUtils.fileWrite(sampleResource.getAbsolutePath(), content);
196         String filterContent = "resource_value_1=this_is_filtered\n";
197         filterContent += "resource_value_2=this_is_filtered";
198         FileUtils.fileWrite(filterFile.getAbsolutePath(), filterContent);
199 
200         mojo.execute();
201 
202         // validate filtered file
203         content = FileUtils.fileRead(expectedResourceWDirFile);
204         reader = new BufferedReader(new StringReader(content));
205 
206         assertEquals("error in filtering using System Properties", comment, reader.readLine());
207 
208         assertEquals(
209                 "error in filtering using System properties",
210                 "system_key_1=" + System.getProperty("user.dir"),
211                 reader.readLine());
212         assertEquals(
213                 "error in filtering using System properties",
214                 "system_key_2=" + System.getProperty("user.dir"),
215                 reader.readLine());
216 
217         assertEquals("error in filtering using project properties", "project_key_1=i_think_so", reader.readLine());
218         assertEquals("error in filtering using project properties", "project_key_2=i_think_so", reader.readLine());
219 
220         assertEquals("error in filtering using project properties", "project_name_1=Test Project ", reader.readLine());
221         assertEquals("error in filtering using project properties", "project_name_2=Test Project ", reader.readLine());
222 
223         assertEquals(
224                 "error in filtering using System properties",
225                 "system_property_1=new-system-property-value",
226                 reader.readLine());
227         assertEquals(
228                 "error in filtering using System properties",
229                 "system_property_2=new-system-property-value",
230                 reader.readLine());
231 
232         assertEquals("error in filtering using filter files", "resource_key_1=this_is_filtered", reader.readLine());
233         assertEquals("error in filtering using filter files", "resource_key_2=this_is_filtered", reader.readLine());
234 
235         // house keeping
236         expectedWebSourceFile.delete();
237         expectedWebSource2File.delete();
238         expectedResourceFile.delete();
239         expectedResourceWDirFile.delete();
240     }
241 }