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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.jack.test.toolchain;
import com.google.common.io.Files;
import com.android.jack.Sourcelist;
import com.android.jack.test.TestConfigurationException;
import com.android.jack.test.TestsProperties;
import com.android.jack.test.runner.RuntimeRunner;
import com.android.jack.test.runner.RuntimeRunnerException;
import com.android.jack.test.runner.RuntimeRunnerFactory;
import com.android.jack.test.util.ExecFileException;
import com.android.jack.test.util.ExecuteFile;
import com.android.jack.util.NamingTools;
import org.junit.Assume;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* Utility class that act also as a Factory for toolchains. It holds the global
* configuration for tests.
*/
public abstract class AbstractTestTools {
@Nonnull
public static final String JUNIT_RUNNER_NAME = "org.junit.runner.JUnitCore";
@Nonnull
private static HashMap<String, ToolchainBuilder> toolchainBuilders;
@Nonnull
private static final String JACK_TESTS_FOLDER = "jack-tests";
@Nonnull
private static final String PROPERTY_VALUE_SEPARATOR = ",";
@Nonnull
private static final String TOOLCHAIN_REFERENCE_KEY = "toolchain.reference";
@Nonnull
private static final String TOOLCHAIN_CANDIDATE_KEY = "toolchain.candidate";
@Nonnull
private static final String RUNTIME_LIST_KEY = "runtime.list";
@Nonnull
private static final String RUNTIME_LOCATION_PREFIX = "runtime.location.";
@Nonnull
private static final String TOOL_PREFIX = "tool.";
@Nonnull
private static final String TOOLCHAIN_PREBUILT_PREFIX = "toolchain.prebuilt.";
@Nonnull
private static final List<RuntimeRunner> runtimes = new ArrayList<RuntimeRunner>();
@Nonnull
private static final Map<String, File> runtimeEnvironmentLocations = new HashMap<String, File>();
static {
toolchainBuilders = new HashMap<String, ToolchainBuilder>();
toolchainBuilders.put("jack-cli" , new JackCliToolchainBuilder());
toolchainBuilders.put("jack-api-v01" , new JackApiV01ToolchainBuilder());
toolchainBuilders.put("legacy" , new LegacyToolchainBuilder());
toolchainBuilders.put("jill-legacy" , new LegacyJillToolchainBuilder());
try {
runtimes.addAll(parseRuntimeList(TestsProperties.getProperty(RUNTIME_LIST_KEY)));
} catch (SecurityException e) {
throw new TestConfigurationException(e);
} catch (IllegalArgumentException e) {
throw new TestConfigurationException(e);
} catch (RuntimeRunnerException e) {
throw new TestConfigurationException(e);
}
}
private interface ToolchainBuilder {
IToolchain build();
}
private static class LegacyToolchainBuilder implements ToolchainBuilder {
@Override
@Nonnull
public LegacyToolchain build() {
return new LegacyToolchain(getPrebuilt("legacy-java-compiler"), getPrebuilt("jarjar"),
getPrebuilt("proguard"));
}
}
private static class JackCliToolchainBuilder implements ToolchainBuilder {
@Override
@Nonnull
public JackCliToolchain build() {
return new JackCliToolchain(getPrebuilt("jack"));
}
}
private static class JackApiV01ToolchainBuilder implements ToolchainBuilder {
@Override
@Nonnull
public JackApiV01Toolchain build() {
return new JackApiV01Toolchain(getPrebuilt("jack"));
}
}
private static class LegacyJillToolchainBuilder implements ToolchainBuilder {
@Override
public IToolchain build() {
return new LegacyJillToolchain(getPrebuilt("legacy-java-compiler"), getPrebuilt("jill"),
getPrebuilt("jack"), getPrebuilt("jarjar"), getPrebuilt("proguard"));
}
}
public static File getPrebuilt(@Nonnull String prebuiltName) {
String prebuiltVarName = TOOLCHAIN_PREBUILT_PREFIX + prebuiltName;
String prebuiltPath;
try {
prebuiltPath = TestsProperties.getProperty(prebuiltVarName);
} catch (TestConfigurationException e) {
throw new TestConfigurationException(
"Cannot find path for prebuilt '" + prebuiltName + "'", e);
}
File result = new File(prebuiltPath);
if (!result.isAbsolute()) {
result = new File(TestsProperties.getJackRootDir(), prebuiltPath);
}
if (!result.exists()) {
throw new TestConfigurationException(
"Can not find '" + prebuiltName + "' prebuilt at '" + result.getPath() + "'");
}
return result;
}
@Nonnull
private static final File getTestsRootDir() {
return new File(TestsProperties.getJackRootDir(),
JACK_TESTS_FOLDER + File.separator + "tests");
}
@Nonnull
public static final File getTestRootDir(@Nonnull String packageName) {
return new File(getTestsRootDir(), packageName.replace(".", File.separator));
}
@SuppressWarnings("unchecked")
@Nonnull
public static final <T extends IToolchain> T getCandidateToolchain() {
IToolchain result = createToolchain("candidate.toolchain");
return (T) result;
}
@Nonnull
public static final <T extends IToolchain> T getCandidateToolchain(
@Nonnull Class<? extends IToolchain> clazz) {
T result = getCandidateToolchain();
Assume.assumeTrue(clazz.isAssignableFrom(result.getClass()));
return result;
}
@Nonnull
public static final <T extends IToolchain> T getCandidateToolchain(
@Nonnull Class<? extends IToolchain> clazz,
@Nonnull List<Class<? extends IToolchain>> excludeClazz) {
T result = getCandidateToolchain(clazz);
for (Class<? extends IToolchain> c : excludeClazz) {
Assume.assumeTrue(!c.isAssignableFrom(result.getClass()));
}
return result;
}
@SuppressWarnings("unchecked")
@Nonnull
public static final <T extends IToolchain> T getReferenceToolchain() {
return (T) createToolchain("reference.toolchain");
}
@Nonnull
public static final <T extends IToolchain> T getReferenceToolchain(
@Nonnull Class<? extends IToolchain> clazz) {
T result = getReferenceToolchain();
Assume.assumeTrue(clazz.isAssignableFrom(result.getClass()));
return result;
}
@Nonnull
public static final <T extends IToolchain> T getReferenceToolchain(
@Nonnull Class<? extends IToolchain> clazz,
@Nonnull List<Class<? extends IToolchain>> excludeClazz) {
T result = getReferenceToolchain(clazz);
for (Class<? extends IToolchain> c : excludeClazz) {
Assume.assumeTrue(!c.isAssignableFrom(result.getClass()));
}
return result;
}
@Nonnull
private static IToolchain createToolchain(@Nonnull String propertyName) {
return getToolchainBuilder(TestsProperties.getProperty(propertyName)).build();
}
@Nonnull
private static ToolchainBuilder getToolchainBuilder(@Nonnull String toolchainName) {
ToolchainBuilder toolchainBuilder = toolchainBuilders.get(toolchainName);
if (toolchainBuilder == null) {
throw new TestConfigurationException("Unknown toolchain: '" + toolchainName + "'");
}
return toolchainBuilder;
}
@Nonnull
public static File createTempFile(@Nonnull String prefix, @Nonnull String suffix)
throws IOException {
File tmp = File.createTempFile(prefix, suffix);
tmp.deleteOnExit();
return tmp;
}
@Nonnull
public static File createTempDir() throws IOException {
try {
final File tmpDir = Files.createTempDir();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
deleteTempDir(tmpDir);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
});
return tmpDir;
} catch (IllegalStateException e) {
throw new IOException(e);
}
}
@Nonnull
public static File createDir(@Nonnull File directory, @Nonnull String name) throws IOException {
if (!directory.exists() || !directory.isDirectory()) {
throw new AssertionError();
}
File result = new File(directory, name);
if (!result.mkdir()) {
throw new IOException("Failed to create dir " + result.getAbsolutePath());
}
return result;
}
public static void deleteTempDir(@CheckForNull File tmp) throws IOException {
if (tmp == null) {
return;
}
if (tmp.isDirectory()) {
for (File sub : tmp.listFiles()) {
deleteTempDir(sub);
}
}
if (!tmp.delete()) {
throw new IOException("Failed to delete file " + tmp.getAbsolutePath());
}
}
public static void copyDirectory(@Nonnull File source, @Nonnull File dest) throws IOException {
if (!source.isDirectory() || !dest.isDirectory()) {
throw new AssertionError("Existing directories must be provided");
}
recursiveFileCopy(source, dest);
}
private static void recursiveFileCopy(@Nonnull File src, @Nonnull File dest)
throws IOException {
if (src.isDirectory()) {
if (!dest.exists() && !dest.mkdir()) {
throw new AssertionError("Unable to create directory '" + dest.getPath() + "'");
}
for (String file : src.list()) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
recursiveFileCopy(srcFile, destFile);
}
} else {
assert src.isFile();
Files.copy(src, dest);
}
}
@Nonnull
public static String getClasspathAsString(@Nonnull File[] files) {
if (files.length == 0) {
return "";
}
StringBuilder classpathStr = new StringBuilder();
for (int i = 0; i < files.length; i++) {
classpathStr.append(files[i].getAbsolutePath());
if (i != files.length - 1) {
classpathStr.append(File.pathSeparatorChar);
}
}
return classpathStr.toString();
}
@Nonnull
public static String getClasspathsAsString(
@Nonnull File[] bootClasspath, @Nonnull File[] classpath) {
if (bootClasspath.length == 0) {
return getClasspathAsString(classpath);
} else if (classpath.length == 0) {
return getClasspathAsString(bootClasspath);
} else {
return concatClasspathStrings(
getClasspathAsString(bootClasspath), getClasspathAsString(classpath));
}
}
@Nonnull
private static String concatClasspathStrings(
@Nonnull String bootclasspath, @Nonnull String classpath) {
if (bootclasspath.isEmpty()) {
return classpath;
} else if (classpath.isEmpty()) {
return bootclasspath;
} else {
StringBuilder classpathStr = new StringBuilder(bootclasspath);
classpathStr.append(File.pathSeparatorChar);
classpathStr.append(classpath);
return classpathStr.toString();
}
}
@Nonnull
public static File createFile(@Nonnull File folder, @Nonnull String packageName,
@Nonnull String fileName, @Nonnull String fileContent) throws IOException {
File packageFolder = new File(folder, packageName.replace('.', File.separatorChar));
if (!packageFolder.exists() && !packageFolder.mkdirs()) {
throw new IOException("Failed to create folder " + packageFolder.getAbsolutePath());
}
File javaFile = new File(packageFolder, fileName);
if (javaFile.exists() && !javaFile.delete()) {
throw new IOException("Failed to delete file " + javaFile.getAbsolutePath());
}
if (!javaFile.createNewFile()) {
throw new IOException("Failed to create file " + javaFile.getAbsolutePath());
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(javaFile);
fos.write(fileContent.getBytes());
} finally {
if (fos != null) {
fos.close();
}
}
return javaFile;
}
public static void deleteJavaFile(@Nonnull File folder, @Nonnull String packageName,
@Nonnull String fileName) throws IOException {
File packageFolder = new File(folder, NamingTools.getBinaryName(packageName));
File javaFile = new File(packageFolder, fileName);
deleteFile(javaFile);
}
public static void deleteFile(@Nonnull File file) throws IOException {
if (!file.delete()) {
throw new IOException("Failed to delete file " + file.getAbsolutePath());
}
}
@Nonnull
public static File getDir(@Nonnull File file) {
if (file.isDirectory()) {
return file;
} else {
return file.getParentFile();
}
}
@Nonnull
public static List<File> getFiles(@Nonnull File folder, @Nonnull String extension) {
assert folder.isDirectory();
List<File> jackFiles = new ArrayList<File>();
fillWithFiles(folder, jackFiles, extension);
return jackFiles;
}
private static void fillWithFiles(@Nonnull File file, @Nonnull List<File> jackFiles,
@Nonnull String extension) {
if (file.isDirectory()) {
for (File subFile : file.listFiles()) {
fillWithFiles(subFile, jackFiles, extension);
}
} else if (extension == null || extension.equals("*") || file.getName().endsWith(extension)) {
jackFiles.add(file);
}
}
public static void addFile(@Nonnull List<String> args,
boolean mustExist, @Nonnull File... filesOrSourceLists) {
for (File file : filesOrSourceLists) {
addFile(file, args, mustExist);
}
}
private static void addFile(@Nonnull File fileOrSourceList, @Nonnull List<String> args,
boolean mustExist) {
if (fileOrSourceList instanceof Sourcelist) {
args.add("@" + fileOrSourceList.getAbsolutePath());
} else {
List<File> sourceFiles = new ArrayList<File>();
try {
getJavaFiles(fileOrSourceList, sourceFiles, mustExist);
} catch (IOException e) {
}
for (File sourceFile : sourceFiles) {
args.add(sourceFile.getAbsolutePath());
}
}
}
public static void getJavaFiles(@Nonnull File fileObject, @Nonnull List<File> filePaths,
boolean mustExist) throws IOException {
if (fileObject.isDirectory()) {
File allFiles[] = fileObject.listFiles();
for (File aFile : allFiles) {
getJavaFiles(aFile, filePaths, mustExist);
}
} else if (fileObject.getName().endsWith(".java") && (!mustExist || fileObject.isFile())) {
filePaths.add(fileObject.getCanonicalFile());
}
}
public static void unzip(@Nonnull File jarfile, @Nonnull File outputFolder) {
String[] args = new String[] {"unzip", "-qo", jarfile.getAbsolutePath(), "-d",
outputFolder.getAbsolutePath()};
ExecuteFile execFile = new ExecuteFile(args);
try {
if (execFile.run() != 0) {
throw new RuntimeException("Unzip exited with an error");
}
} catch (ExecFileException e) {
throw new RuntimeException("An error occured while running unzip", e);
}
}
public static void zip(@Nonnull File directory, @Nonnull File outputFile) throws IOException {
String[] args = new String[] {"zip", "-r", outputFile.getAbsolutePath(), "."};
ExecuteFile execFile = new ExecuteFile(args);
execFile.setWorkingDir(directory, /* create = */ false);
execFile.setErr(System.err);
execFile.setOut(System.out);
try {
if (execFile.run() != 0) {
throw new RuntimeException("Zip exited with an error");
}
} catch (ExecFileException e) {
throw new RuntimeException("An error occured while running zip", e);
}
}
public static void createjar(@Nonnull File jarfile, @Nonnull File inputFiles) {
String[] args = new String[] {"jar",
"cf",
jarfile.getAbsolutePath(),
"-C",
inputFiles.getAbsolutePath(),
"."};
ExecuteFile execFile = new ExecuteFile(args);
try {
if (execFile.run() != 0) {
throw new RuntimeException("Jar exited with an error");
}
} catch (ExecFileException e) {
throw new RuntimeException("An error occured while running jar command", e);
}
}
@Nonnull
protected static List<String> buildEcjArgs() {
List<String> ecjArgs = new ArrayList<String>();
ecjArgs.add("-nowarn");
return ecjArgs;
}
@Nonnull
public static List<RuntimeRunner> listRuntimeTestRunners(@CheckForNull Properties properties)
throws SecurityException, IllegalArgumentException, RuntimeRunnerException {
if (properties != null) {
String rtAsString = properties.getProperty(RUNTIME_LIST_KEY);
if (rtAsString != null) {
return parseRuntimeList(rtAsString);
}
}
return runtimes;
}
@Nonnull
private static List<RuntimeRunner> parseRuntimeList(@CheckForNull String runtimeList)
throws SecurityException, IllegalArgumentException, RuntimeRunnerException {
List<RuntimeRunner> result = new ArrayList<RuntimeRunner>(0);
if (runtimeList != null) {
String[] rtList = runtimeList.split(PROPERTY_VALUE_SEPARATOR);
for (String rtName : rtList) {
result.add(RuntimeRunnerFactory.create(rtName));
}
}
return result;
}
@Nonnull
public static File getRuntimeEnvironmentRootDir(@Nonnull String rtName) {
String rtLocationPath = TestsProperties.getProperty(RUNTIME_LOCATION_PREFIX + rtName);
if (rtLocationPath == null) {
throw new TestConfigurationException(
"Location for runtime '" + rtName + "' is not specified. Set property '"
+ RUNTIME_LOCATION_PREFIX + rtName + "'");
}
File rtLocation = new File(rtLocationPath);
if (!rtLocation.exists()) {
throw new TestConfigurationException(
"Location for runtime " + rtName + " does not exist: '" + rtLocationPath + "'");
}
if (!rtLocation.isDirectory()) {
throw new TestConfigurationException(
"Location for runtime " + rtName + " is not a directory: '" + rtLocationPath + "'");
}
return rtLocation;
}
}
|