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.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 public 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     public static final Expectation SUCCESS = new Expectation(Result.SUCCESS, MATCH_ALL_PATTERN,
     46             Collections.<String>emptySet(), "", -1);
     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     public Expectation(Result result, Pattern pattern, Set<String> tags, String description, long bug) {
     67         if (result == null || description == null || pattern == null) {
     68             throw new IllegalArgumentException(
     69                     "result=" + result + " description=" + description + " pattern=" + pattern);
     70         }
     71 
     72         this.description = description;
     73         this.result = result;
     74         this.pattern = pattern;
     75         this.tags = new LinkedHashSet<String>(tags);
     76         this.bug = bug;
     77     }
     78 
     79     public String getDescription() {
     80         return description;
     81     }
     82 
     83     public long getBug() {
     84         return bug;
     85     }
     86 
     87     public Result getResult() {
     88         return result;
     89     }
     90 
     91     public Set<String> getTags() {
     92         return tags;
     93     }
     94 
     95     /**
     96      * Set the current status of this expectation's bug. When a bug is open,
     97      * any result (success or failure) is permitted.
     98      */
     99     public void setBugIsOpen(boolean bugIsOpen) {
    100         this.bugIsOpen = bugIsOpen;
    101     }
    102 
    103     /**
    104      * Returns true if {@code outcome} matches this expectation.
    105      */
    106     public boolean matches(Outcome outcome) {
    107         return patternMatches(outcome) && (bugIsOpen || result == outcome.getResult());
    108     }
    109 
    110     private boolean patternMatches(Outcome outcome) {
    111         return pattern.matcher(outcome.getOutput()).matches();
    112     }
    113 
    114     @Override public String toString() {
    115         return "Expectation[description=" + description + " pattern=" + pattern.pattern() + "]";
    116     }
    117 }
    118