Esempio n. 1
0
 /** Unit test for getNamesOfReadWriteProperties. */
 @Test
 public void testGetNamesOfReadWriteProperties() {
   TypeAnalysis analysis = new TypeAnalysis(BeanWith1Field.class);
   Set<String> properties = analysis.getNamesOfReadWriteProperties();
   Assert.assertEquals(1, properties.size());
   Assert.assertTrue(properties.contains("field2"));
 }
Esempio n. 2
0
  /**
   * Unit test for analyzeFields() and for the get methods that retrieve information generated by
   * analyzeFields()..
   */
  @Test
  public void testAnalyzeFields() {
    TypeAnalysis analysis = new TypeAnalysis();
    analysis.clazz = BeanWithReadOnlyAndWriteOnly.class;

    analysis.analyzeFields();
    Assert.assertEquals(3, analysis.annotatedFields.size());
    Set<Field> allSupw = analysis.getAnnotated(Anno.class);
    Assert.assertEquals(1, allSupw.size());

    List<Field> stateSupw = analysis.getAnnotated(Anno.class, "state");
    Assert.assertEquals(1, stateSupw.size());

    Field state1 = analysis.getFirstAnnotated(Anno.class, "state");
    Assert.assertNotNull(state1);

    Field state2 = analysis.getFirstFieldByName("state");
    Assert.assertNotNull(state2);

    List<Field> allState = analysis.getAllFieldsByName("state");
    Assert.assertEquals(1, allState.size());

    Set<Field> all = analysis.getAllFields();
    Assert.assertEquals(4, all.size());
  }
Esempio n. 3
0
  /** Unit test for constructor on concrete class. */
  @Test
  public void testConstructor_onConcreteClass() {
    TypeAnalysis analysis = new TypeAnalysis(BeanWithReadOnlyAndWriteOnly.class);
    Assert.assertEquals(BeanWithReadOnlyAndWriteOnly.class, analysis.clazz);
    Assert.assertEquals(0, analysis.getAbstractGetters().size());
    Assert.assertEquals(0, analysis.getAbstractSetters().size());
    Assert.assertEquals(0, analysis.getAbstractMethods().size());

    Assert.assertEquals(3, analysis.getConcreteGetters().size());
    Assert.assertEquals(4, analysis.getConcreteSetters().size());
    Assert.assertEquals(OBJECT_METHODS_COUNT + 1, analysis.concreteMethods.size());

    Assert.assertEquals(1, analysis.getOddProperties().size());
    Assert.assertEquals(1, analysis.getOddPropertiesNames().size());

    Assert.assertEquals(5, analysis.allProperties.size());
    Assert.assertEquals(1, analysis.oddPropertiesByName.size());

    BeanPropertyDefinition<?> odd = analysis.oddProperties.iterator().next();
    Assert.assertEquals("name", odd.getName());
    Assert.assertNull(odd.getGetter());

    Assert.assertEquals(1, analysis.getAllFieldsByName("name").size());

    Assert.assertNotNull(analysis.annotatedFields);
    Assert.assertEquals(3, analysis.annotatedFields.size());
  }
Esempio n. 4
0
 /** Unit test for handleSetter. */
 @Test
 public void testHandleMethod_concreteNoAccessor() {
   TypeAnalysis analysis = new TypeAnalysis();
   analysis.clazz = AbstractSampleInterfaceImpl.class;
   Method method = ReflectionUtils.getMethodByUniqueName("toString", analysis.clazz);
   analysis.handleMethod(method);
   Assert.assertTrue(analysis.abstractGetters.isEmpty());
   Assert.assertTrue(analysis.concreteGetters.isEmpty());
   Assert.assertTrue(analysis.abstractSetters.isEmpty());
   Assert.assertTrue(analysis.concreteSetters.isEmpty());
   Assert.assertTrue(analysis.abstractMethods.isEmpty());
   Assert.assertTrue(analysis.concreteMethods.contains(method));
 }
