1 package com.tirsen.nanning.samples.rmi; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 import java.util.HashSet ; 8 import java.security.PrivilegedExceptionAction ; 9 import java.security.AccessController ; 10 import java.security.PrivilegedActionException ; 11 12 import javax.security.auth.Subject ; 13 14 import com.tirsen.nanning.Aspects; 15 import com.tirsen.nanning.attribute.AbstractAttributesTest; 16 import com.tirsen.nanning.config.AspectSystem; 17 import com.tirsen.nanning.config.FindTargetMixinAspect; 18 import com.tirsen.nanning.samples.prevayler.CurrentPrevayler; 19 import com.tirsen.nanning.samples.prevayler.MySystem; 20 import com.tirsen.nanning.samples.prevayler.PrevaylerAspect; 21 import org.prevayler.implementation.SnapshotPrevayler; 22 import com.tirsen.nanning.samples.prevayler.MyObject; 23 24 public class RemoteCallServerTest extends AbstractAttributesTest { 25 private AspectSystem serverAspectSystem; 26 private File prevaylerDir; 27 private int port = 12346; 28 private SnapshotPrevayler prevayler; 29 private SocketRemoteCallServer remoteCallServer; 30 private RemoteMarshaller clientMarshaller; 31 32 protected void setUp() throws Exception { 33 super.setUp(); 34 prevaylerDir = File.createTempFile("test", ""); 35 prevaylerDir.delete(); 36 prevaylerDir.deleteOnExit(); 37 prevaylerDir.mkdirs(); 38 39 serverAspectSystem = new AspectSystem(); 40 serverAspectSystem.addAspect(new FindTargetMixinAspect()); 41 serverAspectSystem.addAspect(new PrevaylerAspect()); 42 43 clientMarshaller = RemoteMarshaller.createClientSideMarshaller(); 44 45 Aspects.setContextAspectFactory(serverAspectSystem); 47 prevayler = new SnapshotPrevayler(serverAspectSystem.newInstance(MySystem.class), 48 prevaylerDir.getAbsolutePath()); 49 50 remoteCallServer = new SocketRemoteCallServer(); 51 remoteCallServer.setPort(port); 52 53 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 54 public void run() { 55 remoteCallServer.start(); 57 remoteCallServer.setAspectFactory(serverAspectSystem); 58 } 59 }); 60 } 61 62 protected void tearDown() throws Exception { 63 super.tearDown(); 64 remoteCallServer.stop(); 65 } 66 67 public void testAuthenticatedCall() throws PrivilegedActionException { 68 remoteCallServer.bind("MyStatelessService", serverAspectSystem.newInstance(MyStatelessService.class)); 70 71 Subject.doAs(new Subject (false, new HashSet (), new HashSet (), new HashSet ()), new PrivilegedExceptionAction () { 73 public Object run() throws Exception { 74 Subject subject = Subject.getSubject(AccessController.getContext()); 75 subject.getPrincipals().add(new MyPrincipal("expectedUserName")); 76 77 Naming naming = new Naming(clientMarshaller, new SocketConnectionManager("localhost", port)); 78 MyStatelessService myService = (MyStatelessService) naming.lookup("MyStatelessService"); 79 80 myService.authenticatedCall("expectedUserName"); 81 82 return null; 83 } 84 }); 85 } 86 87 public void testStatelessRemoteCall() throws IOException , ClassNotFoundException { 88 remoteCallServer.bind("MyStatelessService", serverAspectSystem.newInstance(MyStatelessService.class)); 90 91 Naming naming = new Naming(clientMarshaller, new SocketConnectionManager("localhost", port)); 93 MyStatelessService myService = (MyStatelessService) naming.lookup("MyStatelessService"); 94 95 { 96 myService.createObject("attributeValue"); 97 } 98 99 { 100 MyObject myObject = myService.getMyObject(); 101 String value = myObject.getValue(); 102 assertEquals("attributeValue", value); 103 } 104 105 { 106 Collection col = myService.getAllObjects(); 107 assertEquals(1, col.size()); 108 MyObject[] myObjects = (MyObject[])col.toArray(new MyObject[1]); 109 MyObject myObject = myObjects[0]; 110 String value = myObject.getValue(); 111 assertEquals("attributeValue", value); 112 } 113 114 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 116 public void run() { 117 MySystem system = (MySystem) CurrentPrevayler.getSystem(); 118 119 assertNotNull("object not created on server side", system.getMyObject()); 121 assertEquals("attribute wrong value", "attributeValue", system.getMyObject().getValue()); 122 } 123 }); 124 } 125 126 public void testStatefulRemoteCall() throws IOException , ClassNotFoundException { 127 remoteCallServer.bind("MyStatefulService", serverAspectSystem.newInstance(MyStatefulService.class)); 129 130 MyStatefulService statefulService = (MyStatefulService) new Naming(clientMarshaller, new SocketConnectionManager("localhost", port)).lookup("MyStatefulService"); 132 assertNull(statefulService.value()); 133 statefulService.modify("value"); 134 assertEquals("value", statefulService.value()); 135 } 136 } 137 | Popular Tags |