Author: bdonlan
Date: 2005-06-19 20:28:22 -0400 (Sun, 19 Jun 2005)
New Revision: 775

Added:
   trunk/java/src/org/haverdev/haver/server/LobbyAdaptor.java
   trunk/java/test/org/
   trunk/java/test/org/haverdev/
   trunk/java/test/org/haverdev/common/
   trunk/java/test/org/haverdev/common/HaverEncodingTest.java
Modified:
   trunk/java/nbproject/build-impl.xml
   trunk/java/nbproject/project.properties
   trunk/java/src/org/haverdev/common/HaverEncoding.java
   trunk/java/src/org/haverdev/haver/server/Entity.java
   trunk/java/src/org/haverdev/haver/server/Main.java
   trunk/java/src/org/haverdev/haver/server/ServerContext.java
Log:
Exclude &lobby from itself; add tests for HaverEncoding

Modified: trunk/java/nbproject/build-impl.xml
===================================================================
--- trunk/java/nbproject/build-impl.xml 2005-06-19 17:43:16 UTC (rev 774)
+++ trunk/java/nbproject/build-impl.xml 2005-06-20 00:28:22 UTC (rev 775)
@@ -125,6 +125,7 @@
             <attribute name="includes" default="**/*Test.java"/>
             <sequential>
                 <junit showoutput="true" fork="true" dir="${basedir}" 
failureproperty="tests.failed" errorproperty="tests.failed">
+                    <jvmarg value="-ea"/>
                     <batchtest todir="${build.test.results.dir}">
                         <fileset dir="${test.src.dir}" includes="@{includes}"/>
                     </batchtest>

Modified: trunk/java/nbproject/project.properties
===================================================================
--- trunk/java/nbproject/project.properties     2005-06-19 17:43:16 UTC (rev 
774)
+++ trunk/java/nbproject/project.properties     2005-06-20 00:28:22 UTC (rev 
775)
@@ -46,7 +46,7 @@
 # Space-separated list of JVM arguments used when running the project
 # (you may also define separate properties like run-sys-prop.name=value 
instead of -Dname=value
 # or test-sys-prop.name=value to set system properties for unit tests):
-run.jvmargs=
+run.jvmargs=-ea
 run.test.classpath=\
     ${javac.test.classpath}:\
     ${build.test.classes.dir}

