1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25
26
27
28
29 public class FileHelper
30 {
31
32
33
34
35
36
37
38
39
40 private final File rootDir;
41
42
43
44
45
46 private final String fileNameExtension;
47
48
49
50
51
52 private final boolean overwrite;
53
54
55
56
57
58
59
60
61
62
63
64
65 public FileHelper(final File rootDir, final String fileNameExtension)
66 {
67 this(rootDir, fileNameExtension, true);
68 }
69
70
71
72
73
74
75
76
77
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
88
89
90
91
92
93
94
95
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)
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
203
204 }