1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package com.qulice.spi;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.nio.charset.Charset;
36 import java.nio.charset.StandardCharsets;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.LinkedList;
43 import java.util.Map;
44 import java.util.Set;
45 import org.apache.commons.io.FileUtils;
46 import org.apache.commons.io.filefilter.DirectoryFileFilter;
47 import org.apache.commons.io.filefilter.IOFileFilter;
48 import org.apache.commons.io.filefilter.WildcardFileFilter;
49
50
51
52
53
54
55 @SuppressWarnings("PMD.TooManyMethods")
56 public interface Environment {
57
58
59
60
61
62 File basedir();
63
64
65
66
67
68 File tempdir();
69
70
71
72
73
74 File outdir();
75
76
77
78
79
80
81
82 String param(String name, String value);
83
84
85
86
87
88 ClassLoader classloader();
89
90
91
92
93
94 Collection<String> classpath();
95
96
97
98
99
100
101
102
103
104
105
106 Collection<File> files(String pattern);
107
108
109
110
111
112
113
114 boolean exclude(String check, String name);
115
116
117
118
119
120
121
122
123
124 Collection<String> excludes(String checker);
125
126
127
128
129
130 Charset encoding();
131
132
133
134
135
136
137 final class Mock implements Environment {
138
139
140
141 private final File basedir;
142
143
144
145
146 private final Set<String> classpath;
147
148
149
150
151 private final Map<String, String> params;
152
153
154
155
156 private String excl;
157
158
159
160
161
162 @SuppressWarnings(
163 "PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
164 )
165 public Mock() throws IOException {
166 this.params = new HashMap<>();
167 this.classpath = new HashSet<>(1);
168 final File temp = File.createTempFile(
169 "mock", ".qulice",
170 new File(System.getProperty("java.io.tmpdir"))
171 );
172 if (!temp.delete()) {
173 throw new IllegalStateException("files collision");
174 }
175 if (!temp.mkdirs()) {
176 throw new IllegalStateException("mkdir failed");
177 }
178 FileUtils.forceDeleteOnExit(temp);
179 this.basedir = new File(temp, "basedir");
180 if (this.basedir.mkdirs()) {
181 assert this.basedir != null;
182 }
183 this.classpath.add(
184 this.outdir().getAbsolutePath().replace(File.separatorChar, '/')
185 );
186 }
187
188
189
190
191
192
193
194 public Environment.Mock withParam(final String name,
195 final String value) {
196 this.params.put(name, value);
197 return this;
198 }
199
200
201
202
203
204
205
206
207 public Environment.Mock withFile(final String name,
208 final String content) throws IOException {
209 final File file = new File(this.basedir, name);
210 FileUtils.writeStringToFile(
211 file,
212 content,
213 StandardCharsets.UTF_8
214 );
215 return this;
216 }
217
218
219
220
221
222
223
224
225 public Environment.Mock withFile(final String name,
226 final byte[] bytes) throws IOException {
227 final File file = new File(this.basedir, name);
228 FileUtils.writeByteArrayToFile(file, bytes);
229 return this;
230 }
231
232
233
234
235
236
237 public Environment.Mock withExcludes(final String excludes) {
238 this.excl = excludes;
239 return this;
240 }
241
242
243
244
245
246 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
247 public Environment.Mock withDefaultClasspath() {
248 Collections.addAll(
249 this.classpath,
250 System.getProperty("java.class.path")
251 .split(System.getProperty("path.separator"))
252 );
253 return this;
254 }
255
256 @Override
257 public File basedir() {
258 return this.basedir;
259 }
260
261 @Override
262 public File tempdir() {
263 final File file = new File(this.basedir, "target/tempdir");
264 if (file.mkdirs()) {
265 assert file != null;
266 }
267 return file;
268 }
269
270 @Override
271 public File outdir() {
272 final File file = new File(this.basedir, "target/classes");
273 if (file.mkdirs()) {
274 assert file != null;
275 }
276 return file;
277 }
278
279 @Override
280 public String param(final String name, final String value) {
281 String val = this.params.get(name);
282 if (val == null) {
283 val = value;
284 }
285 return val;
286 }
287
288 @Override
289 public ClassLoader classloader() {
290 return Thread.currentThread().getContextClassLoader();
291 }
292
293 @Override
294 public Collection<String> classpath() {
295 return Collections.unmodifiableCollection(this.classpath);
296 }
297
298 @Override
299 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
300 public Collection<File> files(final String pattern) {
301 final Collection<File> files = new LinkedList<>();
302 final IOFileFilter filter = WildcardFileFilter.builder().setWildcards(pattern).get();
303 for (final String dir : new String[]{"src"}) {
304 final File sources = new File(this.basedir(), dir);
305 if (sources.exists()) {
306 files.addAll(
307 FileUtils.listFiles(
308 sources,
309 filter,
310 DirectoryFileFilter.INSTANCE
311 )
312 );
313 }
314 }
315 return files;
316 }
317
318 @Override
319 public boolean exclude(final String check, final String name) {
320 return false;
321 }
322
323 @Override
324 public Collection<String> excludes(final String checker) {
325 final Collection<String> exc;
326 if (this.excl == null) {
327 exc = Collections.emptyList();
328 } else {
329 exc = Arrays.asList(this.excl.split(","));
330 }
331 return exc;
332 }
333
334 @Override
335 public Charset encoding() {
336 return StandardCharsets.UTF_8;
337 }
338 }
339 }