View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.doc;
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  import org.apache.commons.io.FilenameUtils;
22  
23  /**
24   * Helps with creating directories and files.
25   *
26   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
27   * @version $Revision:591 $
28   */
29  public class FileHelper
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The root directory to which the generated files are written.
39     */
40    private final File rootDir;
41  
42    /**
43     * The extension for the files being exported by this instance (such as
44     * <code>xml</code> or <code>properties</code>).
45     */
46    private final String fileNameExtension;
47  
48    /**
49     * The flag determines whether an existing file should be overwritten (
50     * <code>true</code>) or not (<code>false</code>).
51     */
52    private final boolean overwrite;
53  
54    // ****************************** Initializer *******************************
55  
56    // ****************************** Constructors ******************************
57  
58    /**
59     * Convenience constructor selecting the option to overwrite.
60     *
61     * @param rootDir the root directory to which the generated files are written.
62     * @param fileNameExtension the extension for the files being exported by this
63     *          instance (such as <code>xml</code> or <code>properties</code>).
64     */
65    public FileHelper(final File rootDir, final String fileNameExtension)
66    {
67      this(rootDir, fileNameExtension, true);
68    }
69  
70    /**
71     * Default constructor.
72     *
73     * @param rootDir the root directory to which the generated files are written.
74     * @param fileNameExtension the extension for the files being exported by this
75     *          instance (such as <code>xml</code> or <code>properties</code>).
76     * @param overwrite the flag determines whether an existing file should be
77     *          overwritten ( <code>true</code>) or not (<code>false</code>).
78     */
79    public FileHelper(final File rootDir, final String fileNameExtension,
80        final boolean overwrite)
81    {
82      this.rootDir = rootDir;
83      this.fileNameExtension = fileNameExtension;
84      this.overwrite = overwrite;
85    }
86  
87    // ****************************** Inner Classes *****************************
88  
89    // ********************************* Methods ********************************
90  
91    // --- init -----------------------------------------------------------------
92  
93    // --- get&set --------------------------------------------------------------
94  
95    // --- business -------------------------------------------------------------
96  
97    public final File provideFile(final Type uutType) throws IOException
98    {
99      final File outputFile = createFileRef(uutType, null);
100     return provideFile(outputFile);
101   }
102 
103   public final File provideFile(final Type testCaseType, final Type uutType)
104     throws IOException
105   {
106     final File outputFile = createFileRef(testCaseType, uutType);
107     return provideFile(outputFile);
108   }
109 
110   private File provideFile(final File outputFile) throws IOException
111   {
112     final String fileName = outputFile.getName();
113     final File dir = outputFile.getParentFile();
114     if (outputFile.exists())
115     {
116       if (overwrite && !outputFile.canWrite())
117       {
118         throw new IOException("Cannot write test documentation file '"
119                               + outputFile.getAbsolutePath() + "'.");
120       }
121       else if (!overwrite) // TODO: Do we still need this?
122       {
123         final String baseName = FilenameUtils.getBaseName(fileName);
124         final String extension = FilenameUtils.getExtension(fileName);
125         File file = new File(dir, baseName + "2." + extension);
126         for (int i = 3; file.exists(); i++)
127         {
128           file = new File(dir, baseName + i + '.' + extension);
129         }
130         return file;
131       }
132     }
133     return outputFile;
134   }
135 
136   private File createFileRef(final Type testCaseType, final Type uutType)
137     throws IOException
138   {
139     final File dir = provideDirectory(testCaseType);
140     final String fileName = createFileName(testCaseType, uutType);
141     final File file = new File(dir, fileName);
142     return file;
143   }
144 
145   private String createFileName(final Type type1, final Type type2)
146   {
147     final String stem =
148         type1.getTypeName() + (type2 != null ? '-' + type2.getTypeName() : "");
149     return stem + '.' + fileNameExtension;
150   }
151 
152   public final File[] listFiles()
153   {
154     return rootDir.listFiles();
155   }
156 
157   private File provideDirectory(final Type testCaseType) throws IOException
158   {
159     final File dir = createDirectoryRef(testCaseType);
160     try
161     {
162       return provideDir(dir);
163     }
164     catch (final IOException e)
165     {
166       throw new IOException("Cannot create directory '" + dir.getAbsolutePath()
167                             + "' for test documentation of UUT type '"
168                             + testCaseType + "'.", e);
169     }
170   }
171 
172   public static final File provideDir(final File dir) throws IOException
173   {
174     if (!dir.exists())
175     {
176       final boolean created = dir.mkdirs();
177       if (!created)
178       {
179         throw new IOException("Cannot create directory '"
180                               + dir.getAbsolutePath() + "'.");
181       }
182     }
183     return dir;
184   }
185 
186   public File createDirectoryRef(final Type testCaseType)
187   {
188     final String packageName = testCaseType.getPackageName();
189     final File dir;
190     if (packageName != null)
191     {
192       final String directoryName = packageName.replace('.', '/');
193       dir = new File(rootDir, directoryName);
194     }
195     else
196     {
197       dir = rootDir;
198     }
199     return dir;
200   }
201 
202   // --- object basics --------------------------------------------------------
203 
204 }