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.resources.stub;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  
27  import org.apache.maven.model.Build;
28  import org.codehaus.plexus.util.FileUtils;
29  
30  import static org.apache.commons.io.FileUtils.deleteDirectory;
31  
32  public class MavenProjectBuildStub extends MavenProjectBasicStub {
33      protected Build build;
34  
35      protected String srcDirectory;
36  
37      protected String targetDirectory;
38  
39      protected String buildDirectory;
40  
41      protected String outputDirectory;
42  
43      protected String testOutputDirectory;
44  
45      protected String resourcesDirectory;
46  
47      protected String testResourcesDirectory;
48  
49      protected String targetResourceDirectory;
50  
51      protected String targetTestResourcesDirectory;
52  
53      protected ArrayList<String> fileList;
54  
55      protected ArrayList<String> directoryList;
56  
57      protected HashMap<String, String> dataMap;
58  
59      public MavenProjectBuildStub(String key) throws Exception {
60          super(key);
61  
62          build = new Build();
63          fileList = new ArrayList<>();
64          directoryList = new ArrayList<>();
65          dataMap = new HashMap<>();
66          setupBuild();
67      }
68  
69      public void addDirectory(String name) {
70          if (isValidPath(name)) {
71              directoryList.add(name);
72          }
73      }
74  
75      public void setOutputDirectory(String dir) {
76          outputDirectory = buildDirectory + "/" + dir;
77          build.setOutputDirectory(outputDirectory);
78      }
79  
80      public void addFile(String name) {
81          if (isValidPath(name)) {
82              fileList.add(name);
83          }
84      }
85  
86      public void addFile(String name, String data) {
87          File fileName = new File(name);
88  
89          addFile(name);
90          dataMap.put(fileName.getName(), data);
91      }
92  
93      public String getOutputDirectory() {
94          return outputDirectory;
95      }
96  
97      public String getTestOutputDirectory() {
98          return testOutputDirectory;
99      }
100 
101     public String getResourcesDirectory() {
102         return resourcesDirectory;
103     }
104 
105     public String getTestResourcesDirectory() {
106         return testResourcesDirectory;
107     }
108 
109     public Build getBuild() {
110         return build;
111     }
112 
113     /**
114      * returns true if the path is relative
115      * and false if absolute
116      * also returns false if it is relative to
117      * the parent
118      *
119      * @param path
120      * @return
121      */
122     private boolean isValidPath(String path) {
123         return !path.startsWith("c:") && !path.startsWith("..") && !path.startsWith("/") && !path.startsWith("\\");
124     }
125 
126     private void setupBuild() {
127         // check getBasedir method for the exact path
128         // we need to recreate the dir structure in
129         // an isolated environment
130         srcDirectory = testRootDir + "/src";
131         buildDirectory = testRootDir + "/target";
132         outputDirectory = buildDirectory + "/classes";
133         testOutputDirectory = buildDirectory + "/test-classes";
134         resourcesDirectory = srcDirectory + "/main/resources/";
135         testResourcesDirectory = srcDirectory + "/test/resources/";
136 
137         build.setDirectory(buildDirectory);
138         build.setOutputDirectory(outputDirectory);
139         build.setTestOutputDirectory(testOutputDirectory);
140     }
141 
142     public void cleanBuildEnvironment() throws Exception {
143         if (FileUtils.fileExists(resourcesDirectory)) {
144             deleteDirectory(new File(resourcesDirectory));
145         }
146 
147         if (FileUtils.fileExists(testResourcesDirectory)) {
148             deleteDirectory(new File(testResourcesDirectory));
149         }
150 
151         if (FileUtils.fileExists(outputDirectory)) {
152             deleteDirectory(new File(outputDirectory));
153         }
154 
155         if (FileUtils.fileExists(testOutputDirectory)) {
156             deleteDirectory(new File(testOutputDirectory));
157         }
158     }
159 
160     public void setupBuildEnvironment() throws Exception {
161         // populate dummy resources and dummy test resources
162 
163         // setup src dir
164         if (!FileUtils.fileExists(resourcesDirectory)) {
165             FileUtils.mkdir(resourcesDirectory);
166         }
167 
168         if (!FileUtils.fileExists(testResourcesDirectory)) {
169             FileUtils.mkdir(testResourcesDirectory);
170         }
171 
172         createDirectories(resourcesDirectory, testResourcesDirectory);
173         createFiles(resourcesDirectory, testResourcesDirectory);
174 
175         // setup target dir
176         if (!FileUtils.fileExists(outputDirectory)) {
177             FileUtils.mkdir(outputDirectory);
178         }
179 
180         if (!FileUtils.fileExists(testOutputDirectory)) {
181             FileUtils.mkdir(testOutputDirectory);
182         }
183     }
184 
185     private void createDirectories(String parent, String testparent) {
186         File currentDirectory;
187 
188         for (String directory : directoryList) {
189             currentDirectory = new File(parent, "/" + directory);
190 
191             if (!currentDirectory.exists()) {
192                 currentDirectory.mkdirs();
193             }
194 
195             // duplicate dir structure in test resources
196             currentDirectory = new File(testparent, "/" + directory);
197 
198             if (!currentDirectory.exists()) {
199                 currentDirectory.mkdirs();
200             }
201         }
202     }
203 
204     private void createFiles(String parent, String testparent) throws IOException {
205         File currentFile;
206 
207         for (String file : fileList) {
208             currentFile = new File(parent, file);
209 
210             // create the necessary parent directories
211             // before we create the files
212             if (!currentFile.getParentFile().exists()) {
213                 currentFile.getParentFile().mkdirs();
214             }
215 
216             if (!currentFile.exists()) {
217                 try {
218                     currentFile.createNewFile();
219                     populateFile(currentFile);
220                 } catch (IOException io) {
221                     // TODO: handle exception
222                 }
223             }
224 
225             // duplicate file in test resources
226             currentFile = new File(testparent, file);
227 
228             if (!currentFile.getParentFile().exists()) {
229                 currentFile.getParentFile().mkdirs();
230             }
231 
232             if (!currentFile.exists()) {
233                 currentFile.createNewFile();
234                 populateFile(currentFile);
235             }
236         }
237     }
238 
239     private void populateFile(File file) throws IOException {
240         String data = dataMap.get(file.getName());
241 
242         if ((data != null) && file.exists()) {
243             try (FileOutputStream outputStream = new FileOutputStream(file)) {
244                 outputStream.write(data.getBytes());
245             }
246         }
247     }
248 }