Esempio n. 5
0
 /** Unit test for handleSetter. */
 @Test
 public void testHandleMethod_GetterAndSetter() {
   TypeAnalysis analysis = new TypeAnalysis();
   analysis.clazz = IBeanWithIdAndNameImpl.class;
   Method setter = AccessorUtils.getSetter("beanName", String.class, analysis.clazz);
   analysis.handleMethod(setter);
   Method getter = AccessorUtils.getGetter("beanName", String.class, analysis.clazz);
   analysis.handleMethod(getter);
   Array key = new Array("beanName", String.class);
   BeanPropertyDefinition<?> bpd = analysis.allProperties.get(key);
   Assert.assertNotNull(bpd);
   Assert.assertNotNull(bpd.getGetter());
   Assert.assertNotNull(bpd.getSetter());
 }
Esempio n. 6
0
 /**
  * Run the type checking analysis on an expression that is required to have the type specified by
  * the expected parameter.
  */
 Type require(TypeAnalysis typing, Type expected) {
   Type t = analyze(typing);
   if (t != expected) {
     typing.report(new Failure(pos, "An expression of type " + expected + " was expected"));
     return expected;
   }
   return t;
 }
Esempio n. 7
0
 /** Unit test for handleSetter. */
 @Test
 public void testHandleMethod_abstractSetter() {
   TypeAnalysis analysis = new TypeAnalysis();
   analysis.clazz = IBeanWithIdAndName.class;
   Method setter = AccessorUtils.getSetter("beanName", String.class, analysis.clazz);
   analysis.handleMethod(setter);
   Assert.assertTrue(analysis.abstractGetters.isEmpty());
   Assert.assertTrue(analysis.concreteGetters.isEmpty());
   Assert.assertTrue(analysis.abstractSetters.contains(setter));
   Assert.assertTrue(analysis.concreteSetters.isEmpty());
   Assert.assertTrue(analysis.abstractMethods.isEmpty());
   Assert.assertTrue(analysis.concreteMethods.isEmpty());
   Array key = new Array("beanName", String.class); // ???
   BeanPropertyDefinition<?> bpd = analysis.allProperties.get(key);
   Assert.assertNotNull(bpd);
   Assert.assertNotNull(bpd.getSetter());
 }
Esempio n. 8
0
 /**
  * Run type checking analysis on this expression. The typing parameter provides access to the
  * scope analysis phase (in particular, to the associated error handler), and the env parameter
  * reflects the environment in which the expression is evaluated. Unlike scope analysis for
  * statements, there is no return result here: an expression cannot introduce new variables in to
  * a program, so the final environment will always be the same as the initial environment.
  */
 public Type analyze(TypeAnalysis typing) {
   Type lt = lhs.analyze(typing);
   type = rhs.analyze(typing);
   if (!lt.equal(type)) {
     typing.report(
         new Failure(pos, "Attempt to assign " + type + " value to variable of type " + lt));
   }
   return type;
 }
Esempio n. 9
0
  /** Unit test for constructor on concrete class. */
  @Test
  public void testConstructor_onExtendedAbstractClass() {
    TypeAnalysis analysis = new TypeAnalysis(AbstractExtendedSampleInterfaceImpl.class);
    Assert.assertEquals(AbstractExtendedSampleInterfaceImpl.class, analysis.clazz);
    Assert.assertEquals(5, analysis.getAbstractGetters().size());
    Assert.assertEquals(5, analysis.getAbstractSetters().size());
    Assert.assertEquals(2, analysis.getAbstractMethods().size());

    Assert.assertEquals(1, analysis.getConcreteGetters().size());
    Assert.assertEquals(1, analysis.getConcreteSetters().size());
    Assert.assertEquals(OBJECT_METHODS_COUNT + 1, analysis.concreteMethods.size());

    Assert.assertEquals(0, analysis.getOddProperties().size());
    Assert.assertEquals(0, analysis.getOddPropertiesNames().size());

    Assert.assertNull(analysis.getAllFieldsByName("field1"));

    Assert.assertEquals(6, analysis.allProperties.size());
    Assert.assertEquals(0, analysis.oddPropertiesByName.size());
  }
Esempio n. 10
0
  /** Unit test for isSerializable. */
  @Test
  public void testIsSerializable() {
    TypeAnalysis analysis1 = new TypeAnalysis();
    analysis1.clazz = ArrayList.class;
    Assert.assertTrue(analysis1.isSerializable());

    TypeAnalysis analysis2 = new TypeAnalysis();
    analysis2.clazz = InputStream.class;
    Assert.assertFalse(analysis2.isSerializable());
  }
