public static void main(String[] args) throws Exception {
ExternalizableStrategy.setStrategy(Subsystem.REFLECTIVE_SERIALIZATION);
// create objects
Agent writeAgent = new Agent("reflectiveAgent", "reflectiveAddress", 0, "reflectiveEndpoint", "reflectiveToken");
ResourceType writeResourceType = new ResourceType();
writeResourceType.setName("reflectiveType");
writeResourceType.setPlugin("reflectivePlugin");
writeResourceType.setId(7);
Resource writeParentResource = new Resource();
writeParentResource.setId(11);
writeParentResource.setName("reflectiveParentResource");
writeParentResource.setResourceKey("reflectiveParentKey");
Resource writeResource = new Resource();
writeResource.setId(42);
writeResource.setName("reflectiveResource");
writeResource.setResourceKey("reflectiveKey");
// setup relationships
writeResource.setAgent(writeAgent);
writeResource.setResourceType(writeResourceType);
writeResource.setParentResource(writeParentResource);
System.out.println("BEFORE");
System.out.println(writeResource.toString());
System.out.println("BEFORE");
String tempDir = System.getProperty("java.io.tmpdir");
File tempFile = new File(tempDir, "entitySerializerTest.txt");
FileOutputStream fos = new FileOutputStream(tempFile);
try {
ObjectOutput output = new ObjectOutputStream(fos);
try {
writeExternalRemote(writeResource, output);
} finally {
output.close();
}
} finally {
fos.close();
}
Resource readResource = new Resource();
FileInputStream fis = new FileInputStream(tempFile);
try {
ObjectInput ois = new ObjectInputStream(fis);
try {
readExternalRemote(readResource, ois);
} finally {
ois.close();
}
} finally {
fis.close();
}
// quick verification
System.out.println("AFTER");
System.out.println(readResource.toString());
System.out.println("AFTER");
// deeper verification
boolean equalsResource = writeResource.equals(readResource);
boolean equalsParentResource = writeParentResource.equals(readResource.getParentResource());
boolean equalsResourceType = writeResourceType.equals(readResource.getResourceType());
boolean equalsAgent = writeAgent.equals(readResource.getAgent());
System.out.println("equalsResource: " + equalsResource);
System.out.println("equalsParentResource: " + equalsParentResource);
System.out.println("equalsResourceType: " + equalsResourceType);
System.out.println("equalsAgent: " + equalsAgent);