You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
7.3 KiB
161 lines
7.3 KiB
import java.util.UUID;
|
|
import java.util.Map;
|
|
import java.util.HashMap;
|
|
import java.io.Serializable;
|
|
|
|
/**
|
|
* Test class for Distributed Object Creation System
|
|
*/
|
|
public class DistributedObjectSystemTest {
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println("=== Distributed Object Creation System Test Started ===");
|
|
|
|
// Test 1: Object Factory Test
|
|
testObjectFactory();
|
|
|
|
// Test 2: Object Registry Test
|
|
testObjectRegistry();
|
|
|
|
// Test 3: Object Proxy Test
|
|
testObjectProxy();
|
|
|
|
// Test 4: Complete Flow Test
|
|
testCompleteFlow();
|
|
|
|
System.out.println("=== Distributed Object Creation System Test Ended ===");
|
|
}
|
|
|
|
/**
|
|
* Test Object Factory functionality
|
|
*/
|
|
private static void testObjectFactory() {
|
|
System.out.println("\n--- Object Factory Test ---");
|
|
|
|
// Create default object factory
|
|
DistributedObjectFactory factory = new DefaultObjectFactory();
|
|
|
|
// Test creating different types of objects
|
|
String remoteServiceId = UUID.randomUUID().toString();
|
|
String dataEntityId = UUID.randomUUID().toString();
|
|
String configNodeId = UUID.randomUUID().toString();
|
|
|
|
System.out.println("Creating Remote Service object...");
|
|
DistributedObject remoteService = factory.createObject(ObjectType.REMOTE_SERVICE, remoteServiceId);
|
|
System.out.println("Creating Data Entity object...");
|
|
DistributedObject dataEntity = factory.createObject(ObjectType.DATA_ENTITY, dataEntityId);
|
|
System.out.println("Creating Config Node object...");
|
|
DistributedObject configNode = factory.createObject(ObjectType.CONFIG_NODE, configNodeId);
|
|
|
|
// Verify object creation results
|
|
System.out.println("Remote Service Object ID: " + (remoteService != null ? remoteService.getObjectId() : "null"));
|
|
System.out.println("Data Entity Object ID: " + (dataEntity != null ? dataEntity.getObjectId() : "null"));
|
|
System.out.println("Config Node Object ID: " + (configNode != null ? configNode.getObjectId() : "null"));
|
|
}
|
|
|
|
/**
|
|
* Test Object Registry functionality
|
|
*/
|
|
private static void testObjectRegistry() {
|
|
System.out.println("\n--- Object Registry Test ---");
|
|
|
|
// Get registry instance
|
|
ObjectRegistry registry = ObjectRegistry.getInstance();
|
|
System.out.println("Get Registry Instance: " + (registry != null ? "Success" : "Failed"));
|
|
|
|
// Create test object
|
|
DistributedObjectFactory factory = new DefaultObjectFactory();
|
|
String testId = UUID.randomUUID().toString();
|
|
DistributedObject testObject = factory.createObject(ObjectType.REMOTE_SERVICE, testId);
|
|
|
|
if (testObject != null) {
|
|
// Register object
|
|
String address = "http://localhost:8080/services/" + testId;
|
|
System.out.println("Registering object to address: " + address);
|
|
registry.registerObject(testObject, address);
|
|
|
|
// Query object address
|
|
String retrievedAddress = registry.getObjectAddress(testId);
|
|
System.out.println("Retrieved object address: " + retrievedAddress);
|
|
|
|
// Discover object
|
|
DistributedObject discoveredObject = registry.discoverObject(testId);
|
|
System.out.println("Discover object: " + (discoveredObject != null ? "Success" : "Failed"));
|
|
if (discoveredObject != null) {
|
|
System.out.println("Discovered object ID: " + discoveredObject.getObjectId());
|
|
}
|
|
} else {
|
|
System.out.println("Cannot create test object, skipping registry test");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test Object Proxy functionality
|
|
*/
|
|
private static void testObjectProxy() {
|
|
System.out.println("\n--- Object Proxy Test ---");
|
|
|
|
// Create object proxy
|
|
DistributedObjectFactory factory = new DefaultObjectFactory();
|
|
String remoteAddress = "http://remote-server:9000";
|
|
ObjectProxy proxy = new ObjectProxy(remoteAddress, factory);
|
|
|
|
// Test remote object creation
|
|
System.out.println("Creating remote object through proxy...");
|
|
DistributedObject remoteObject = proxy.createRemoteObject(ObjectType.DATA_ENTITY);
|
|
System.out.println("Remote created object: " + (remoteObject != null ? "Success" : "Failed"));
|
|
if (remoteObject != null) {
|
|
System.out.println("Remote object ID: " + remoteObject.getObjectId());
|
|
|
|
// Test remote object cloning
|
|
System.out.println("Cloning remote object through proxy...");
|
|
DistributedObject clonedObject = proxy.cloneRemoteObject(remoteObject);
|
|
System.out.println("Remote cloned object: " + (clonedObject != null ? "Success" : "Failed"));
|
|
if (clonedObject != null) {
|
|
System.out.println("Cloned object ID: " + clonedObject.getObjectId());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test complete flow
|
|
*/
|
|
private static void testCompleteFlow() {
|
|
System.out.println("\n--- Complete Flow Test ---");
|
|
|
|
// Get registry
|
|
ObjectRegistry registry = ObjectRegistry.getInstance();
|
|
if (registry == null) {
|
|
System.out.println("Cannot get registry instance, skipping complete flow test");
|
|
return;
|
|
}
|
|
|
|
// Create object factory
|
|
DistributedObjectFactory factory = new DefaultObjectFactory();
|
|
|
|
// Create and register different types of objects
|
|
String serviceId = "service-" + UUID.randomUUID().toString();
|
|
String entityId = "entity-" + UUID.randomUUID().toString();
|
|
String configId = "config-" + UUID.randomUUID().toString();
|
|
|
|
DistributedObject service = factory.createObject(ObjectType.REMOTE_SERVICE, serviceId);
|
|
DistributedObject entity = factory.createObject(ObjectType.DATA_ENTITY, entityId);
|
|
DistributedObject config = factory.createObject(ObjectType.CONFIG_NODE, configId);
|
|
|
|
if (service != null) registry.registerObject(service, "http://server1:8080/" + serviceId);
|
|
if (entity != null) registry.registerObject(entity, "http://server2:8080/" + entityId);
|
|
if (config != null) registry.registerObject(config, "http://server3:8080/" + configId);
|
|
|
|
System.out.println("Registered three types of objects");
|
|
|
|
// Discover objects through registry
|
|
System.out.println("Discover service object: " + (registry.discoverObject(serviceId) != null ? "Success" : "Failed"));
|
|
System.out.println("Discover entity object: " + (registry.discoverObject(entityId) != null ? "Success" : "Failed"));
|
|
System.out.println("Discover config object: " + (registry.discoverObject(configId) != null ? "Success" : "Failed"));
|
|
|
|
// Test proxy functionality
|
|
ObjectProxy proxy = new ObjectProxy("http://load-balancer:8080", factory);
|
|
DistributedObject remoteService = proxy.createRemoteObject(ObjectType.REMOTE_SERVICE);
|
|
System.out.println("Create remote service through proxy: " + (remoteService != null ? "Success" : "Failed"));
|
|
}
|
|
} |