Search in sources :

Example 66 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class TypeRewriterTests method rewriteWithPrimitiveReturnValues_double.

@Test
public void rewriteWithPrimitiveReturnValues_double() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("data.HelloWorldPrimitive");
    ReloadableType rtype = typeRegistry.addType("data.HelloWorldPrimitive", loadBytesForClass("data.HelloWorldPrimitive"));
    Result r = runUnguarded(rtype.getClazz(), "getValueDouble");
    assertTrue(r.returnValue instanceof Double);
    assertEquals(3.0D, r.returnValue);
    rtype.loadNewVersion("000", rtype.bytesInitial);
    r = runUnguarded(rtype.getClazz(), "getValueDouble");
    assertTrue(r.returnValue instanceof Double);
    assertEquals(3.0D, r.returnValue);
    rtype.loadNewVersion("002", retrieveRename("data.HelloWorldPrimitive", "data.HelloWorldPrimitive002"));
    r = runUnguarded(rtype.getClazz(), "getValueDouble");
    assertTrue(r.returnValue instanceof Double);
    assertEquals(6.0D, r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 67 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class TypeRewriterTests method rewriteWithParams.

@Test
public void rewriteWithParams() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("data.HelloWorld");
    ReloadableType rtype = typeRegistry.addType("data.HelloWorld", loadBytesForClass("data.HelloWorld"));
    Result r = runUnguarded(rtype.getClazz(), "getValueWithParams", "aaa", "bb");
    assertTrue(r.returnValue instanceof String);
    assertEquals("message with inserts aaa and bb", r.returnValue);
    rtype.loadNewVersion("000", rtype.bytesInitial);
    r = runUnguarded(rtype.getClazz(), "getValueWithParams", "c", "d");
    assertTrue(r.returnValue instanceof String);
    assertEquals("message with inserts c and d", r.returnValue);
    rtype.loadNewVersion("002", retrieveRename("data.HelloWorld", "data.HelloWorld002"));
    r = runUnguarded(rtype.getClazz(), "getValueWithParams", "aaa", "bb");
    assertTrue(r.returnValue instanceof String);
    assertEquals("message with inserts bb and aaa", r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 68 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class TypeRewriterTests method rewriteWithPrimitiveReturnValues_intArray.

@Test
public void rewriteWithPrimitiveReturnValues_intArray() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("data.HelloWorldPrimitive");
    ReloadableType rtype = typeRegistry.addType("data.HelloWorldPrimitive", loadBytesForClass("data.HelloWorldPrimitive"));
    Result r = runUnguarded(rtype.getClazz(), "getArrayInt");
    assertTrue(r.returnValue instanceof int[]);
    assertEquals(3, ((int[]) r.returnValue)[0]);
    rtype.loadNewVersion("000", rtype.bytesInitial);
    r = runUnguarded(rtype.getClazz(), "getArrayInt");
    assertTrue(r.returnValue instanceof int[]);
    assertEquals(3, ((int[]) r.returnValue)[0]);
    rtype.loadNewVersion("002", retrieveRename("data.HelloWorldPrimitive", "data.HelloWorldPrimitive002"));
    r = runUnguarded(rtype.getClazz(), "getArrayInt");
    assertTrue(r.returnValue instanceof int[]);
    assertEquals(5, ((int[]) r.returnValue)[0]);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 69 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_callReloadableMethodWithNonReloadableImplementation.

/**
	 * Scenario where method lookup spills over from the reloadable world into the non-reloadable world. This can only
	 * happen if are looking for a method that is declared on a reloadable type, but we need to find the implementation
	 * in a non-reloadable one.
	 */
@Test
public void test_callReloadableMethodWithNonReloadableImplementation() throws Exception {
    //Scenario requires a method that is reloadable but not implemented by a reloadable
    //class. The followig circumstances trigger this condition:
    // Situation involves three types:
    // A non reloadable superClass
    String superClassName = "reflection.NonReloadableSuperClass";
    Class<?> superClass = nonReloadableClass(superClassName);
    // A reloadable interface
    String interfaceName = TARGET_PACKAGE + ".InterfaceTarget";
    ReloadableType interfaceTarget = reloadableClass(interfaceName);
    ClassPrinter.print(interfaceTarget.getBytesLoaded());
    // A reloadable class
    String subclassName = TARGET_PACKAGE + ".SubClassImplementsInterface";
    ReloadableType subClass = reloadableClass(subclassName);
    // These types relate to one another as follows:
    //1) the method 'm' must be declared in the reloadable interface
    String interfaceMethodName = "interfaceMethod";
    Result r = getDeclaredMethod(interfaceTarget.getClazz(), interfaceMethodName);
    assertMethod("public abstract java.lang.String " + interfaceName + "." + interfaceMethodName + "()", r);
    Method m = (Method) r.returnValue;
    //2) The reloadable type implements this interface (without providing its own implementation of m)
    assertTrue(interfaceTarget.getClazz().isAssignableFrom(subClass.getClazz()));
    try {
        r = getDeclaredMethod(subClass.getClazz(), interfaceMethodName);
        fail("Assuming that interface implementation is inherited, not directly implemented");
    } catch (ResultException e) {
        assertNoSuchMethodException(subclassName + "." + interfaceMethodName + "()", e);
    }
    //3) A non-reloadable superclass provides the actual implementation of m via inheritance
    r = getDeclaredMethod(superClass, interfaceMethodName);
    assertMethod("public java.lang.String " + superClassName + "." + interfaceMethodName + "()", r);
    //4) Invoke the interface method on an instance of the subclass... should cause lookup
    //   to find implementation in non-reloadable superclass
    Object subInstance = subClass.getClazz().newInstance();
    r = invokeOn(subInstance, m);
    assertEquals("NonReloadableSuperClass.interfaceMethod", r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Method(java.lang.reflect.Method) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 70 with Result

use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.

the class ConstructorAdHocTest method test_accessDeletedConstructor.

//	/**
//	 * When a Constructor is "regotten" the newly gotten Field will have to be a "fresh" object with its accessibility flag NOT
//	 * set.
//	 */
//	private void doTestAccessibleFlagIsRefreshed(Constructor<?> cons) throws Exception {
//		registry = getTypeRegistry("reflection.constructors..*");
//		targetClass = reloadableClass("reflection.constructors.ClassForNewInstance");
//
//		callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
//		callerInstance = newInstance(callerClazz);
//		
//		String fieldToAccess = scope + "Field";
//		String expectMsg = "Class " + INVOKER_CLASS_NAME + " " + "can not access a member of class " + targetClass.dottedtypename + " "
//				+ "with modifiers \"" + (scope.equals("default") ? "" : scope) + "\"";
//
//		//First... we do set the Access flag, it should work!
//		Result r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
//		Assert.assertEquals(r.returnValue, fieldToAccess + " value");
//
//		//Then... we do not set the Access flag, it should fail!
//		try {
//			r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, false);
//			Assert.fail("Without setting access flag shouldn't be allowed!");
//		} catch (ResultException e) {
//			assertIllegalAccess(expectMsg, e);
//		}
//
//		//Finally, this should also still work... after we reload the type!
//		reloadType(targetClass, "002");
//
//		//First... we do set the Access flag, it should work!
//		r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
//		Assert.assertEquals("new "+fieldToAccess + " value", r.returnValue);
//
//		//Then... we do not set the Access flag, it should not be allowed!
//		try {
//			r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, false);
//			Assert.fail("Without setting access flag shouldn't be allowed!");
//		} catch (ResultException e) {
//			assertIllegalAccess(expectMsg, e);
//		}
//	}
//	@Test
//	public void test_accessibleFlagIsRefreshedForPrivate() throws Exception {
//		doTestAccessibleFlagIsRefreshed("private");
//	}
//
//	@Test
//	public void test_accessibleFlagIsRefreshedForProtected() throws Exception {
//		doTestAccessibleFlagIsRefreshed("protected");
//	}
//
//	@Test
//	public void test_accessibleFlagIsRefreshedForDefault() throws Exception {
//		doTestAccessibleFlagIsRefreshed("default");
//	}
@Test
public void test_accessDeletedConstructor() throws Exception {
    registry = getTypeRegistry("reflection.constructors..*");
    targetClass = reloadableClass("reflection.constructors.ClassForNewInstance");
    //		Object targetInstance = 
    newInstance(targetClass.getClazz());
    Constructor<?> c = jlClassGetConstructor(targetClass.getClazz(), char.class, char.class);
    Assert.assertEquals("public reflection.constructors.ClassForNewInstance(char,char)", c.toString());
    // First try to access the Constructor before reloading
    callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
    callerInstance = newInstance(callerClazz);
    Result r = runOnInstance(callerClazz, callerInstance, "callNewInstance", c, (Object) new Object[] { 'a', 'b' });
    Assert.assertEquals(targetClass.getClazz(), r.returnValue.getClass());
    // Now reload the class... constructor is deleted
    reloadType(targetClass, "002");
    try {
        r = runOnInstance(callerClazz, callerInstance, "callNewInstance", c, (Object) new Object[] { 'a', 'b' });
        Assert.fail("Expected an error");
    } catch (ResultException re) {
        Throwable e = re.getCause();
        //			e.printStackTrace();
        Assert.assertEquals(InvocationTargetException.class, e.getClass());
        //Nested exception
        e = e.getCause();
        e.printStackTrace();
        Assert.assertEquals(NoSuchMethodError.class, e.getClass());
        //Example error message from Sun JVM:
        //			Exception in thread "main" java.lang.NoSuchMethodError: Target.<init>(C)V
        //			at Main.main(Main.java:10)
        Assert.assertEquals("reflection.constructors.ClassForNewInstance.<init>(CC)V", e.getMessage());
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Aggregations

Result (org.springsource.loaded.test.infra.Result)155 Test (org.junit.Test)139 ReloadableType (org.springsource.loaded.ReloadableType)106 TypeRegistry (org.springsource.loaded.TypeRegistry)97 ResultException (org.springsource.loaded.test.infra.ResultException)28 Method (java.lang.reflect.Method)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PrintStream (java.io.PrintStream)5 Constructor (java.lang.reflect.Constructor)4 Ignore (org.junit.Ignore)4 Field (java.lang.reflect.Field)2 ReflectiveInterceptor.jlClassGetField (org.springsource.loaded.ri.ReflectiveInterceptor.jlClassGetField)1