Home | History | Annotate | Download | only in dex
      1 /*
      2  * Copyright (C) 2009 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 signature.converter.dex;
     18 
     19 
     20 import dex.reader.DexTestsCommon;
     21 import dex.reader.util.JavaSource;
     22 import dex.reader.util.JavaSourceToDexUtil;
     23 import dex.structure.DexClass;
     24 import dex.structure.DexFile;
     25 
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 
     29 import signature.converter.dex.DexToSigConverter;
     30 import signature.model.impl.SigPackage;
     31 import static signature.converter.dex.DexUtil.*;
     32 
     33 import java.io.IOException;
     34 
     35 import static org.junit.Assert.*;
     36 
     37 public class DexUtilTest extends DexTestsCommon {
     38 
     39     private JavaSourceToDexUtil dexUtil;
     40     private DexToSigConverter converter;
     41 
     42     @Before
     43     public void setupDexUtil(){
     44         dexUtil = new JavaSourceToDexUtil();
     45         converter = new DexToSigConverter();
     46     }
     47 
     48     @Test
     49     public void convertPackageTest(){
     50         SigPackage aPackage = converter.convertPackage("a");
     51         assertEquals("a", aPackage.getName());
     52 
     53         aPackage = converter.convertPackage("a.b.c");
     54         assertEquals("a.b.c", aPackage.getName());
     55     }
     56 
     57     @Test
     58     public void getPackageNameTest(){
     59         assertEquals("",getPackageName("LA;"));
     60         assertEquals("a",getPackageName("La/A;"));
     61         assertEquals("a.b.c",getPackageName("La/b/c/A;"));
     62     }
     63 
     64     @Test
     65     public void getClassNameTest(){
     66         assertEquals("A",getClassName("LA;"));
     67         assertEquals("A",getClassName("La/A;"));
     68         assertEquals("A",getClassName("La/b/c/A;"));
     69     }
     70 
     71     @Test
     72     public void hasGenericSignatureTest() throws IOException {
     73         DexFile dexFile = dexUtil.getFrom(new JavaSource("A", "public class A{}"));
     74         DexClass dexClass = getClass(dexFile, "LA;");
     75         assertFalse(hasGenericSignature(dexClass));
     76 
     77         dexFile = dexUtil.getFrom(new JavaSource("B", "public class B<T>{}"));
     78         dexClass = getClass(dexFile, "LB;");
     79         assertTrue(hasGenericSignature(dexClass));
     80     }
     81 
     82     @Test
     83     public void getGenericSignatureTest() throws IOException {
     84         DexFile dexFile =  dexUtil.getFrom(new JavaSource("A", "public class A{}"));
     85         DexClass dexClass = getClass(dexFile, "LA;");
     86         assertNull(getGenericSignature(dexClass));
     87 
     88         dexFile =  dexUtil.getFrom(new JavaSource("B", "public class B<T>{}"));
     89         dexClass = getClass(dexFile, "LB;");
     90         assertEquals("<T:Ljava/lang/Object;>Ljava/lang/Object;", getGenericSignature(dexClass));
     91     }
     92 
     93 
     94 
     95 }
     96