Home | History | Annotate | Download | only in command
      1 /*
      2  * Copyright (C) 2015 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.command;
     18 
     19 import com.android.tradefed.config.ConfigurationException;
     20 import com.android.tradefed.config.OptionSetter;
     21 import com.android.tradefed.util.FileUtil;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 import java.io.InputStream;
     28 
     29 /**
     30  * Unit tests for {@link Verify}
     31  */
     32 public class VerifyTest extends TestCase {
     33     private static final String TEST_CMD_FILE_PATH = "/testCmdFiles";
     34     private final Verify mVerify;
     35 
     36     public VerifyTest() throws ConfigurationException {
     37         mVerify = new Verify();
     38 
     39         OptionSetter option = new OptionSetter(mVerify);
     40         option.setOptionValue("quiet", "true");
     41     }
     42 
     43     /**
     44      * Extract an embedded command file into a temporary file, which we can feed to the
     45      * CommandFileParser
     46      */
     47     private File extractTestCmdFile(String name) throws IOException {
     48         final InputStream cmdFileStream = getClass().getResourceAsStream(
     49                 String.format("%s/%s.txt", TEST_CMD_FILE_PATH, name));
     50         final String tmpFileName = String.format("VerifyTest_%s_", name);
     51         File tmpFile = FileUtil.createTempFile(tmpFileName, ".txt");
     52         try {
     53             FileUtil.writeToFile(cmdFileStream, tmpFile);
     54         } catch (Throwable t) {
     55             // Clean up tmpFile, if it was created.
     56             FileUtil.deleteFile(tmpFile);
     57             throw t;
     58         }
     59 
     60         return tmpFile;
     61     }
     62 
     63     /**
     64      * Assert that the specified command file parses correctly, and clean up any temporary files
     65      */
     66     private void assertGoodCmdFile(String name) throws IOException {
     67         File cmdFile = extractTestCmdFile(name);
     68         try {
     69             assertTrue(mVerify.runVerify(cmdFile));
     70         } finally {
     71             FileUtil.deleteFile(cmdFile);
     72         }
     73     }
     74 
     75     /**
     76      * Assert that the specified command file does not parse correctly, and clean up any temporary
     77      * files
     78      */
     79     private void assertBadCmdFile(String name) throws IOException {
     80         File cmdFile = extractTestCmdFile(name);
     81         try {
     82             assertFalse(mVerify.runVerify(cmdFile));
     83         } finally {
     84             FileUtil.deleteFile(cmdFile);
     85         }
     86     }
     87 
     88     public void testBasic() throws IOException {
     89         assertGoodCmdFile("basic");
     90     }
     91 
     92     public void testMissingMacroDef() throws IOException {
     93         assertBadCmdFile("missing-macro-def");
     94     }
     95 
     96     public void testMissingBeginMacro() throws IOException {
     97         assertBadCmdFile("missing-begin-macro");
     98     }
     99 
    100     public void testMissingEndMacro() throws IOException {
    101         assertBadCmdFile("missing-end-macro");
    102     }
    103 }
    104