diff options
author | Elliott Hughes <enh@google.com> | 2009-12-15 16:27:08 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2009-12-15 16:27:08 -0800 |
commit | 6b5a19e2208499aab409be48be0b739b5878a1c1 (patch) | |
tree | dd800c209988f49797a8a30f0b4becc463d2b76e | |
parent | ace9bc2946ec5b57af40d5e957831a1a16ca3b62 (diff) | |
download | libcore-6b5a19e2208499aab409be48be0b739b5878a1c1.zip libcore-6b5a19e2208499aab409be48be0b739b5878a1c1.tar.gz libcore-6b5a19e2208499aab409be48be0b739b5878a1c1.tar.bz2 |
Fix our test runner to look inside source files for package declarations.
The relevant libcore .java files were all under test/java/ directories, but
that's not generally true, and not true in the specific case of caliper's
benchmarks.
-rw-r--r-- | tools/dalvik_jtreg/java/dalvik/jtreg/Strings.java | 18 | ||||
-rw-r--r-- | tools/dalvik_jtreg/java/dalvik/jtreg/TestFinder.java | 24 |
2 files changed, 40 insertions, 2 deletions
diff --git a/tools/dalvik_jtreg/java/dalvik/jtreg/Strings.java b/tools/dalvik_jtreg/java/dalvik/jtreg/Strings.java index 9868a93..4719d08 100644 --- a/tools/dalvik_jtreg/java/dalvik/jtreg/Strings.java +++ b/tools/dalvik_jtreg/java/dalvik/jtreg/Strings.java @@ -17,6 +17,11 @@ package dalvik.jtreg; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; @@ -26,6 +31,19 @@ import java.util.Iterator; */ public class Strings { + static String readFile(File f) throws IOException { + StringBuilder result = new StringBuilder(); + BufferedReader in = + new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8")); + String line; + while ((line = in.readLine()) != null) { + result.append(line); + result.append('\n'); + } + in.close(); + return result.toString(); + } + static String join(Object[] objects, String delimiter) { return join(Arrays.asList(objects), delimiter); } diff --git a/tools/dalvik_jtreg/java/dalvik/jtreg/TestFinder.java b/tools/dalvik_jtreg/java/dalvik/jtreg/TestFinder.java index 03c24be..9147ddc 100644 --- a/tools/dalvik_jtreg/java/dalvik/jtreg/TestFinder.java +++ b/tools/dalvik_jtreg/java/dalvik/jtreg/TestFinder.java @@ -17,8 +17,11 @@ package dalvik.jtreg; import java.io.File; +import java.io.IOException; import java.util.LinkedHashSet; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * A pluggable strategy for converting files into test runs. @@ -73,7 +76,24 @@ abstract class TestFinder { throw new IllegalArgumentException("Not a .java file: " + file); } - String fqClass = filePath.replaceAll(".*/test/java/", ""); - return fqClass.replace('/', '.').substring(0, fqClass.length() - 5); + // We can get the unqualified class name from the path. + // It's the last element minus the trailing ".java". + String filename = file.getName(); + String className = filename.substring(0, filename.length() - 5); + + // For the package, the only foolproof way is to look for the package + // declaration inside the file. + try { + String content = Strings.readFile(file); + Pattern packagePattern = Pattern.compile("(?m)^\\s*package\\s+(\\S+)\\s*;"); + Matcher packageMatcher = packagePattern.matcher(content); + if (!packageMatcher.find()) { + throw new IllegalArgumentException("No package in '" + file + "'\n"+content); + } + String packageName = packageMatcher.group(1); + return packageName + "." + className; + } catch (IOException ex) { + throw new IllegalArgumentException("Couldn't read '" + file + "': " + ex.getMessage()); + } } } |