View Javadoc

1   package org.apache.torque.generator.control;
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.regex.Matcher;
30  import java.util.regex.Pattern;
31  
32  import org.apache.commons.io.FileUtils;
33  import org.apache.torque.generator.BaseTest;
34  import org.apache.torque.generator.GeneratorException;
35  import org.apache.torque.generator.configuration.UnitDescriptor;
36  import org.apache.torque.generator.configuration.paths.CustomProjectPaths;
37  import org.apache.torque.generator.configuration.paths.DefaultTorqueGeneratorPaths;
38  import org.apache.torque.generator.configuration.paths.Maven2DirectoryProjectPaths;
39  import org.junit.Before;
40  import org.junit.Test;
41  
42  public class ExistingTargetStrategyTest extends BaseTest
43  {
44      private final File confRootDir = new File("src/test/existingTargetStrategy");
45      private final File targetDir1 = new File("target/test/existingTargetStrategy/target1");
46      private final File targetDir2 = new File("target/test/existingTargetStrategy/target2");
47      private final File workDir
48          = new File("target/test/existingTargetStrategy/work");
49      private final File targetFile1 = new File(targetDir1, "output1.txt");
50      private final File targetFile2 = new File(targetDir2, "output2.txt");
51      private final File workFile1 = new File(workDir, "raw-generated/default/output1.txt");
52      private final File workFile2 = new File(workDir, "raw-generated/other/outputDirKey2/output2.txt");
53  
54      private final String srcPath1 = "src/main/torque-gen/src/source1.properties";
55      private final String srcPath2 = "src/main/torque-gen/src/source2.properties";
56  
57      @Before
58      public void setUp() throws IOException
59      {
60          FileUtils.deleteDirectory(targetDir1);
61          FileUtils.deleteDirectory(targetDir2);
62          FileUtils.deleteDirectory(workDir);
63      }
64  
65      @Test
66      public void testReplaceStrategy() throws Exception
67      {
68          File configurationDirectory = new File(confRootDir, "initial");
69          runGeneration(configurationDirectory, null);
70          assertFile(new File(configurationDirectory, srcPath1), targetFile1);
71          assertFile(new File(configurationDirectory, srcPath2), targetFile2);
72          configurationDirectory = new File(confRootDir, "replaceStrategy");
73          runGeneration(configurationDirectory, null);
74          assertFile(new File(configurationDirectory, srcPath1), targetFile1);
75          assertFile(new File(configurationDirectory, srcPath2), targetFile2);
76      }
77  
78      @Test
79      public void testMergeStrategyChangeGenerationResult() throws Exception
80      {
81          File configurationDirectory = new File(confRootDir, "mergeStrategy");
82          File initialSourceDir
83                  = new File(confRootDir, "/initial/src/main/torque-gen/src");
84          runGeneration(configurationDirectory, initialSourceDir);
85          File initialSourceDirectory = new File(confRootDir, "initial");
86          assertFile(new File(initialSourceDirectory, srcPath1), targetFile1);
87          assertFile(new File(initialSourceDirectory, srcPath2), targetFile2);
88          assertFile(new File(initialSourceDirectory, srcPath1), workFile1);
89          assertFile(new File(initialSourceDirectory, srcPath2), workFile2);
90  
91          // change the generation result but do not change the target file
92          // -> new target must look like new generation result
93          File mergeSourceDir  = new File(
94                  confRootDir,
95                  "/mergeStrategy/src/main/torque-gen/src");
96          runGeneration(configurationDirectory, mergeSourceDir);
97          assertFile(new File(configurationDirectory, srcPath1), targetFile1);
98          assertFile(new File(configurationDirectory, srcPath2), targetFile2);
99          assertFile(new File(configurationDirectory, srcPath1), workFile1);
100         assertFile(new File(configurationDirectory, srcPath2), workFile2);
101     }
102 
103     @Test
104     public void testMergeStrategyChangeTarget() throws Exception
105     {
106         File configurationDirectory = new File(confRootDir, "mergeStrategy");
107         File initialSourceDir
108                 = new File(confRootDir, "/initial/src/main/torque-gen/src");
109         runGeneration(configurationDirectory, initialSourceDir);
110         File initialSourceDirectory = new File(confRootDir, "initial");
111         assertFile(new File(initialSourceDirectory, srcPath1), targetFile1);
112         assertFile(new File(initialSourceDirectory, srcPath2), targetFile2);
113         assertFile(new File(initialSourceDirectory, srcPath1), workFile1);
114         assertFile(new File(initialSourceDirectory, srcPath2), workFile2);
115 
116         // change the target file result but do not change the generation result
117         // -> new target must not change
118         File mergeStrategyDir  = new File(
119                 confRootDir,
120                 "/mergeStrategy");
121         String intermediateTargetContent1 = FileUtils.readFileToString(
122                 new File(mergeStrategyDir, srcPath1),
123                 "ISO-8859-1");
124         intermediateTargetContent1 = Pattern.compile("#.*\\r?\\n{1}?")
125                 .matcher(intermediateTargetContent1)
126                 .replaceAll("");
127         FileUtils.writeStringToFile(
128                 targetFile1,
129                 intermediateTargetContent1,
130                 "ISO-8859-1");
131         String intermediateTargetContent2 = FileUtils.readFileToString(
132                 new File(mergeStrategyDir, srcPath2),
133                 "ISO-8859-1");
134         intermediateTargetContent2 = Pattern.compile("#.*\\r?\\n{1}?")
135                 .matcher(intermediateTargetContent2)
136                 .replaceAll("");
137         FileUtils.writeStringToFile(
138                 targetFile2,
139                 intermediateTargetContent2,
140                 "ISO-8859-1");
141         runGeneration(configurationDirectory, initialSourceDir);
142         String endGenerationContent1
143                 = FileUtils.readFileToString(targetFile1, "ISO-8859-1");
144         assertEquals(intermediateTargetContent1, endGenerationContent1);
145         String endGenerationContent2
146                 = FileUtils.readFileToString(targetFile2, "ISO-8859-1");
147         assertEquals(intermediateTargetContent2, endGenerationContent2);
148         assertFile(new File(initialSourceDirectory, srcPath1), workFile1);
149         assertFile(new File(initialSourceDirectory, srcPath2), workFile2);
150     }
151 
152     @Test
153     public void testMergeStrategyChangeGenerationResultAndTarget()
154             throws Exception
155     {
156         File configurationDirectory = new File(confRootDir, "mergeStrategy");
157         File initialSourceDir
158                 = new File(confRootDir, "/initial/src/main/torque-gen/src");
159         runGeneration(configurationDirectory, initialSourceDir);
160         File initialSourceDirectory = new File(confRootDir, "initial");
161         assertFile(new File(initialSourceDirectory, srcPath1), targetFile1);
162         assertFile(new File(initialSourceDirectory, srcPath2), targetFile2);
163         assertFile(new File(initialSourceDirectory, srcPath1), workFile1);
164         assertFile(new File(initialSourceDirectory, srcPath2), workFile2);
165 
166         // change the target file result and the generation result
167         File newTargetContentsFile1 = new File(
168                 configurationDirectory,
169                 "src/main/torque-gen/src/source1.properties");
170         String newTargetContent1 = FileUtils.readFileToString(
171                 newTargetContentsFile1,
172                 "ISO-8859-1");
173         newTargetContent1 = Pattern.compile("#.*\\r?\\n{1}?")
174                 .matcher(newTargetContent1)
175                 .replaceAll("");
176         FileUtils.writeStringToFile(targetFile1, newTargetContent1, "ISO-8859-1");
177         File newTargetContentsFile2 = new File(
178                 configurationDirectory,
179                 "src/main/torque-gen/src/source2.properties");
180         String newTargetContent2 = FileUtils.readFileToString(
181                 newTargetContentsFile2,
182                 "ISO-8859-1");
183         newTargetContent2 = Pattern.compile("#.*\\r?\\n{1}?")
184                 .matcher(newTargetContent2)
185                 .replaceAll("");
186         FileUtils.writeStringToFile(targetFile2, newTargetContent2, "ISO-8859-1");
187         File conflictSourceDir = new File(configurationDirectory, "conflict");
188         runGeneration(configurationDirectory, conflictSourceDir);
189         assertFile(
190                 new File(conflictSourceDir, "expected1.properties"),
191                 targetFile1);
192         assertFile(
193                 new File(conflictSourceDir, "expected2.properties"),
194                 targetFile2);
195         assertFile(new File(conflictSourceDir, "source1.properties"), workFile1);
196         assertFile(new File(conflictSourceDir, "source2.properties"), workFile2);
197     }
198 
199     @Test
200     public void testSkipStrategy() throws Exception
201     {
202         File initialConfigurationDirectory = new File(confRootDir, "initial");
203         runGeneration(initialConfigurationDirectory, null);
204         assertFile(new File(initialConfigurationDirectory, srcPath1), targetFile1);
205         assertFile(new File(initialConfigurationDirectory, srcPath2), targetFile2);
206         File newConfigurationDirectory = new File(confRootDir, "skipStrategy");
207         runGeneration(newConfigurationDirectory, null);
208         assertFile(new File(initialConfigurationDirectory, srcPath1), targetFile1);
209         assertFile(new File(initialConfigurationDirectory, srcPath2), targetFile2);
210     }
211 
212     @Test
213     public void testAppendStrategy() throws Exception
214     {
215         File configurationDirectoryInitial = new File(confRootDir, "initial");
216         runGeneration(configurationDirectoryInitial, null);
217         assertFile(
218                 new File(configurationDirectoryInitial, srcPath1),
219                 targetFile1);
220         assertFile(
221                 new File(configurationDirectoryInitial, srcPath2),
222                 targetFile2);
223         File configurationDirectoryAppend
224                 = new File(confRootDir, "appendStrategy");
225         runGeneration(configurationDirectoryAppend, null);
226         File expectedTargetFile1 = new File(targetDir1, "expectedOutput1.txt");
227         String initial1 = FileUtils.readFileToString(
228                 new File(configurationDirectoryInitial, srcPath1),
229                 "ISO-8859-1");
230         String appended1 = FileUtils.readFileToString(
231                 new File(configurationDirectoryAppend, srcPath1),
232                 "ISO-8859-1");
233         FileUtils.writeStringToFile(expectedTargetFile1, initial1 + appended1);
234         File expectedTargetFile2 = new File(targetDir1, "expectedOutput2.txt");
235         String initial2 = FileUtils.readFileToString(
236                 new File(configurationDirectoryInitial, srcPath2),
237                 "ISO-8859-1");
238         String appended2 = FileUtils.readFileToString(
239                 new File(configurationDirectoryAppend, srcPath2),
240                 "ISO-8859-1");
241         FileUtils.writeStringToFile(expectedTargetFile2, initial2 + appended2);
242         assertFile(expectedTargetFile1, targetFile1);
243         assertFile(expectedTargetFile2, targetFile2);
244     }
245 
246     private File runGeneration(File configurationDirectory, File sourceDir)
247             throws GeneratorException
248     {
249         Controller controller = new Controller();
250         List<UnitDescriptor> unitDescriptors = new ArrayList<UnitDescriptor>();
251         CustomProjectPaths projectPaths = new CustomProjectPaths(
252                 new Maven2DirectoryProjectPaths(configurationDirectory));
253         projectPaths.setOutputDirectory(null, targetDir1);
254         projectPaths.setOutputDirectory("outputDirKey2", targetDir2);
255         projectPaths.setWorkDir(workDir);
256         if (sourceDir != null)
257         {
258             projectPaths.setSourceDir(sourceDir);
259         }
260         unitDescriptors.add(new UnitDescriptor(
261                 UnitDescriptor.Packaging.DIRECTORY,
262                 projectPaths,
263                 new DefaultTorqueGeneratorPaths()));
264         controller.run(unitDescriptors);
265         return configurationDirectory;
266     }
267 
268     private void assertFile(File expectedFile, File actualFile)
269             throws IOException
270     {
271         assertTrue(actualFile.exists());
272         String expected = FileUtils.readFileToString(expectedFile);
273         // remove Apache license header
274         expected = Pattern.compile("#.*\\r?\\n{1}?")
275             .matcher(expected)
276             .replaceAll("");
277         String actual = FileUtils.readFileToString(actualFile);
278         assertEquals(expected, actual);
279     }
280 }