server.setHandler(new Handler() { public void handle(DataInputStream in, DataOutputStream out) throws IOException { while (true) { Header h= Header.readFrom(in);   if (Header.DEFAULT_MAGIC_NUMBER != h.getMagicNumber()) { out.writeUTF("-ERR MAGIC"); out.flush(); break; }  System.out.print(h.getMessageType() + " => "); Delegate delegate= h.getMessageType().delegateFrom(in);  try { out.writeUTF("+OK " + delegate.getClass().getName() + ": " + Serializer.representationOf(delegate.invoke())); } catch (Exception e) { e.printStackTrace(System.err); out.writeUTF("-ERR " + e.getClass().getName()); }  out.flush(); } } });
server.setHandler(new ServerHandler());
    @BeforeClass public static void createServerThread() throws Exception {        InetAddress addr= InetAddress.getLocalHost();        // Create server, set handler and start it!        server= new ServerThread(new ServerSocket(TEST_PORT, 1, addr));        server.setHandler(new Handler() {            public void handle(DataInputStream in, DataOutputStream out) throws IOException {                while (true) {                    Header h= Header.readFrom(in);                    // Verify magic number                    if (Header.DEFAULT_MAGIC_NUMBER != h.getMagicNumber()) {                        out.writeUTF("-ERR MAGIC");                        out.flush();                        break;                    }                                        System.out.print(h.getMessageType() + " => ");                    Delegate delegate= h.getMessageType().delegateFrom(in);                                        try {                        out.writeUTF("+OK " + delegate.getClass().getName() + ": " + Serializer.representationOf(delegate.invoke()));                    } catch (Exception e) {                        e.printStackTrace(System.err);                        out.writeUTF("-ERR " + e.getClass().getName());                    }                                        out.flush();                }            }        });        server.start();                // Set up client socket        client= new Socket(addr, TEST_PORT);        // Set Mock context as initial        NamingManager.setInitialContextFactoryBuilder(new InitialContextFactoryBuilder() {            public InitialContextFactory createInitialContextFactory(Hashtable<?,?> environment) {                return new MockContextFactory();            }        });                // Bind test objects        InitialContext ctx= new InitialContext();        ctx.bind("test/DateObject", new Date(1123681953000L));        ctx.bind("test/PersonObject", new Person());        ctx.bind("test/Interface", Proxy.newProxyInstance(            ITest.class.getClassLoader(),            new Class[] { ITest.class },            new InvocationHandler() {                public Object invoke(Object proxy, Method method, Object[] args) {                    return null;                }            }        ));            }