Home | History | Annotate | Download | only in suite
      1 /*
      2  * Copyright (C) 2018 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.compatibility.common.tradefed.result.suite;
     17 
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import com.android.compatibility.common.util.ChecksumReporter.ChecksumValidationException;
     22 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     23 import com.android.tradefed.result.TestDescription;
     24 import com.android.tradefed.result.TestResult;
     25 import com.android.tradefed.result.TestRunResult;
     26 import com.android.tradefed.util.FileUtil;
     27 
     28 import org.junit.After;
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.junit.runners.JUnit4;
     33 
     34 import java.io.File;
     35 import java.util.ArrayList;
     36 import java.util.Collection;
     37 import java.util.HashMap;
     38 import java.util.Map.Entry;
     39 
     40 /**
     41  * Unit tests for {@link CertificationChecksumHelper}.
     42  */
     43 @RunWith(JUnit4.class)
     44 public class CertificationChecksumHelperTest {
     45     private final static String FINGERPRINT = "thisismyfingerprint";
     46     private File mWorkingDir;
     47     private File mFakeLogFile;
     48 
     49     @Before
     50     public void setUp() throws Exception {
     51         mWorkingDir = FileUtil.createTempDir("certification-tests");
     52         mFakeLogFile = FileUtil.createTempFile("fake-log-file", ".xml", mWorkingDir);
     53         FileUtil.writeToFile("Bunch of data to make the file unique", mFakeLogFile);
     54     }
     55 
     56     @After
     57     public void tearDown() {
     58         FileUtil.recursiveDelete(mWorkingDir);
     59     }
     60 
     61     /**
     62      * Validation that the checksum generated can be properly checked when re-reading the file.
     63      */
     64     @Test
     65     public void testCreateChecksum() throws ChecksumValidationException {
     66         Collection<TestRunResult> results = new ArrayList<>();
     67         TestRunResult run1 = createFakeResults("run1", 2);
     68         results.add(run1);
     69         TestRunResult run2 = createFakeResults("run2", 3);
     70         results.add(run2);
     71         boolean res = CertificationChecksumHelper.tryCreateChecksum(
     72                 mWorkingDir, results, FINGERPRINT);
     73         assertTrue(res);
     74         // Attempt to parse the results back
     75         File checksum = new File(mWorkingDir, CertificationChecksumHelper.NAME);
     76         assertTrue(checksum.exists());
     77         CertificationChecksumHelper parser = new CertificationChecksumHelper(
     78                 mWorkingDir, FINGERPRINT);
     79         // Assert that the info are checkable
     80         assertTrue(parser.containsFile(mFakeLogFile, mWorkingDir.getName()));
     81         // Check run1
     82         for (Entry<TestDescription, TestResult> entry : run1.getTestResults().entrySet()) {
     83             assertTrue(parser.containsTestResult(entry, run1, FINGERPRINT));
     84         }
     85         // Check run2
     86         for (Entry<TestDescription, TestResult> entry : run2.getTestResults().entrySet()) {
     87             assertTrue(parser.containsTestResult(entry, run2, FINGERPRINT));
     88         }
     89     }
     90 
     91     private TestRunResult createFakeResults(String runName, int testCount) {
     92         TestRunResult results = new TestRunResult();
     93         results.testRunStarted(runName, testCount);
     94         for (int i = 0; i < testCount; i++) {
     95             TestDescription test = new TestDescription("com.class.path", "testMethod" + i);
     96             results.testStarted(test);
     97             results.testEnded(test, new HashMap<String, Metric>());
     98         }
     99         results.testRunEnded(500L, new HashMap<String, Metric>());
    100         return results;
    101     }
    102 }
    103