Home | History | Annotate | Download | only in suite
      1 /*
      2  * Copyright (C) 2017 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.suite;
     17 
     18 import static org.junit.Assert.*;
     19 
     20 import com.android.tradefed.invoker.shard.StrictShardHelperTest.SplitITestSuite;
     21 import com.android.tradefed.testtype.IRemoteTest;
     22 
     23 import org.junit.Test;
     24 import org.junit.runner.RunWith;
     25 import org.junit.runners.JUnit4;
     26 
     27 import java.util.Collection;
     28 import java.util.Iterator;
     29 
     30 /** Unit tests for {@link ModuleMerger}. */
     31 @RunWith(JUnit4.class)
     32 public class ModuleMergerTest {
     33 
     34     /**
     35      * Test that {@link ModuleMerger#arePartOfSameSuite(ITestSuite, ITestSuite)} returns false when
     36      * the first suite is not splitted yet.
     37      */
     38     @Test
     39     public void testPartOfSameSuite_notSplittedYet() {
     40         SplitITestSuite suite1 = new SplitITestSuite("module1");
     41         SplitITestSuite suite2 = new SplitITestSuite("module2");
     42         assertFalse(ModuleMerger.arePartOfSameSuite(suite1, suite2));
     43     }
     44 
     45     /**
     46      * Test that {@link ModuleMerger#arePartOfSameSuite(ITestSuite, ITestSuite)} returns false when
     47      * the second suite is not splitted yet.
     48      */
     49     @Test
     50     public void testPartOfSameSuite_notSplittedYet2() {
     51         SplitITestSuite suite1 = new SplitITestSuite("module1");
     52         Collection<IRemoteTest> res1 = suite1.split(2);
     53         SplitITestSuite suite2 = new SplitITestSuite("module2");
     54         assertFalse(ModuleMerger.arePartOfSameSuite((ITestSuite) res1.iterator().next(), suite2));
     55     }
     56 
     57     /**
     58      * Test that {@link ModuleMerger#arePartOfSameSuite(ITestSuite, ITestSuite)} returns true when
     59      * the two suites are splitted and from the same module.
     60      */
     61     @Test
     62     public void testPartOfSameSuite_sameSuite() {
     63         SplitITestSuite suite1 = new SplitITestSuite("module1");
     64         Collection<IRemoteTest> res1 = suite1.split(2);
     65         Iterator<IRemoteTest> ite = res1.iterator();
     66         assertTrue(
     67                 ModuleMerger.arePartOfSameSuite((ITestSuite) ite.next(), (ITestSuite) ite.next()));
     68     }
     69 
     70     /**
     71      * Test that {@link ModuleMerger#arePartOfSameSuite(ITestSuite, ITestSuite)} returns false when
     72      * the two suites are splitted but from different modules.
     73      */
     74     @Test
     75     public void testPartOfSameSuite_notSameSuite() {
     76         SplitITestSuite suite1 = new SplitITestSuite("module1");
     77         Collection<IRemoteTest> res1 = suite1.split(2);
     78         SplitITestSuite suite2 = new SplitITestSuite("module2");
     79         Collection<IRemoteTest> res2 = suite2.split(2);
     80         assertFalse(
     81                 ModuleMerger.arePartOfSameSuite(
     82                         (ITestSuite) res1.iterator().next(), (ITestSuite) res2.iterator().next()));
     83     }
     84 
     85     /**
     86      * Test that {@link ModuleMerger#mergeSplittedITestSuite(ITestSuite, ITestSuite)} throws an
     87      * exception when the first suite is not splitted yet.
     88      */
     89     @Test
     90     public void testMergeSplittedITestSuite_notSplittedYet() {
     91         SplitITestSuite suite1 = new SplitITestSuite("module1");
     92         SplitITestSuite suite2 = new SplitITestSuite("module2");
     93         try {
     94             ModuleMerger.mergeSplittedITestSuite(suite1, suite2);
     95             fail("Should have thrown an exception.");
     96         } catch (IllegalArgumentException expected) {
     97             // expected
     98         }
     99     }
    100 
    101     /**
    102      * Test that {@link ModuleMerger#mergeSplittedITestSuite(ITestSuite, ITestSuite)} throws an
    103      * exception when the second suite is not splitted yet.
    104      */
    105     @Test
    106     public void testMergeSplittedITestSuite_notSplittedYet2() {
    107         SplitITestSuite suite1 = new SplitITestSuite("module1");
    108         Collection<IRemoteTest> res1 = suite1.split(2);
    109         SplitITestSuite suite2 = new SplitITestSuite("module2");
    110         try {
    111             ModuleMerger.mergeSplittedITestSuite((ITestSuite) res1.iterator().next(), suite2);
    112             fail("Should have thrown an exception.");
    113         } catch (IllegalArgumentException expected) {
    114             // expected
    115         }
    116     }
    117 
    118     /**
    119      * Test that {@link ModuleMerger#mergeSplittedITestSuite(ITestSuite, ITestSuite)} throws an
    120      * exception when the two suites are splitted but coming from different modules.
    121      */
    122     @Test
    123     public void testMergeSplittedITestSuite_splittedSuiteFromDifferentModules() {
    124         SplitITestSuite suite1 = new SplitITestSuite("module1");
    125         Collection<IRemoteTest> res1 = suite1.split(2);
    126         SplitITestSuite suite2 = new SplitITestSuite("module2");
    127         Collection<IRemoteTest> res2 = suite2.split(2);
    128         try {
    129             ModuleMerger.mergeSplittedITestSuite(
    130                     (ITestSuite) res1.iterator().next(), (ITestSuite) res2.iterator().next());
    131             fail("Should have thrown an exception.");
    132         } catch (IllegalArgumentException expected) {
    133             // expected
    134         }
    135     }
    136 
    137     /**
    138      * Test that {@link ModuleMerger#mergeSplittedITestSuite(ITestSuite, ITestSuite)} properly
    139      * assigns tests from the second suite to the first since part of same module.
    140      */
    141     @Test
    142     public void testMergeSplittedITestSuite() {
    143         SplitITestSuite suite1 = new SplitITestSuite("module1");
    144         Collection<IRemoteTest> res1 = suite1.split(2);
    145         Iterator<IRemoteTest> ite = res1.iterator();
    146         ITestSuite split1 = (ITestSuite) ite.next();
    147         ITestSuite split2 = (ITestSuite) ite.next();
    148         assertEquals(2, split1.getDirectModule().numTests());
    149         assertEquals(2, split2.getDirectModule().numTests());
    150         ModuleMerger.mergeSplittedITestSuite(split1, split2);
    151         assertEquals(4, split1.getDirectModule().numTests());
    152     }
    153 }
    154