1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package vogar;
18 
19 import java.io.File;
20 
21 /**
22  * A named job such as a test or benchmark run. This class tracks the resource
23  * files and classes for compiling and running a Java source file.
24  */
25 public final class Action {
26 
27     private static final String TEST_ROOT = "/test/";
28 
29     private final String name;
30     private final String actionClass;
31     private final File resourcesDirectory;
32     private final File sourcePath;
33     private final File javaFile;
34     private File userDir = new File(System.getProperty("user.dir"));
35 
Action(String name, String actionClass, File resourcesDirectory, File sourcePath, File javaFile)36     public Action(String name, String actionClass, File resourcesDirectory,
37             File sourcePath, File javaFile) {
38         // Some part of the stack dies with hashes in file-names.
39         this.name = name.replaceAll("#", "_");
40         this.actionClass = actionClass;
41         this.resourcesDirectory = resourcesDirectory;
42         this.sourcePath = sourcePath;
43         this.javaFile = javaFile;
44     }
45 
46     /**
47      * Returns the local directory containing this action's required resource
48      * files, or {@code null} if this action is standalone.
49      */
getResourcesDirectory()50     public File getResourcesDirectory() {
51         return resourcesDirectory;
52     }
53 
54     /**
55      * Returns this action's source path, or {@code null} if this file wasn't
56      * built from source.
57      */
getSourcePath()58     public File getSourcePath() {
59         return sourcePath;
60     }
61 
62     /**
63      * Returns this action's java file, or {@code null} if this file wasn't
64      * built from source.
65      */
getJavaFile()66     public File getJavaFile() {
67         return javaFile;
68     }
69 
70     /**
71      * Returns the executable classname, such as java.lang.IntegerTest
72      * or BitTwiddle.
73      */
getTargetClass()74     public String getTargetClass() {
75         return actionClass;
76     }
77 
78     /**
79      * Returns a unique identifier for this action.
80      */
getName()81     public String getName() {
82         return name;
83     }
84 
85     /**
86      * Initializes the directory from which local files can be read by the
87      * action.
88      */
setUserDir(File base)89     public void setUserDir(File base) {
90         this.userDir = base;
91     }
92 
getUserDir()93     public File getUserDir() {
94         return userDir;
95     }
96 
toString()97     @Override public String toString() {
98         return name;
99     }
100 
101     /**
102      * Returns an action name appropriate for the given {@code .java} file
103      * without first reading the contents of the file.
104      */
nameForJavaFile(File javaFile)105     public static String nameForJavaFile(File javaFile) {
106         String path = javaFile.getAbsolutePath();
107         int indexOfTest = path.indexOf(TEST_ROOT);
108         path = (indexOfTest != -1)
109                 ? path.substring(indexOfTest + TEST_ROOT.length(), path.length() - ".java".length())
110                 : path.substring(1);
111         return path.replace(File.separatorChar, '.');
112     }
113 }
114