Exemplo n.º 1
0
 @Test
 public void testThatAccessorAlwaysReturnsSameInstance() throws Exception {
   Method accessor = checkThatSingletonContainsStaticInstanceAccessorMethod(Earth.class);
   Earth earth1 = (Earth) accessor.invoke(null);
   Earth earth2 = (Earth) accessor.invoke(null);
   assertSame("Instances returned should be the same", earth1, earth2);
 }
Exemplo n.º 2
0
 @Test
 public void checkThatOtherMethodsAreNonStatic() throws NoSuchMethodException {
   Method spin = Earth.class.getDeclaredMethod("spin");
   assertFalse("Method spin() should not be static", Modifier.isStatic(spin.getModifiers()));
   Method warmUp = Earth.class.getDeclaredMethod("warmUp");
   assertFalse("Method warmUp() should not be static", Modifier.isStatic(warmUp.getModifiers()));
 }
Exemplo n.º 3
0
 @Test
 public void callTheSpinAndWarmUpMethods() throws Exception {
   Method accessor = checkThatSingletonContainsStaticInstanceAccessorMethod(Earth.class);
   Earth earth = (Earth) accessor.invoke(null);
   earth.spin();
   earth.warmUp();
 }
Exemplo n.º 4
0
  @Test
  public void get_generic_type_of_interface_method_parameters() throws Exception {
    Method method = Interface.class.getDeclaredMethod("stringListMethod", List.class);
    Type[] types = method.getGenericParameterTypes();
    ParameterizedType type = (ParameterizedType) types[0];

    assertEquals(List.class, type.getRawType());
    assertArrayEquals(new Type[] {String.class}, type.getActualTypeArguments());
    assertEquals(null, type.getOwnerType());
  }
Exemplo n.º 5
0
 private Method checkThatSingletonContainsStaticInstanceAccessorMethod(Class clazz) {
   for (Method method : clazz.getDeclaredMethods()) {
     if (method.getReturnType() == clazz && method.getParameterTypes().length == 0) {
       assertTrue(
           "Method returning the Earth instance should be static",
           Modifier.isStatic(method.getModifiers()));
       return method;
     }
   }
   fail("No static accessor method found.");
   return null; // will never be called.
 }
Exemplo n.º 6
0
  @Test
  public void keywordTest() {
    startTest("Keyword Test");

    attr = Method.Create("protected():int");
    assertTrue(attr == null);
    attr = Method.Create("_x():private");
    assertTrue(attr == null);
    attr = Method.Create("Abstract():int");
    assertFalse(attr == null);
    passed();
  }
Exemplo n.º 7
0
  @Test
  public void argTest() {
    startTest("arg test");

    attr = Method.Create("name():String");
    assertNotNull(attr);
    assertTrue(attr.getParameters().isEmpty());

    attr = Method.Create("name ( ) : String");
    assertNotNull(attr);
    assertTrue(attr.getParameters().isEmpty());

    attr = Method.Create(" name ( index : int ): String ");
    assertNotNull(attr);

    // only type of an argument matters
    assertTrue(attr.getParameters().contains(Argument.Create("x : int")));
    assertTrue(attr.getParameters().size() == 1);

    attr = Method.Create("add ( int x, int y ) : int");
    assertNotNull(attr);
    assertTrue(attr.getParameters().contains(Argument.Create("x : int")));
    assertTrue(attr.getParameters().size() == 2);

    passed();
  }
Exemplo n.º 8
0
 private void invalidValueHelper(Object obj, String methodName, int i) {
   try {
     Method m = obj.getClass().getMethod(methodName, int.class);
     m.invoke(obj, i);
     fail("Didn't cause an invalid value exception.");
   } catch (NoSuchMethodException | SecurityException e) {
     System.err.println("Error when getting the method. Uh oh!");
     e.printStackTrace();
     fail();
   } catch (IllegalAccessException | IllegalArgumentException e) {
     System.err.println("Error when invoking the method. Uh oh!");
     e.printStackTrace();
     fail();
   } catch (InvocationTargetException e) {
     assertEquals(JsonTypeException.class, e.getTargetException().getClass());
   }
 }
Exemplo n.º 9
0
  @Test
  public void accessTest() {
    startTest("Access Test");

    String sig = " count ( ) : int";

    assertTrue(Method.Create(sig) != null);

    assertTrue(Method.Create(sig).getAccess() == Access.DEFAULT);

    assertTrue(Method.Create("~" + sig).getAccess() == Access.DEFAULT);
    assertTrue(Method.Create("+" + sig).getAccess() == Access.PUBLIC);
    assertTrue(Method.Create("-" + sig).getAccess() == Access.PRIVATE);
    assertTrue(Method.Create("#" + sig).getAccess() == Access.PROTECTED);

    assertTrue(Method.Create("default " + sig).getAccess() == Access.DEFAULT);
    assertTrue(Method.Create("public " + sig).getAccess() == Access.PUBLIC);
    assertTrue(Method.Create("private " + sig).getAccess() == Access.PRIVATE);
    assertTrue(Method.Create("protected " + sig).getAccess() == Access.PROTECTED);

    passed();
  }
Exemplo n.º 10
0
  @Test
  public void basicTest() {
    startTest("Basic Test");
    attr = Method.Create("- _name():String");

    assertNotNull(attr);

    assertTrue(attr.getName().equals("_name"));
    assertTrue(attr.getType().equals("String"));
    assertTrue(attr.getAccess() == Access.PRIVATE);

    assertFalse(attr.isStatic());
    assertFalse(attr.isAbstract());

    assertTrue(Method.Create("_name : String") == null);
    assertTrue(Method.Create("_name String") == null);

    // decided not to test
    // Method meth = Method.Create ( "_name( ) String " );

    passed();
  }
Exemplo n.º 11
0
  @Test
  public void modTest() {
    startTest("Modifier test");
    attr = Method.Create("+ abstract static x( ):int");

    // Methods cannot be abstract and static
    assertTrue(attr == null);

    attr = Method.Create("- static modTest():int");
    assertFalse(attr == null);
    assertTrue(attr.isStatic());
    assertFalse(attr.isAbstract());

    attr = Method.Create("- abstract regex():Pattern");
    assertFalse(attr == null);
    assertTrue(attr.isAbstract());
    assertFalse(attr.isStatic());

    passed();
  }