View Javadoc

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