diff options
-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()); + } } } |