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.util.Collections;
20 import java.util.LinkedHashSet;
21 import java.util.Set;
22 import java.util.regex.Pattern;
23 
24 /**
25  * The expected result of an action execution. This is typically encoded in the
26  * expectations text file, which has the following format:
27  * <pre>
28  * test java.io.StreamTokenizer.Reset
29  * result UNSUPPORTED
30  * pattern .*should get token \[, but get -1.*
31  *
32  * # should we fix this?
33  * test java.util.Arrays.CopyMethods
34  * result COMPILE_FAILED
35  * pattern .*cannot find symbol.*
36  * </pre>
37  */
38 final class Expectation {
39 
40     /** The pattern to use when no expected output is specified */
41     public static final Pattern MATCH_ALL_PATTERN
42             = Pattern.compile(".*", Pattern.MULTILINE | Pattern.DOTALL);
43 
44     /** The expectation of a general successful run. */
45     static final Expectation SUCCESS = new Expectation(Result.SUCCESS, MATCH_ALL_PATTERN,
46             Collections.<String>emptySet(), "", -1, false);
47 
48     /** Justification for this expectation */
49     private final String description;
50 
51     /** The action's expected result, such as {@code EXEC_FAILED}. */
52     private final Result result;
53 
54     /** The pattern the expected output will match. */
55     private final Pattern pattern;
56 
57     /** Attributes of this test. */
58     private final Set<String> tags;
59 
60     /** The tracking bug ID */
61     private final long bug;
62 
63     /** True if the identified bug still active. */
64     private boolean bugIsOpen = false;
65 
66     /** True if the expectation was read from an expectation file */
67     private boolean isFromExpectationFile = false;
68 
Expectation(Result result, Pattern pattern, Set<String> tags, String description, long bug, boolean fromExpectationFile)69     public Expectation(Result result,
70                        Pattern pattern,
71                        Set<String> tags,
72                        String description,
73                        long bug,
74                        boolean fromExpectationFile) {
75         if (result == null || description == null || pattern == null) {
76             throw new IllegalArgumentException(
77                     "result=" + result + " description=" + description + " pattern=" + pattern);
78         }
79 
80         this.description = description;
81         this.result = result;
82         this.pattern = pattern;
83         this.tags = new LinkedHashSet<String>(tags);
84         this.bug = bug;
85         this.isFromExpectationFile = fromExpectationFile;
86     }
87 
getDescription()88     public String getDescription() {
89         return description;
90     }
91 
getBug()92     public long getBug() {
93         return bug;
94     }
95 
getResult()96     public Result getResult() {
97         return result;
98     }
99 
getTags()100     public Set<String> getTags() {
101         return tags;
102     }
103 
getIsFromExpectationFile()104     public boolean getIsFromExpectationFile() {
105         return isFromExpectationFile;
106     }
107 
108     /**
109      * Set the current status of this expectation's bug. When a bug is open,
110      * any result (success or failure) is permitted.
111      */
setBugIsOpen(boolean bugIsOpen)112     public void setBugIsOpen(boolean bugIsOpen) {
113         this.bugIsOpen = bugIsOpen;
114     }
115 
116     /**
117      * Returns true if {@code outcome} matches this expectation.
118      */
matches(Outcome outcome)119     public boolean matches(Outcome outcome) {
120         return patternMatches(outcome) && (bugIsOpen || result == outcome.getResult());
121     }
122 
patternMatches(Outcome outcome)123     private boolean patternMatches(Outcome outcome) {
124         return pattern.matcher(outcome.getOutput()).matches();
125     }
126 
toString()127     @Override public String toString() {
128         return "Expectation[description=" + description + " pattern=" + pattern.pattern() + "]";
129     }
130 }
131