Home | History | Annotate | Download | only in testtype
      1 /*
      2  * Copyright (C) 2016 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 package com.android.tradefed.testtype;
     17 
     18 import com.android.ddmlib.Log;
     19 import com.android.tradefed.result.ITestLifeCycleReceiver;
     20 import com.android.tradefed.result.TestDescription;
     21 
     22 import junit.framework.TestCase;
     23 
     24 import org.easymock.EasyMock;
     25 
     26 import java.io.BufferedReader;
     27 import java.io.File;
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.io.InputStreamReader;
     31 import java.util.Map;
     32 import java.util.Vector;
     33 
     34 
     35 /**
     36  * Unit tests for {@link VtsFuzzTestResultParserTest}.
     37  */
     38 public class VtsFuzzTestResultParserTest extends TestCase {
     39     private static final String TEST_TYPE_DIR = "testtype";
     40     private static final String TEST_MODULE_NAME = "module";
     41     private static final String VTS_FUZZ_OUTPUT_FILE_1 = "vts_fuzz_output1.txt";
     42     private static final String VTS_FUZZ_OUTPUT_FILE_2 = "vts_fuzz_output2.txt";
     43     private static final String LOG_TAG = "VtsFuzzTestResultParserTest";
     44 
     45     /**
     46      * Helper to read a file from the res/testtype directory and return its contents as a String[]
     47      *
     48      * @param filename the name of the file (without the extension) in the res/testtype directory
     49      * @return a String[] of the
     50      */
     51     private String[] readInFile(String filename) {
     52         Vector<String> fileContents = new Vector<String>();
     53         try {
     54             InputStream gtestResultStream1 = getClass().getResourceAsStream(File.separator +
     55                     TEST_TYPE_DIR + File.separator + filename);
     56             BufferedReader reader = new BufferedReader(new InputStreamReader(gtestResultStream1));
     57             String line = null;
     58             while ((line = reader.readLine()) != null) {
     59                 fileContents.add(line);
     60             }
     61         }
     62         catch (NullPointerException e) {
     63             Log.e(LOG_TAG, "Output file does not exist: " + filename);
     64         }
     65         catch (IOException e) {
     66             Log.e(LOG_TAG, "Unable to read contents of an output file: " + filename);
     67         }
     68         return fileContents.toArray(new String[fileContents.size()]);
     69     }
     70 
     71     /**
     72      * Tests the parser for a passed test run output with 1 test.
     73      */
     74     @SuppressWarnings("unchecked")
     75     public void testParsePassedFile() throws Exception {
     76         String[] contents = readInFile(VTS_FUZZ_OUTPUT_FILE_1);
     77         ITestLifeCycleReceiver mockRunListener = EasyMock.createMock(ITestLifeCycleReceiver.class);
     78         mockRunListener.testRunStarted(TEST_MODULE_NAME, 1);
     79         mockRunListener.testEnded(
     80                 (TestDescription) EasyMock.anyObject(), (Map<String, String>) EasyMock.anyObject());
     81         EasyMock.replay(mockRunListener);
     82         VtsFuzzTestResultParser resultParser =
     83                 new VtsFuzzTestResultParser(TEST_MODULE_NAME, mockRunListener);
     84         resultParser.processNewLines(contents);
     85         resultParser.done();
     86     }
     87 
     88     /**
     89      * Tests the parser for a failed test run output with 1 test.
     90      */
     91     public void testParseFailedFile() throws Exception {
     92         String[] contents =  readInFile(VTS_FUZZ_OUTPUT_FILE_2);
     93         ITestLifeCycleReceiver mockRunListener = EasyMock.createMock(ITestLifeCycleReceiver.class);
     94         mockRunListener.testRunStarted(TEST_MODULE_NAME, 1);
     95         mockRunListener.testFailed(
     96                 (TestDescription) EasyMock.anyObject(), (String) EasyMock.anyObject());
     97         mockRunListener.testEnded(
     98                 (TestDescription) EasyMock.anyObject(), (Map<String, String>) EasyMock.anyObject());
     99         mockRunListener.testRunFailed((String)EasyMock.anyObject());
    100         EasyMock.replay(mockRunListener);
    101         VtsFuzzTestResultParser resultParser = new VtsFuzzTestResultParser(
    102             TEST_MODULE_NAME, mockRunListener);
    103         resultParser.processNewLines(contents);
    104         resultParser.done();
    105     }
    106 }
    107