Esempio n. 11
0
  /** The type is an interface that has the id property declared by more than one interfaces. */
  @Test
  public void testConstructor_samePropertyDeclaredInDifferentInterfaces() {
    TypeAnalysis analysis = new TypeAnalysis(IdentifiedBean.class);

    Assert.assertEquals(3, analysis.getAbstractGetters().size());
    Assert.assertEquals(3, analysis.getAbstractSetters().size());
    Assert.assertEquals(1, analysis.getAbstractMethods().size());

    Assert.assertEquals(0, analysis.getConcreteGetters().size());
    Assert.assertEquals(0, analysis.getConcreteSetters().size());
    Assert.assertEquals(0, analysis.concreteMethods.size());

    Assert.assertEquals(3, analysis.allProperties.size());

    Assert.assertEquals(0, analysis.getOddProperties().size());
    Assert.assertEquals(0, analysis.getOddPropertiesNames().size());
    Assert.assertEquals(0, analysis.oddPropertiesByName.size());
  }
Esempio n. 12
0
  /** Unit test for constructor on concrete class. */
  @Test
  public void testConstructor_onConcreteTypeThatOverridesItsSupertype() {
    TypeAnalysis analysis = new TypeAnalysis(Range.class);

    Assert.assertEquals(0, analysis.getAbstractGetters().size());
    Assert.assertEquals(0, analysis.getAbstractSetters().size());
    Assert.assertEquals(0, analysis.getAbstractMethods().size());

    Assert.assertEquals(2, analysis.getConcreteGetters().size());
    Assert.assertEquals(2, analysis.getConcreteSetters().size());
    Assert.assertEquals(
        OBJECT_METHODS_COUNT + COMPARABLE_METHODS_COUNT + 3, analysis.concreteMethods.size());

    Assert.assertEquals(2, analysis.allProperties.size());

    Assert.assertEquals(0, analysis.getOddProperties().size());
    Assert.assertEquals(0, analysis.getOddPropertiesNames().size());
    Assert.assertEquals(0, analysis.oddPropertiesByName.size());
  }
Esempio n. 13
0
  /** Unit test for getSerialVersionUniqueId. */
  @Test
  public void testGetSerialVersionUniqueId() {
    TypeAnalysis analysis1 = new TypeAnalysis();
    analysis1.clazz = Address.class;
    Long l = analysis1.getSerialVersionUniqueId();
    Assert.assertEquals(Long.valueOf(3L), l);

    TypeAnalysis analysis2 = new TypeAnalysis();
    analysis2.clazz = AlmostEmpty1.class;
    Long n = analysis2.getSerialVersionUniqueId();
    Assert.assertNull(n);
  }
Esempio n. 14
0
  /** Unit test for constructor on interface. */
  @Test
  public void testConstructor_onInterfaceWithAllTypesOfPropertyNames() {
    TypeAnalysis analysis = new TypeAnalysis(IBeanWithAllTypesOfPropertyNames.class);
    Assert.assertEquals(IBeanWithAllTypesOfPropertyNames.class, analysis.clazz);
    Assert.assertEquals(4, analysis.getAbstractGetters().size());
    Assert.assertEquals(4, analysis.getAbstractSetters().size());
    Assert.assertEquals(0, analysis.getAbstractMethods().size());

    Assert.assertEquals(0, analysis.getConcreteGetters().size());
    Assert.assertEquals(0, analysis.getConcreteSetters().size());
    Assert.assertEquals(0, analysis.concreteMethods.size());

    Assert.assertEquals(0, analysis.getOddProperties().size());
    Assert.assertEquals(0, analysis.getOddPropertiesNames().size());

    Assert.assertNull(analysis.getAllFieldsByName("beanName"));

    Assert.assertEquals(4, analysis.allProperties.size());
    Assert.assertEquals(0, analysis.oddPropertiesByName.size());

    Assert.assertNotNull(analysis.annotatedFields);
    Assert.assertEquals(0, analysis.annotatedFields.size());
  }