Home | History | Annotate | Download | only in converter
      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;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotNull;
     21 
     22 import java.io.IOException;
     23 import java.util.Set;
     24 
     25 import org.junit.Test;
     26 
     27 import signature.converter.Visibility;
     28 import signature.converter.util.AbstractConvertTest;
     29 import signature.converter.util.CompilationUnit;
     30 import signature.model.IApi;
     31 import signature.model.IClassDefinition;
     32 import signature.model.IMethod;
     33 import signature.model.IPackage;
     34 import signature.model.util.ModelUtil;
     35 
     36 public abstract class ConvertVisibilityTest extends AbstractConvertTest {
     37 
     38     @Test
     39     public void testVisibilityMethods1() throws IOException {
     40         CompilationUnit src = new CompilationUnit("a.A",
     41                 "package a;" +
     42                 "public class A{" +
     43                 "   public void foo1(){}" +
     44                 "   protected void foo2(){}" +
     45                 "   void foo3(){}" +
     46                 "   private void foo4(){}" +
     47                 "}");
     48 
     49         IApi api = convert(Visibility.PUBLIC, src);
     50         IPackage p = ModelUtil.getPackage(api, "a");
     51         IClassDefinition c = ModelUtil.getClass(p, "A");
     52         Set<IMethod> methods = c.getMethods();
     53         assertEquals(1, methods.size());
     54     }
     55 
     56     @Test
     57     public void testVisibilityMethods2() throws IOException {
     58         CompilationUnit src = new CompilationUnit("a.A",
     59                 "package a;" +
     60                 "public class A{" +
     61                 "   public void foo1(){}" +
     62                 "   protected void foo2(){}" +
     63                 "   void foo3(){}" +
     64                 "   private void foo4(){}" +
     65                 "}");
     66 
     67         IApi api = convert(Visibility.PROTECTED, src);
     68         IPackage p = ModelUtil.getPackage(api, "a");
     69         IClassDefinition c = ModelUtil.getClass(p, "A");
     70         Set<IMethod> methods = c.getMethods();
     71         assertEquals(2, methods.size());
     72     }
     73 
     74     @Test
     75     public void testVisibilityMethods3() throws IOException {
     76         CompilationUnit src = new CompilationUnit("a.A",
     77                 "package a;" +
     78                 "public class A{" +
     79                 "   public void foo1(){}" +
     80                 "   protected void foo2(){}" +
     81                 "   void foo3(){}" +
     82                 "   private void foo4(){}" +
     83                 "}");
     84 
     85         IApi api = convert(Visibility.PACKAGE, src);
     86         IPackage p = ModelUtil.getPackage(api, "a");
     87         IClassDefinition c = ModelUtil.getClass(p, "A");
     88         Set<IMethod> methods = c.getMethods();
     89         assertEquals(3, methods.size());
     90     }
     91 
     92     @Test
     93     public void testVisibilityMethods4() throws IOException {
     94         CompilationUnit src = new CompilationUnit("a.A",
     95                 "package a;" +
     96                 "public class A{" +
     97                 "   public void foo1(){}" +
     98                 "   protected void foo2(){}" +
     99                 "   void foo3(){}" +
    100                 "   private void foo4(){}" +
    101                 "}");
    102 
    103         IApi api = convert(Visibility.PRIVATE, src);
    104         IPackage p = ModelUtil.getPackage(api, "a");
    105         IClassDefinition c = ModelUtil.getClass(p, "A");
    106         Set<IMethod> methods = c.getMethods();
    107         assertEquals(4, methods.size());
    108     }
    109 
    110     @Test
    111     public void testVisibility1() throws IOException {
    112         CompilationUnit src = new CompilationUnit("a.X1",
    113                 "package a;" +
    114                 "public class X1{" +
    115                 "   static class X2{}" +
    116                 "   protected static class X3{}" +
    117                 "   private static class X4{}" +
    118                 "}");
    119 
    120         IApi api = convert(Visibility.PUBLIC, src);
    121         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    122         assertEquals(1, sigPackage.getClasses().size());
    123     }
    124 
    125     @Test
    126     public void testVisibility2() throws IOException {
    127         CompilationUnit src = new CompilationUnit("a.X1",
    128                 "package a;" +
    129                 "public class X1{" +
    130                 "   static class X2{}" +
    131                 "   protected static class X3{}" +
    132                 "   private static class X4{}" +
    133                 "}");
    134 
    135         IApi api = convert(Visibility.PROTECTED, src);
    136         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    137         assertEquals(2, sigPackage.getClasses().size());
    138         assertNotNull(ModelUtil.getClass(sigPackage, "X1.X3"));
    139     }
    140 
    141     @Test
    142     public void testVisibility3() throws IOException {
    143         CompilationUnit src = new CompilationUnit("a.X1",
    144                 "package a;" +
    145                 "public class X1{" +
    146                 "   static class X2{}" +
    147                 "   protected static class X3{}" +
    148                 "   private static class X4{}" +
    149                 "}");
    150 
    151         IApi api = convert(Visibility.PACKAGE, src);
    152         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    153         assertEquals(3, sigPackage.getClasses().size());
    154         assertNotNull(ModelUtil.getClass(sigPackage, "X1.X2"));
    155     }
    156 
    157     @Test
    158     public void testVisibility4() throws IOException {
    159         CompilationUnit src = new CompilationUnit("a.X1",
    160                 "package a;" +
    161                 "public class X1{" +
    162                 "   static class X2{}" +
    163                 "   protected static class X3{}" +
    164                 "   private static class X4{}" +
    165                 "}");
    166 
    167         IApi api = convert(Visibility.PRIVATE, src);
    168         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    169         assertEquals(4, sigPackage.getClasses().size());
    170         assertNotNull(ModelUtil.getClass(sigPackage, "X1.X4"));
    171     }
    172 
    173 }