Home | History | Annotate | Download | only in runner
      1 /*
      2  * Copyright (C) 2012 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.test.runner;
     17 
     18 import android.content.Context;
     19 
     20 import com.android.test.InjectContext;
     21 import com.android.test.runner.ClassPathScanner.ExcludePackageNameFilter;
     22 import com.android.test.runner.ClassPathScanner.ExternalClassNameFilter;
     23 import com.android.test.runner.ClassPathScanner.InclusivePackageNameFilter;
     24 
     25 import dalvik.system.DexFile;
     26 
     27 import junit.framework.Assert;
     28 
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 
     32 import java.io.IOException;
     33 import java.util.Enumeration;
     34 import java.util.Set;
     35 import java.util.Vector;
     36 
     37 /**
     38  * Unit tests for {@link ClassPathScanner}.
     39  */
     40 public class ClassPathScannerTest {
     41 
     42     private ClassPathScanner mClassPathScanner;
     43     private Enumeration<String> mDexEntries;
     44 
     45     @InjectContext
     46     public Context mContext;
     47 
     48     @Before
     49     public void setUp() throws Exception {
     50         mClassPathScanner = new ClassPathScanner(mContext.getPackageCodePath()) {
     51             @Override
     52             Enumeration<String> getDexEntries(DexFile dexFile) {
     53                 return mDexEntries;
     54             }
     55         };
     56     }
     57 
     58     @Test
     59     public void externalClassNameFilter() throws IOException {
     60         mDexEntries = createEntries("com.example.MyName",
     61                 "com.example.MyName$Inner");
     62         Set<String> result = mClassPathScanner.getClassPathEntries(new ExternalClassNameFilter());
     63         Assert.assertEquals(1, result.size());
     64         Assert.assertTrue(result.contains("com.example.MyName"));
     65     }
     66 
     67     @Test
     68     public void inclusivePackageNameFilter() throws IOException {
     69         mDexEntries = createEntries("com.example.MyName",
     70                 "com.exclude.Excluded", "com.example2.MyName");
     71         Set<String> result = mClassPathScanner.getClassPathEntries(new InclusivePackageNameFilter(
     72                 "com.example"));
     73         Assert.assertEquals(1, result.size());
     74         Assert.assertTrue(result.contains("com.example.MyName"));
     75     }
     76 
     77     @Test
     78     public void exclusivePackageNameFilter() throws IOException {
     79         mDexEntries = createEntries("com.example.MyName",
     80                 "com.exclude.Excluded", "com.exclude2.Excluded");
     81         Set<String> result = mClassPathScanner.getClassPathEntries(new ExcludePackageNameFilter(
     82                 "com.exclude"));
     83         Assert.assertEquals(2, result.size());
     84         Assert.assertTrue(result.contains("com.example.MyName"));
     85         Assert.assertTrue(result.contains("com.exclude2.Excluded"));
     86     }
     87 
     88     private Enumeration<String> createEntries(String... entries) {
     89         Vector<String> v = new Vector<String>(entries.length);
     90         for (String entry : entries) {
     91             v.add(entry);
     92         }
     93         return v.elements();
     94     }
     95 }
     96