Beispiel #1
0
  /**
   * Test case for TEATROVE-55.
   *
   * <p>https://github.com/teatrove/teatrove/issues/55
   */
  @Test
  public void testGetConstructorWithGenerics() throws Exception {

    ClassInjector injector = ClassInjector.getInstance(this.getClass().getClassLoader());

    // build first constructor that purely merges general play context
    Constructor<?> ctor1 =
        MergedClass.getConstructor2(
            injector,
            new Class[] {PlayContext.class, GameContext.class, StringContext.class},
            new String[] {"PlayContext$", "GameContext$", "StringContext$"});

    // ensure two get play methods and both return hockey game
    int found1 = 0, bridge1 = 0;
    Method[] methods1 = ctor1.getDeclaringClass().getMethods();
    for (Method method1 : methods1) {
      if (method1.getName().equals("getPlay")) {
        found1++;
        if (method1.isBridge()) {
          bridge1++;
        }
      }
    }

    assertEquals("expected two getPlay methods", 2, found1);
    assertEquals("expected one getPlay bridge method", 1, bridge1);

    // now build another constructor that merges that merge plus another
    // to ensure generics flow through from one merge to another
    Constructor<?> ctor2 =
        MergedClass.getConstructor2(
            ClassInjector.getInstance(this.getClass().getClassLoader()),
            new Class[] {ctor1.getDeclaringClass(), Dog.class});

    // ensure two get play methods and both return hockey game
    int found2 = 0, bridge2 = 0;
    Method[] methods2 = ctor2.getDeclaringClass().getMethods();
    for (Method method2 : methods2) {
      if (method2.getName().equals("getPlay")) {
        found2++;
        if (method2.isBridge()) {
          bridge2++;
        }
      }
    }

    assertEquals("expected two getPlay methods", 2, found1);
    assertEquals("expected one getPlay bridge method", 1, bridge1);

    // ensure get plays method has generic signature for list
    Method playsMethod = ctor2.getDeclaringClass().getMethod("getPlays");
    Type returnType = playsMethod.getGenericReturnType();
    assertTrue("expected param type", returnType instanceof ParameterizedType);

    assertEquals(
        "expected plays type for type param",
        HockeyGamePlayLog.class,
        ((ParameterizedType) returnType).getActualTypeArguments()[0]);
  }
Beispiel #2
0
  @Test
  public void testGetConstructor() throws Exception {

    // generate ctor
    Constructor<?> ctor =
        MergedClass.getConstructor(
            ClassInjector.getInstance(this.getClass().getClassLoader()),
            new Class[] {Dog.class, Cat.class},
            new String[] {"Dog$", "Cat$"});

    // validate ctor
    assertNotNull("invalid ctor", ctor);

    // validate get legs returns valid value
    Object instance = ctor.newInstance(new Dog(), new Cat());
    try {
      instance.getClass().getMethod("getLegs");
      fail("expected getLegs method to be invalid as it conflicts");
    } catch (NoSuchMethodException nsme) {
      /* valid */
    }

    // validate Dog.getLegs
    Method dmethod = instance.getClass().getMethod("Dog$getLegs");
    Object dresult = dmethod.invoke(instance);
    assertEquals("invalid Dog.getLegs value", Integer.valueOf(1), dresult);

    // validate Cat.getLegs
    Method cmethod = instance.getClass().getMethod("Cat$getLegs");
    Object cresult = cmethod.invoke(instance);
    assertEquals("invalid Cat.getLegs value", Double.valueOf(2.0), cresult);

    // validate equals methods
    assertFalse("invalid equals(null)", instance.equals(null));
    assertTrue("invalid equality to itself", instance.equals(instance));
    assertFalse("expected non-empty string", instance.toString().isEmpty());
  }