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 import static org.junit.Assert.assertSame;
     22 import static org.junit.Assert.assertTrue;
     23 import static org.junit.Assert.assertFalse;
     24 
     25 import java.io.IOException;
     26 import java.util.HashMap;
     27 import java.util.HashSet;
     28 import java.util.Map;
     29 import java.util.Set;
     30 
     31 import org.junit.Test;
     32 
     33 import signature.converter.util.AbstractConvertTest;
     34 import signature.converter.util.CompilationUnit;
     35 import signature.model.IApi;
     36 import signature.model.IArrayType;
     37 import signature.model.IClassDefinition;
     38 import signature.model.IClassReference;
     39 import signature.model.IConstructor;
     40 import signature.model.IEnumConstant;
     41 import signature.model.IField;
     42 import signature.model.IMethod;
     43 import signature.model.IPackage;
     44 import signature.model.IParameter;
     45 import signature.model.Kind;
     46 import signature.model.Modifier;
     47 import signature.model.util.ModelUtil;
     48 
     49 public abstract class ConvertEnumTest extends AbstractConvertTest {
     50 
     51     @Test
     52     public void testEnum1() throws IOException {
     53         IApi api = convert(new CompilationUnit("test.A",
     54                 "package test; " +
     55                 "public enum A {" +
     56                 "  ONE, TWO, THREE" +
     57                 "}"));
     58         IPackage sigPackage = ModelUtil.getPackage(api, "test");
     59         IClassDefinition c = ModelUtil.getClass(sigPackage, "A");
     60         assertNotNull(c);
     61         assertTrue(c.getKind() == Kind.ENUM);
     62 
     63         Set<IEnumConstant> constants = c.getEnumConstants();
     64         assertEquals(3, constants.size());
     65 
     66         Set<String> constantNames = new HashSet<String>();
     67         for (IEnumConstant constant : constants) {
     68             constantNames.add(constant.getName());
     69         }
     70 
     71         assertTrue(constantNames.contains("ONE"));
     72         assertTrue(constantNames.contains("TWO"));
     73         assertTrue(constantNames.contains("THREE"));
     74 
     75 //        IEnumConstant[] enumConstants = new IEnumConstant[3];
     76 //        for (IEnumConstant enumConstant : constants) {
     77 //            enumConstants[enumConstant.getOrdinal()] = enumConstant;
     78 //            assertEquals(0, enumConstant.getAnnotations().size());
     79 //            assertSame(c, enumConstant.getType());
     80 //        }
     81 //
     82 //        assertEquals("ONE", enumConstants[0].getName());
     83 //        assertEquals("TWO", enumConstants[1].getName());
     84 //        assertEquals("THREE", enumConstants[2].getName());
     85     }
     86 
     87     @Test
     88     public void testEnum2() throws IOException {
     89         IApi api = convert(new CompilationUnit("test.A",
     90                 "package test; " +
     91                 "public enum A {" +
     92                 "  ONE, TWO, THREE;" +
     93                 "  public static A FOUR = ONE;" +
     94                 "}"));
     95         IPackage sigPackage = ModelUtil.getPackage(api, "test");
     96         IClassDefinition c = ModelUtil.getClass(sigPackage, "A");
     97         assertNotNull(c);
     98         assertTrue(c.getKind() == Kind.ENUM);
     99 
    100         Set<IEnumConstant> constants = c.getEnumConstants();
    101         assertEquals(3, constants.size());
    102 
    103         Set<IField> fields = c.getFields();
    104         assertEquals(1, fields.size());
    105         IField field = c.getFields().iterator().next();
    106 
    107         assertEquals("FOUR", field.getName());
    108         assertSame(c, ((IClassReference)field.getType()).getClassDefinition());
    109     }
    110 
    111 
    112    @Test
    113     public void testEnum3() throws IOException {
    114         IApi api = convert(new CompilationUnit("test.A",
    115                 "package test; " +
    116                 "public enum A {" +
    117                 "  ONE(1), TWO(2), THREE(3);" +
    118                 "  A(int value){}" +
    119                 "}"));
    120         IPackage sigPackage = ModelUtil.getPackage(api, "test");
    121         IClassDefinition c = ModelUtil.getClass(sigPackage, "A");
    122         assertNotNull(c);
    123         assertTrue(c.getKind() == Kind.ENUM);
    124 
    125         Set<IEnumConstant> constants = c.getEnumConstants();
    126         assertEquals(3, constants.size());
    127 
    128         Set<IConstructor> ctors = c.getConstructors();
    129         assertEquals(0, ctors.size());
    130 
    131         Set<IMethod> methods = c.getMethods();
    132         assertEquals(2, methods.size());
    133         Map<String, IMethod> map = new HashMap<String, IMethod>();
    134         for(IMethod m : methods){
    135             map.put(m.getName(), m);
    136         }
    137 
    138         IMethod values = map.get("values");
    139         assertNotNull(values);
    140         assertEquals(0, values.getParameters().size());
    141         assertTrue(values.getReturnType() instanceof IArrayType);
    142         assertSame(c, ((IClassReference)((IArrayType)values.getReturnType()).getComponentType()).getClassDefinition());
    143         assertTrue(c.getModifiers().contains(Modifier.PUBLIC));
    144         assertFalse(c.getModifiers().contains(Modifier.STATIC));
    145         assertTrue(c.getModifiers().contains(Modifier.FINAL));
    146 
    147         IMethod valueOf = map.get("valueOf");
    148         assertNotNull(valueOf);
    149         assertEquals(1, valueOf.getParameters().size());
    150         IParameter param = valueOf.getParameters().iterator().next();
    151         assertEquals("java.lang.String", ((IClassReference)param.getType()).getClassDefinition().getQualifiedName());
    152         assertSame(c, ((IClassReference)valueOf.getReturnType()).getClassDefinition());
    153     }
    154 
    155    @Test
    156    public void testEnum4() throws IOException {
    157        IApi api = convert(new CompilationUnit("test.A",
    158                "package test; " +
    159                "public enum A {" +
    160                "  ONE { void m(){} }, TWO{ void m(){} };" +
    161                "  abstract void m();" +
    162                "}"));
    163        IPackage sigPackage = ModelUtil.getPackage(api, "test");
    164        IClassDefinition c = ModelUtil.getClass(sigPackage, "A");
    165        assertNotNull(c);
    166        assertTrue(c.getKind() == Kind.ENUM);
    167 
    168        Set<IEnumConstant> constants = c.getEnumConstants();
    169        assertEquals(2, constants.size());
    170 
    171        Set<IConstructor> ctors = c.getConstructors();
    172        assertEquals(0, ctors.size());
    173 
    174        Set<IMethod> methods = c.getMethods();
    175        assertEquals(2, methods.size());
    176        Map<String, IMethod> map = new HashMap<String, IMethod>();
    177        for(IMethod m : methods){
    178            map.put(m.getName(), m);
    179        }
    180 
    181        IMethod values = map.get("values");
    182        assertNotNull(values);
    183        assertEquals(0, values.getParameters().size());
    184        assertTrue(values.getReturnType() instanceof IArrayType);
    185        assertSame(c, ((IClassReference)((IArrayType)values.getReturnType()).getComponentType()).getClassDefinition());
    186        assertTrue(c.getModifiers().contains(Modifier.PUBLIC));
    187        assertFalse(c.getModifiers().contains(Modifier.STATIC));
    188        assertFalse(c.getModifiers().contains(Modifier.FINAL));
    189    }
    190 
    191 }
    192