use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class TypeRewriterTests method constructorReloading4.
// Now looking at simply changing what fields we initialize in a ctor - the simplest case really
@Test
public void constructorReloading4() throws Exception {
String theType = "ctors.Setter";
TypeRegistry r = getTypeRegistry(theType);
ReloadableType rtype = r.addType(theType, loadBytesForClass(theType));
result = runConstructor(rtype.getClazz(), "");
Result res = runOnInstance(rtype.getClazz(), result.returnValue, "getInteger");
assertEquals(1, ((Integer) res.returnValue).intValue());
res = runOnInstance(rtype.getClazz(), result.returnValue, "getString");
assertEquals("one", (res.returnValue));
rtype.loadNewVersion("000", rtype.bytesInitial);
result = runConstructor(rtype.getClazz(), "");
res = runOnInstance(rtype.getClazz(), result.returnValue, "getInteger");
assertEquals(1, ((Integer) res.returnValue).intValue());
res = runOnInstance(rtype.getClazz(), result.returnValue, "getString");
assertEquals("one", (res.returnValue));
rtype.loadNewVersion("002", retrieveRename(theType, theType + "2"));
result = runConstructor(rtype.getClazz(), "");
res = runOnInstance(rtype.getClazz(), result.returnValue, "getInteger");
assertEquals(2, ((Integer) res.returnValue).intValue());
res = runOnInstance(rtype.getClazz(), result.returnValue, "getString");
assertEquals("two", (res.returnValue));
// version 3 no longer sets the string
rtype.loadNewVersion("003", retrieveRename(theType, theType + "3"));
result = runConstructor(rtype.getClazz(), "");
res = runOnInstance(rtype.getClazz(), result.returnValue, "getInteger");
assertEquals(3, ((Integer) res.returnValue).intValue());
res = runOnInstance(rtype.getClazz(), result.returnValue, "getString");
assertNull(res.returnValue);
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class TypeRewriterTests method rewriteWithPrimitiveReturnValues_int.
@Test
public void rewriteWithPrimitiveReturnValues_int() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("data.HelloWorldPrimitive");
ReloadableType rtype = typeRegistry.addType("data.HelloWorldPrimitive", loadBytesForClass("data.HelloWorldPrimitive"));
Result r = runUnguarded(rtype.getClazz(), "getValue");
assertTrue(r.returnValue instanceof Integer);
assertEquals(42, r.returnValue);
rtype.loadNewVersion("000", rtype.bytesInitial);
r = runUnguarded(rtype.getClazz(), "getValue");
assertTrue(r.returnValue instanceof Integer);
assertEquals(42, r.returnValue);
rtype.loadNewVersion("002", retrieveRename("data.HelloWorldPrimitive", "data.HelloWorldPrimitive002"));
r = runUnguarded(rtype.getClazz(), "getValue");
assertTrue(r.returnValue instanceof Integer);
assertEquals(37, r.returnValue);
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class TypeRewriterTests method rewriteStaticWithPrimitiveDoubleSlottersParams.
@Test
public void rewriteStaticWithPrimitiveDoubleSlottersParams() throws Exception {
TypeRegistry typeRegistry = getTypeRegistry("data.HelloWorld");
ReloadableType rtype = typeRegistry.addType("data.HelloWorld", loadBytesForClass("data.HelloWorld"));
Result r = runUnguarded(rtype.getClazz(), "getStaticValueWithPrimitiveDSParams", 3L, "a", 2.0d, true);
assertTrue(r.returnValue instanceof String);
assertEquals("message with inserts 3 and a and 2.0 and true", r.returnValue);
rtype.loadNewVersion("000", rtype.bytesInitial);
r = runUnguarded(rtype.getClazz(), "getStaticValueWithPrimitiveDSParams", 3L, "a", 2.0d, true);
assertTrue(r.returnValue instanceof String);
assertEquals("message with inserts 3 and a and 2.0 and true", r.returnValue);
rtype.loadNewVersion("002", retrieveRename("data.HelloWorld", "data.HelloWorld002"));
r = runUnguarded(rtype.getClazz(), "getStaticValueWithPrimitiveDSParams", 3L, "a", 2.0d, true);
assertTrue(r.returnValue instanceof String);
assertEquals("message with inserts true and 2.0 and a and 3", r.returnValue);
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class TypeRewriterTests method constructorReloading5.
@Test
public void constructorReloading5() throws Exception {
String supertype = "ctors.A";
String subtype = "ctors.B";
TypeRegistry r = getTypeRegistry(supertype + "," + subtype);
ReloadableType rsupertype = r.addType(supertype, loadBytesForClass(supertype));
ReloadableType rsubtype = r.addType(subtype, loadBytesForClass(subtype));
Result res = null;
// Use the code 'untouched'
result = runConstructor(rsubtype.getClazz(), "int", 3);
res = runOnInstance(rsubtype.getClazz(), result.returnValue, "getString");
assertEquals("3", (res.returnValue));
// reload the types
rsubtype.loadNewVersion("000", rsubtype.bytesInitial);
rsupertype.loadNewVersion("000", rsupertype.bytesInitial);
result = runConstructor(rsubtype.getClazz(), "int", 5);
res = runOnInstance(rsubtype.getClazz(), result.returnValue, "getString");
assertEquals("5", (res.returnValue));
// load a new version of the subtype which adjusts the super call
rsubtype.loadNewVersion("001", retrieveRename(subtype, subtype + "2"));
result = runConstructor(rsubtype.getClazz(), "int", 5);
res = runOnInstance(rsubtype.getClazz(), result.returnValue, "getString");
assertEquals("27", (res.returnValue));
}
use of org.springsource.loaded.test.infra.Result in project spring-loaded by spring-projects.
the class TypeRewriterTests method constructorsAndFinalFields.
/**
* Final fields. Final fields are typically inlined at their usage sites, which means in a reloadable scenario they
* can introduce unexpected (but correct) behaviour.
*/
@Test
public void constructorsAndFinalFields() throws Exception {
String caller = "ctors.Finals";
TypeRegistry r = getTypeRegistry(caller);
ReloadableType rcaller = r.addType(caller, loadBytesForClass(caller));
Result res = null;
// Use the code 'untouched'
Object callerInstance = rcaller.getClazz().newInstance();
res = runOnInstance(rcaller.getClazz(), callerInstance, "getValue");
assertEquals("324 abc", res.returnValue.toString());
// Reload the code
rcaller.loadNewVersion("002", retrieveRename(caller, caller + "2"));
// Constants are inlined - that is why getValue() returns the new value for the String
res = runOnInstance(rcaller.getClazz(), callerInstance, "getValue");
assertEquals("324 def", res.returnValue.toString());
// Without changing visibility from final this would cause an IllegalAccessError from the ___init___ method.
// That is because, if the constant hasn't changed value, there will be PUTFIELD for an already set final
// field in the ___init___ that gets run. If the value is changed an entirely different codepath is used.
callerInstance = rcaller.getClazz().newInstance();
}
Aggregations