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