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