Modified: trunk/java/src/org/haverdev/common/HaverEncoding.java
===================================================================
--- trunk/java/src/org/haverdev/common/HaverEncoding.java       2005-06-19 
17:43:16 UTC (rev 774)
+++ trunk/java/src/org/haverdev/common/HaverEncoding.java       2005-06-20 
00:28:22 UTC (rev 775)
@@ -17,7 +17,7 @@
  */
 public final class HaverEncoding {
     
-    private HaverEncoding() {}
+    public HaverEncoding() {}
     
     /**
      * Reverses Haver escaping.
@@ -30,7 +30,7 @@
         int pin = 0;
         int nextEsc = -1;
         while (-1 != (nextEsc = val.indexOf("\033", pin))) {
-            out.append(val.substring(0, nextEsc - 1));
+            out.append(val.substring(pin, nextEsc));
             switch (val.charAt(nextEsc + 1)) {
                 case 'r':
                     out.append('\r');
@@ -101,12 +101,19 @@
         nl = inLine.indexOf("\n");
         if (nl != -1)
             inLine = inLine.substring(0, nl);
+     
+        ArrayList args = new ArrayList();
+        while (true) {
+            int end = inLine.indexOf('\t');
+            if (end == -1) {
+                args.add(unescape(inLine));
+                break;
+            }
+            args.add(unescape(inLine.substring(0, end)));
+            inLine = inLine.substring(end + 1);
+        }
         
-        String[] args = splitString(inLine, "\t");
-        for (int i = 0; i < args.length; i++) {
-            args[i] = unescape(args[i]);
-        }
-        return args;
+        return (String[]) args.toArray(new String[args.size()]);
     }
     
     /**

Modified: trunk/java/src/org/haverdev/haver/server/Entity.java
===================================================================
--- trunk/java/src/org/haverdev/haver/server/Entity.java        2005-06-19 
17:43:16 UTC (rev 774)
+++ trunk/java/src/org/haverdev/haver/server/Entity.java        2005-06-20 
00:28:22 UTC (rev 775)
@@ -22,6 +22,8 @@
      */
     public String getName();
     
+    public void setName(String name);
+    
     public boolean isPersistant();
     
     public ServerContext getServerContext();

Added: trunk/java/src/org/haverdev/haver/server/LobbyAdaptor.java
===================================================================
--- trunk/java/src/org/haverdev/haver/server/LobbyAdaptor.java  2005-06-19 
17:43:16 UTC (rev 774)
+++ trunk/java/src/org/haverdev/haver/server/LobbyAdaptor.java  2005-06-20 
00:28:22 UTC (rev 775)
@@ -0,0 +1,95 @@
+/*
+ * LobbyAdaptor.java
+ *
+ * Created on June 19, 2005, 1:23 PM
+ *
+ * To change this template, choose Tools | Options and locate the template 
under
+ * the Source Creation and Management node. Right-click the template and choose
+ * Open. You can then make changes to the template in the Source Editor.
+ */
+
+package org.haverdev.haver.server;
+
+/**
+ *
+ * @author bdonlan
+ */
+public final class LobbyAdaptor extends EntityBase implements Channel {
+    
+    private Channel delegate;
+    
+    /** Creates a new instance of LobbyAdaptor */
+    public LobbyAdaptor(ServerContext ctx, Channel delegate) {
+        super(ctx);
+        this.delegate = delegate;
+        delegate.setName("&lobby");
+        super.setName("&lobby");
+    }
+
+    public void setName(String name) {
+        throw new UnsupportedOperationException("&lobby must always be 
&lobby");
+    }
+    
+    public Entity[] filterContents(String namespace) {
+        return delegate.filterContents(namespace);
+    }
+
+    public String[] getNames(String namespace) {
+        return delegate.getNames(namespace);
+    }
+
+    public boolean contains(Entity what) {
+        if (what.equals(this))
+            return true;
+        return delegate.contains(what);
+    }
+
+    public void distributeJoin(Entity who) throws 
org.haverdev.haver.server.exceptions.PropagatedException {
+        delegate.distributeJoin(who);
+    }
+
+    public void distributePart(Entity who) throws 
org.haverdev.haver.server.exceptions.PropagatedException {
+        delegate.distributePart(who);
+    }
+
+    public void register(Entity e) throws 
org.haverdev.haver.server.exceptions.PropagatedException {
+        if (equals(e))
+            return;
+        delegate.register(e);
+    }
+
+    public void unregister(Entity e) {
+        if (equals(e))
+            return;
+        delegate.unregister(e);
+    }
+
+    public void distributePublicMessage(Entity from, String[] args) throws 
org.haverdev.haver.server.exceptions.PropagatedException {
+        delegate.distributePublicMessage(from, args);
+    }
+
+    public boolean contains(String namespace, String name) throws 
org.haverdev.haver.server.exceptions.PropagatedException {
+        if (namespace.equals("channel") && name.equals("&lobby"))
+            return true;
+        return delegate.contains(namespace, name);
+    }
+
+    public Entity[] getContents() {
+        return delegate.getContents();
+    }
+
+    public String getNamespace() {
+        return delegate.getNamespace();
+    }
+
+    public Entity lookup(String namespace, String name) throws 
org.haverdev.haver.server.exceptions.PropagatedException {
+        if (namespace.equals("channel") && name.equals("&lobby"))
+            return this;
+        return delegate.lookup(namespace, name);
+    }
+
+    public User[] quitListeners() {
+        return delegate.quitListeners();
+    }
+    
+}


Property changes on: trunk/java/src/org/haverdev/haver/server/LobbyAdaptor.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/java/src/org/haverdev/haver/server/Main.java
===================================================================
--- trunk/java/src/org/haverdev/haver/server/Main.java  2005-06-19 17:43:16 UTC 
(rev 774)
+++ trunk/java/src/org/haverdev/haver/server/Main.java  2005-06-20 00:28:22 UTC 
(rev 775)
@@ -34,7 +34,7 @@
         Class[] proto = { ServerContext.class, String.class };
         Object[] args = { ctx, "&lobby" };
         Constructor cons = c.getConstructor(proto);
-        return (Channel)cons.newInstance(args);
+        return new LobbyAdaptor(ctx, (Channel)cons.newInstance(args));
     }
     
     void initPlugin(String what) throws Throwable {
@@ -82,12 +82,17 @@
         // Configure the logger
         PropertyConfigurator.configure(System.getProperties());
         
+        ctx = new ServerContext(null, null, null);
+        ctx.setHostname(System.getProperty("haver.hostname"));
+        if (ctx.getHostname() == null)
+            throw new IllegalArgumentException("haver.hostname is not set");
+        ctx.setStore(
+                initStore(System.getProperty("haver.UserStore", 
"org.haverdev.haver.server.DefaultUserStore"))
+                );
+        ctx.setLobby(initLobby(System.getProperty("haver.Lobby")));
         Channel lobby = initLobby(System.getProperty("haver.Lobby"));
         UserStore store = initStore(System.getProperty("haver.UserStore", 
"org.haverdev.haver.server.DefaultUserStore"));
         String hostname = System.getProperty("haver.hostname");
-        if (hostname == null)
-            throw new IllegalArgumentException("haver.hostname is not set");
-        ctx = new ServerContext(store, lobby, hostname);
         
         String[] plugins = 
HaverEncoding.optionSplit(System.getProperty("haver.Plugins"));
         for (int i = 0; i < plugins.length; i++) {

Modified: trunk/java/src/org/haverdev/haver/server/ServerContext.java
===================================================================
--- trunk/java/src/org/haverdev/haver/server/ServerContext.java 2005-06-19 
17:43:16 UTC (rev 774)
+++ trunk/java/src/org/haverdev/haver/server/ServerContext.java 2005-06-20 
00:28:22 UTC (rev 775)
@@ -19,6 +19,7 @@
     UserStore store;
     Channel lobby;
     String hostname;
+    boolean sealed = false;
     
     /** Creates a new instance of ServerContext */
     public ServerContext(UserStore store, Channel lobby, String hostname) {
@@ -27,16 +28,39 @@
         this.hostname = hostname;
     }
     
+    public void seal() {
+        sealed = true;
+    }
+    
+    void checkSeal() {
+        if (sealed)
+            throw new IllegalStateException("Can't mutate a sealed server 
context");
+    }
+    
     public UserStore getStore() {
         return store;
     }
     
+    public void setStore(UserStore store) {
+        checkSeal();
+        this.store = store;
+    }
+    
     public Channel getLobby() {
         return lobby;
     }
     
+    public void setLobby(Channel lobby) {
+        checkSeal();
+        this.lobby = lobby;
+    }
+    
     public String getHostname() {
         return hostname;
     }
     
+    public void setHostname(String hostname) {
+        checkSeal();
+        this.hostname = hostname;
+    }
 }

Added: trunk/java/test/org/haverdev/common/HaverEncodingTest.java
===================================================================
--- trunk/java/test/org/haverdev/common/HaverEncodingTest.java  2005-06-19 
17:43:16 UTC (rev 774)
+++ trunk/java/test/org/haverdev/common/HaverEncodingTest.java  2005-06-20 
00:28:22 UTC (rev 775)
@@ -0,0 +1,161 @@
+/*
+ * HaverEncodingTest.java
+ * JUnit based test
+ *
+ * Created on June 19, 2005, 7:36 PM
+ */
+
+package org.haverdev.common;
+
+import junit.framework.*;
+import java.util.*;
+import java.lang.reflect.*;
+
+/**
+ *
+ * @author bdonlan
+ */
+public class HaverEncodingTest extends TestCase {
+    
+    HaverEncoding he = new HaverEncoding();
+    
+    public HaverEncodingTest(String testName) {
+        super(testName);
+    }
+
+    protected void setUp() throws Exception {
+    }
+
+    protected void tearDown() throws Exception {
+    }
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(HaverEncodingTest.class);
+        
+        return suite;
+    }
+
+    public static void main(String[] args) {
+        TestResult r = new TestResult();
+        Test t = suite();
+        r.startTest(t);
+        t.run(r);
+    }
+    
+    /**
+     * Test of unescape method, of class org.haverdev.common.HaverEncoding.
+     */
+    public void testUnescape() {
+        System.out.println("testUnescape");
+        
+        assert he.unescape("").equals("");
+        assert he.unescape("foo").equals("foo");
+        
+        assert he.unescape("\033r").equals("\r");
+        assert he.unescape("\033n").equals("\n");
+        assert he.unescape("\033e").equals("\033");
+        assert he.unescape("\033t").equals("\t");
+        
+        assert he.unescape("\033r\033r").equals("\r\r");
+        assert he.unescape("\033r \033r").equals("\r \r");
+    }
+
+    /**
+     * Test of escape method, of class org.haverdev.common.HaverEncoding.
+     */
+    public void testEscape() {
+        System.out.println("testEscape");
+        
+        assert he.escape("").equals("");
+        assert he.escape("foo").equals("foo");
+        
+        assert he.escape("\r").equals("\033r");
+        assert he.escape("\n").equals("\033n");
+        assert he.escape("\t").equals("\033t");
+        assert he.escape("\033").equals("\033e");
+        
+        assert he.escape("\r\r").equals("\033r\033r");
+        assert he.escape("\r \r").equals("\033r \033r");
+    }
+
+    public boolean arrayEquals(Object a, Object b) {
+        int al = Array.getLength(a);
+        int bl = Array.getLength(b);
+        if (al != bl)
+            return false;
+        for (int i = 0; i < al; i++) {
+            Object ao = Array.get(a, i);
+            Object bo = Array.get(b, i);
+            if (ao == bo) continue;
+            if (ao == null) return false;
+            if (!ao.equals(bo)) return false;
+        }
+        return true;
+    }
+    
+    void decodeT(String line, String[] expected) {
+        assert arrayEquals(he.decodeLine(line), expected);
+    }
+    
+    /**
+     * Test of decodeLine method, of class org.haverdev.common.HaverEncoding.
+     */
+    public void testDecodeLine() {
+        System.out.println("testDecodeLine");
+        
+        decodeT("", new String[] { "" });
+        decodeT("FOO", new String[] { "FOO" });
+        decodeT("FOO\tBAR", new String[] { "FOO", "BAR" });
+        decodeT("FOO\t", new String[] { "FOO", "" });
+        decodeT("FOO\t\tBAR", new String[] { "FOO", "", "BAR" });
+        decodeT("FOO\t\t", new String[] {"FOO", "", ""});
+        decodeT("\tFOO", new String[] {"", "FOO" });
+        decodeT("\t", new String[] { "", "" });
+        decodeT("\t\tFOO", new String[] {"", "", "FOO" });
+        
+        decodeT("FOO\t\033tbar", new String[] { "FOO", "\tbar" });
+    }
+
+    void encodeT(String line, String[] expected) {
+        String res = he.encodeLine(expected);
+        assert res.equals(line + "\r\n");
+    }
+    
+    /**
+     * Test of encodeLine method, of class org.haverdev.common.HaverEncoding.
+     */
+    public void testEncodeLine() {
+        System.out.println("testEncodeLine");
+        
+        encodeT("", new String[] { "" });
+        encodeT("FOO", new String[] { "FOO" });
+        encodeT("FOO\tBAR", new String[] { "FOO", "BAR" });
+        encodeT("FOO\t", new String[] { "FOO", "" });
+        encodeT("FOO\t\tBAR", new String[] { "FOO", "", "BAR" });
+        encodeT("FOO\t\t", new String[] {"FOO", "", ""});
+        encodeT("\tFOO", new String[] {"", "FOO" });
+        encodeT("\t", new String[] { "", "" });
+        encodeT("\t\tFOO", new String[] {"", "", "FOO" });
+        
+        encodeT("FOO\t\033tbar", new String[] { "FOO", "\tbar" });
+    }
+
+    /**
+     * Test of splitString method, of class org.haverdev.common.HaverEncoding.
+     */
+    public void testSplitString() {
+        System.out.println("testSplitString");
+        
+        // TODO add test code
+    }
+
+    /**
+     * Test of optionSplit method, of class org.haverdev.common.HaverEncoding.
+     */
+    public void testOptionSplit() {
+        System.out.println("testOptionSplit");
+        
+        // TODO add test code
+    }
+    
+}


Property changes on: trunk/java/test/org/haverdev/common/HaverEncodingTest.java
___________________________________________________________________
Name: svn:eol-style
   + native


Reply via email to