@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); }
@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())); }
@Test public void callTheSpinAndWarmUpMethods() throws Exception { Method accessor = checkThatSingletonContainsStaticInstanceAccessorMethod(Earth.class); Earth earth = (Earth) accessor.invoke(null); earth.spin(); earth.warmUp(); }
@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()); }
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. }
@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(); }
@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(); }
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()); } }
@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(); }
@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(); }
@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(); }