Home | History | Annotate | Download | only in testtype
      1 /*
      2  * Copyright (C) 2013 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 com.android.tradefed.testtype;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.fail;
     21 
     22 import com.android.tradefed.config.OptionSetter;
     23 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     24 import com.android.tradefed.result.ITestInvocationListener;
     25 import com.android.tradefed.result.TestDescription;
     26 
     27 import org.easymock.EasyMock;
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 import org.junit.runners.JUnit4;
     32 
     33 import java.util.HashMap;
     34 import java.util.Map;
     35 
     36 @RunWith(JUnit4.class)
     37 public class FakeTestTest {
     38 
     39     private FakeTest mTest = null;
     40     private ITestInvocationListener mListener = null;
     41     private OptionSetter mOption = null;
     42 
     43     @Before
     44     public void setUp() throws Exception {
     45         mTest = new FakeTest();
     46         mOption = new OptionSetter(mTest);
     47         mListener = EasyMock.createStrictMock(ITestInvocationListener.class);
     48     }
     49 
     50     @Test
     51     public void testIntOrDefault() throws IllegalArgumentException {
     52         assertEquals(55, mTest.toIntOrDefault(null, 55));
     53         assertEquals(45, mTest.toIntOrDefault("45", 55));
     54         assertEquals(-13, mTest.toIntOrDefault("-13", 55));
     55         try {
     56             mTest.toIntOrDefault("thirteen", 55);
     57             fail("No IllegalArgumentException thrown for input \"thirteen\"");
     58         } catch (IllegalArgumentException e) {
     59             // expected
     60         }
     61     }
     62 
     63     @Test
     64     public void testDecodeRle() throws IllegalArgumentException {
     65         // testcases: input -> output
     66         final Map<String, String> t = new HashMap<String, String>();
     67         t.put("PFE", "PFE");
     68         t.put("P1F1E1", "PFE");
     69         t.put("P2F2E2", "PPFFEE");
     70         t.put("P3F2E1", "PPPFFE");
     71         t.put("", "");
     72         for (Map.Entry<String, String> testcase : t.entrySet()) {
     73             final String input = testcase.getKey();
     74             final String output = testcase.getValue();
     75             assertEquals(output, mTest.decodeRle(input));
     76         }
     77 
     78         final String[] errors = {"X", "X1", "P0", "P2F1E0", "2PF", "2PF1", " "};
     79         for (String error : errors) {
     80             try {
     81                 mTest.decodeRle(error);
     82                 fail(String.format("No IllegalArgumentException thrown for input \"%s\"", error));
     83             } catch (IllegalArgumentException e) {
     84                 // expected
     85             }
     86         }
     87     }
     88 
     89     @Test
     90     public void testDecode() throws IllegalArgumentException {
     91         // testcases: input -> output
     92         final Map<String, String> t = new HashMap<String, String>();
     93         // Valid input for decodeRle should be valid for decode
     94         t.put("PFE", "PFE");
     95         t.put("P1F1E1", "PFE");
     96         t.put("P2F2E2", "PPFFEE");
     97         t.put("P3F2E1", "PPPFFE");
     98         t.put("", "");
     99 
    100         // decode should also handle parens and lowercase input
    101         t.put("(PFE)", "PFE");
    102         t.put("pfe", "PFE");
    103         t.put("(PFE)2", "PFEPFE");
    104         t.put("((PF)2)2E3", "PFPFPFPFEEE");
    105         t.put("()PFE", "PFE");
    106         t.put("PFE()", "PFE");
    107         t.put("(PF)(FE)", "PFFE");
    108         t.put("(PF()FE)", "PFFE");
    109 
    110         for (Map.Entry<String, String> testcase : t.entrySet()) {
    111             final String input = testcase.getKey();
    112             final String output = testcase.getValue();
    113             try {
    114                 assertEquals(output, mTest.decode(input));
    115             } catch (IllegalArgumentException e) {
    116                 // Add input to error message to make debugging easier
    117                 final Exception cause = new IllegalArgumentException(String.format(
    118                         "Encountered exception for input \"%s\" with expected output \"%s\"",
    119                         input, output));
    120                 throw new IllegalArgumentException(e.getMessage(), cause);
    121             }
    122         }
    123 
    124         final String[] errors = {
    125                 "X", "X1", "P0", "P2F1E0", "2PF", "2PF1", " ",  // common decodeRle errors
    126                 "(", "(PFE()", "(PFE))", "2(PFE)", "2(PFE)2", "(PF))((FE)", "(PFE)0"};
    127         for (String error : errors) {
    128             try {
    129                 mTest.decode(error);
    130                 fail(String.format("No IllegalArgumentException thrown for input \"%s\"", error));
    131             } catch (IllegalArgumentException e) {
    132                 // expected
    133             }
    134         }
    135     }
    136 
    137     @Test
    138     public void testRun_empty() throws Exception {
    139         final String name = "com.moo.cow";
    140         mListener.testRunStarted(EasyMock.eq(name), EasyMock.eq(0));
    141         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    142 
    143         EasyMock.replay(mListener);
    144         mOption.setOptionValue("run", name, "");
    145         mTest.run(mListener);
    146         EasyMock.verify(mListener);
    147     }
    148 
    149     @Test
    150     public void testRun_simplePass() throws Exception {
    151         final String name = "com.moo.cow";
    152         mListener.testRunStarted(EasyMock.eq(name), EasyMock.eq(1));
    153         testPassExpectations(mListener, name, 1);
    154         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    155 
    156         EasyMock.replay(mListener);
    157         mOption.setOptionValue("run", name, "P");
    158         mTest.run(mListener);
    159         EasyMock.verify(mListener);
    160     }
    161 
    162     @Test
    163     public void testRun_simpleFail() throws Exception {
    164         final String name = "com.moo.cow";
    165         mListener.testRunStarted(EasyMock.eq(name), EasyMock.eq(1));
    166         testFailExpectations(mListener, name, 1);
    167         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    168 
    169         EasyMock.replay(mListener);
    170         mOption.setOptionValue("run", name, "F");
    171         mTest.run(mListener);
    172         EasyMock.verify(mListener);
    173     }
    174 
    175     @Test
    176     public void testRun_basicSequence() throws Exception {
    177         final String name = "com.moo.cow";
    178         int i = 1;
    179         mListener.testRunStarted(EasyMock.eq(name), EasyMock.eq(3));
    180         testPassExpectations(mListener, name, i++);
    181         testFailExpectations(mListener, name, i++);
    182         testPassExpectations(mListener, name, i++);
    183         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    184 
    185         EasyMock.replay(mListener);
    186         mOption.setOptionValue("run", name, "PFP");
    187         mTest.run(mListener);
    188         EasyMock.verify(mListener);
    189     }
    190 
    191     @Test
    192     public void testRun_basicParens() throws Exception {
    193         final String name = "com.moo.cow";
    194         int i = 1;
    195         mListener.testRunStarted(EasyMock.eq(name), EasyMock.eq(4));
    196         testPassExpectations(mListener, name, i++);
    197         testFailExpectations(mListener, name, i++);
    198         testPassExpectations(mListener, name, i++);
    199         testFailExpectations(mListener, name, i++);
    200         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    201 
    202         EasyMock.replay(mListener);
    203         mOption.setOptionValue("run", name, "(PF)2");
    204         mTest.run(mListener);
    205         EasyMock.verify(mListener);
    206     }
    207 
    208     @Test
    209     public void testRun_recursiveParens() throws Exception {
    210         final String name = "com.moo.cow";
    211         int i = 1;
    212         mListener.testRunStarted(EasyMock.eq(name), EasyMock.eq(8));
    213         testPassExpectations(mListener, name, i++);
    214         testFailExpectations(mListener, name, i++);
    215 
    216         testPassExpectations(mListener, name, i++);
    217         testFailExpectations(mListener, name, i++);
    218 
    219         testPassExpectations(mListener, name, i++);
    220         testFailExpectations(mListener, name, i++);
    221 
    222         testPassExpectations(mListener, name, i++);
    223         testFailExpectations(mListener, name, i++);
    224 
    225         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    226 
    227         EasyMock.replay(mListener);
    228         mOption.setOptionValue("run", name, "((PF)2)2");
    229         mTest.run(mListener);
    230         EasyMock.verify(mListener);
    231     }
    232 
    233     @Test
    234     public void testMultiRun() throws Exception {
    235         final String name1 = "com.moo.cow";
    236         int i = 1;
    237         mListener.testRunStarted(EasyMock.eq(name1), EasyMock.eq(2));
    238         testPassExpectations(mListener, name1, i++);
    239         testFailExpectations(mListener, name1, i++);
    240         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    241 
    242         final String name2 = "com.quack.duck";
    243         i = 1;
    244         mListener.testRunStarted(EasyMock.eq(name2), EasyMock.eq(2));
    245         testFailExpectations(mListener, name2, i++);
    246         testPassExpectations(mListener, name2, i++);
    247         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    248 
    249         final String name3 = "com.oink.pig";
    250         i = 1;
    251         mListener.testRunStarted(EasyMock.eq(name3), EasyMock.eq(0));
    252         // empty run
    253         mListener.testRunEnded(EasyMock.eq(0l), EasyMock.<HashMap<String, Metric>>anyObject());
    254 
    255         EasyMock.replay(mListener);
    256         mOption.setOptionValue("run", name1, "PF");
    257         mOption.setOptionValue("run", name2, "FP");
    258         mOption.setOptionValue("run", name3, "");
    259         mTest.run(mListener);
    260         EasyMock.verify(mListener);
    261     }
    262 
    263     private void testPassExpectations(ITestInvocationListener l, String klass,
    264             int idx) {
    265         final String name = String.format("testMethod%d", idx);
    266         final TestDescription test = new TestDescription(klass, name);
    267         l.testStarted(test);
    268         l.testEnded(EasyMock.eq(test), EasyMock.<HashMap<String, Metric>>anyObject());
    269     }
    270 
    271     private void testFailExpectations(ITestInvocationListener l, String klass,
    272             int idx) {
    273         final String name = String.format("testMethod%d", idx);
    274         final TestDescription test = new TestDescription(klass, name);
    275         l.testStarted(test);
    276         l.testFailed(EasyMock.eq(test), EasyMock.<String>anyObject());
    277         l.testEnded(EasyMock.eq(test), EasyMock.<HashMap<String, Metric>>anyObject());
    278     }
    279 }
    280 
    281