Mercurial > hg > Members > shoshi > TreeCMSv1
changeset 37:78e9b96ef04a
added common test
line wrap: on
line diff
--- a/src/treecms/proto/api/Node.java Mon Dec 27 21:08:43 2010 +0900 +++ b/src/treecms/proto/api/Node.java Wed Dec 29 03:13:18 2010 +0900 @@ -1,6 +1,7 @@ package treecms.proto.api; import java.util.List; +import java.util.Set; import java.util.Iterator; @@ -22,6 +23,7 @@ public String getAttribute(String _attr); public void setAttribute(String _attr,String _value); + public Set<String> getAttributeKeys(); public void up(Node _child); public void down(Node _child); @@ -30,4 +32,5 @@ public NodeID getID(); public Node cloneNode(); public Node createNode(); + public Link createLink(); } \ No newline at end of file
--- a/src/treecms/proto/api/NodeID.java Mon Dec 27 21:08:43 2010 +0900 +++ b/src/treecms/proto/api/NodeID.java Wed Dec 29 03:13:18 2010 +0900 @@ -2,6 +2,7 @@ public interface NodeID { + public NodeID create(); public NodeID update(); public NodeID getTip(); public String getUUID();
--- a/src/treecms/proto/id/IncrementalNodeID.java Mon Dec 27 21:08:43 2010 +0900 +++ b/src/treecms/proto/id/IncrementalNodeID.java Wed Dec 29 03:13:18 2010 +0900 @@ -24,6 +24,12 @@ } @Override + public NodeID create() + { + return new IncrementalNodeID(); + } + + @Override public NodeID update() { IncrementalNodeID id = new IncrementalNodeID(m_inheritedID);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/id/test/IncrementalNodeIDTest1.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,109 @@ +package treecms.proto.id.test; + +/** + * IncrementalNodeIDTest1 + * + * testIDstartsWithZero() + * the id must be start from zero. + * + * testUpdateIDFromLatestID() + * check that id able to update correctly from latest node id. + * + * testUpdateIDFromOldID() + * check that id able to update correctly from old node id. + * + * testMultiThreadUpdateID() + * execute concurrent update using shared object. + */ + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import org.junit.Test; +import org.junit.Assert; +import org.junit.runner.JUnitCore; +import treecms.proto.api.NodeID; +import treecms.proto.id.IncrementalNodeID; +import treecms.proto.test.NodeIDTest; + +public class IncrementalNodeIDTest1 extends NodeIDTest +{ + public static void main(String _args[]) + { + JUnitCore.main(IncrementalNodeIDTest1.class.getName()); + } + + public IncrementalNodeIDTest1() + { + super(new IncrementalNodeID()); + } + + @Test + public void testIDstartsWithZero() + { + NodeID id = new IncrementalNodeID(); + long ver = Long.parseLong(id.toString().split("@")[1]); + Assert.assertEquals(ver,0); + } + + @Test + public void testUpdateIDFromLatestID() + { + int count = 100; + NodeID id = new IncrementalNodeID(); + + for(int i = 1;i <= count;i ++){ + id = id.update(); + long ver = Long.parseLong(id.toString().split("@")[1]); + Assert.assertEquals(ver,i); + } + } + + @Test + public void testUpdateIDFromOldID() + { + int count = 100; + NodeID id0 = new IncrementalNodeID(); + + for(int i = 1;i <= count;i ++){ + NodeID id = id0.update(); + long ver = Long.parseLong(id.toString().split("@")[1]); + Assert.assertEquals(ver,i); + } + } + + @Test + public void testMultiThreadedUpdateID() + { + final int threads = 10; //number of threads + final int count = 100; //modification count + ExecutorService service = Executors.newFixedThreadPool(threads); + + final NodeID id = new IncrementalNodeID(); + + for(int i = 0;i < threads;i ++){ + service.execute(new Runnable(){ + @Override + public void run() + { + for(int j = 0;j < count;j ++){ + id.update(); + } + } + }); + } + + service.shutdown(); + + try { + service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + //check id + NodeID tip = id.update(); + long ver = Long.parseLong(tip.toString().split("@")[1]); + Assert.assertEquals(ver,threads*count + 1); + } +}
--- a/src/treecms/proto/simple/SimpleBrowser.java Mon Dec 27 21:08:43 2010 +0900 +++ b/src/treecms/proto/simple/SimpleBrowser.java Wed Dec 29 03:13:18 2010 +0900 @@ -1,23 +1,32 @@ package treecms.proto.simple; -import java.util.Hashtable; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import treecms.proto.api.*; import treecms.proto.id.IncrementalNodeID; public class SimpleBrowser implements Browser { + public static void main(String _args[]) + { + SimpleBrowser browser = SimpleBrowser.getSingleton(); + Node node = browser.useContents(); + Node cloned = node.cloneNode(); + + System.out.println(cloned.getID().toString()); + System.out.println(browser.useContents().getID().toString()); + } + private static final SimpleBrowser m_instance = new SimpleBrowser(); - private Hashtable<NodeID,Node> m_table; + private Map<String,Node> m_table; private SimpleNode m_root; private SimpleBrowser() { - m_table = new Hashtable<NodeID,Node>(); + m_table = new ConcurrentHashMap<String,Node>(); m_root = createNode(); - - m_table.put(m_root.getID(),m_root); } public static SimpleBrowser getSingleton() @@ -27,23 +36,41 @@ public void put(NodeID _id,Node _node) { - m_table.put(_id,_node); + m_table.put(_id.toString(),_node); } public Node get(NodeID _id) { - return m_table.get(_id); + return m_table.get(_id.toString()); } public SimpleNode createNode() { SimpleNode node = new SimpleNode(new IncrementalNodeID()); + m_table.put(node.getID().toString(),node); return node; } + public SimpleNode cloneNode(SimpleNode _target) + { + SimpleNode cloned = new SimpleNode(_target.getID().update()); + cloned.setClassName(_target.getClassName()); + cloned.setTitle(_target.getTitle()); + + for(String _key : _target.getAttributeKeys()){ + cloned.setAttribute(_key,_target.getAttribute(_key)); + } + + cloned.addChildren(_target.getChildren()); + + m_table.put(cloned.getID().toString(),cloned); + return cloned; + } + public SimpleLink createLink(Node _target) { - SimpleLink link = new SimpleLink(_target); + SimpleLink link = new SimpleLink(new IncrementalNodeID(),_target); + m_table.put(link.getID().toString(),link); return link; } @@ -51,6 +78,6 @@ public Node useContents() { NodeID tip = m_root.getID().getTip(); - return m_table.get(tip); + return m_table.get(tip.toString()); } }
--- a/src/treecms/proto/simple/SimpleLink.java Mon Dec 27 21:08:43 2010 +0900 +++ b/src/treecms/proto/simple/SimpleLink.java Wed Dec 29 03:13:18 2010 +0900 @@ -1,61 +1,70 @@ package treecms.proto.simple; -import java.util.Collections; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; +import java.util.Set; import treecms.proto.api.Link; import treecms.proto.api.Node; import treecms.proto.api.NodeID; -import treecms.proto.id.IncrementalNodeID; public class SimpleLink implements Link { private NodeID m_id; private Node m_target; - public SimpleLink(Node _target) + SimpleLink(NodeID _id,Node _target) + { + m_id = _id; + m_target = _target; + } + + private Node getTip() { - m_id = new IncrementalNodeID(); - m_target = _target; - SimpleNodeTable.getInstance().put(m_id.toString(),this); + NodeID tip = m_target.getID().getTip(); + return SimpleBrowser.getSingleton().get(tip); + } + + @Override + public Set<String> getAttributeKeys() + { + return getTip().getAttributeKeys(); } @Override public Node getNode() { - return m_target; + return getTip(); } @Override - public void setNode(Node link) + public void setNode(Node _link) { - m_target = link; + m_target = _link; } @Override - public Node addChild(Node child) + public Node addChild(Node _child) { - return child; + return getTip().addChild(_child); } @Override public Node cloneNode() { - return new SimpleLink(m_target); + return getTip().cloneNode(); } @Override public List<Node> getChildren() { - return null; + return getTip().getChildren(); } @Override public String getClassName() { - return m_target.getClassName(); + return getTip().getClassName(); } @Override @@ -67,72 +76,90 @@ @Override public String getTitle() { - return m_target.getTitle(); + return getTip().getTitle(); } @Override - public boolean isChild(Node child) + public boolean isChild(Node _child) { - return m_node.isChild(_child); + return getTip().isChild(_child); } @Override public Iterator<Node> iterator() { - return Collections.EMPTY_LIST; + return getTip().iterator(); } @Override - public boolean removeChild(Node child) + public boolean removeChild(Node _child) { - return false; + return getTip().removeChild(_child); } @Override public void setClassName(String _class) { - m_target.setClassName(_class); + getTip().setClassName(_class); } @Override public void setTitle(String _title) { - m_target.setTitle(_title); + getTip().setTitle(_title); } @Override - public void addChildren(List<Node> child) + public void addChildren(List<Node> _child) { - return; + getTip().addChildren(_child); } @Override - public void down(Node child) + public void down(Node _child) { - return; + getTip().down(_child); } @Override - public void replace(Node target, Node newChild) + public void replace(Node _target, Node _newChild) { - return; + getTip().replace(_target,_newChild); } @Override - public void up(Node child) + public void up(Node _child) { - return; + getTip().up(_child); } @Override public void clearChildren() { - return; + getTip().clearChildren(); } - + + @Override + public Link createLink() + { + return getTip().createLink(); + } + @Override public Node createNode() { - return new SimpleNode(); + return SimpleBrowser.getSingleton().createNode(); + } + + @Override + public String getAttribute(String _attr) + { + return getTip().getAttribute(_attr); + } + + @Override + public void setAttribute(String _attr,String _value) + { + getTip().setAttribute(_attr,_value); } }
--- a/src/treecms/proto/simple/SimpleNode.java Mon Dec 27 21:08:43 2010 +0900 +++ b/src/treecms/proto/simple/SimpleNode.java Wed Dec 29 03:13:18 2010 +0900 @@ -4,9 +4,12 @@ import java.util.Collections; import java.util.List; import java.util.LinkedList; +import java.util.Map; +import java.util.Set; + +import treecms.proto.api.Link; import treecms.proto.api.Node; import treecms.proto.api.NodeID; -import treecms.proto.id.IncrementalNodeID; import java.util.Hashtable; class SimpleNode implements Node @@ -15,7 +18,7 @@ private NodeID m_id; private String m_title,m_class; - private Hashtable<String,String> m_attrs; + private Map<String,String> m_attrs; SimpleNode(NodeID _id) { @@ -26,14 +29,17 @@ m_class = ""; m_attrs = new Hashtable<String,String>(); - - SimpleNodeTable.getInstance().put(m_id.toString(),this); + } + + @Override + public Set<String> getAttributeKeys() + { + return m_attrs.keySet(); } @Override public List<Node> getChildren() { - // return unmodifiable list<node> return Collections.unmodifiableList(m_children); } @@ -46,9 +52,9 @@ @Override public Node addChild(Node _child) { - if(m_children.contains(_child)){ + if(!m_children.contains(_child)){ m_children.add(_child); - return null; + return _child; } return null; } @@ -62,7 +68,6 @@ @Override public Iterator<Node> iterator() { - // return unmodifiable iterator return m_children.iterator(); } @@ -80,15 +85,10 @@ } } - @Override public Node cloneNode() { - SimpleNode clone = new SimpleNode(m_id.update()); - clone.setTitle(getTitle()); - clone.setClassName(getClassName()); - clone.addChildren(m_children); - return clone; + return SimpleBrowser.getSingleton().cloneNode(this); } @Override @@ -119,9 +119,9 @@ public void down(Node _child) { int index = m_children.indexOf(_child); - if(index != 0){ + if(index != -1 && index < m_children.size()-1){ m_children.remove(_child); - m_children.add(index,_child); + m_children.add(index+1,_child); } } @@ -129,7 +129,7 @@ public void replace(Node _target, Node _newChild) { int index = m_children.indexOf(_target); - if(index != 0){ + if(index != -1){ m_children.remove(_target); m_children.add(index,_newChild); } @@ -139,9 +139,9 @@ public void up(Node _child) { int index = m_children.indexOf(_child); - if(index != 0){ + if(index > 0){ m_children.remove(_child); - m_children.add(index,_child); + m_children.add(index-1,_child); } } @@ -173,4 +173,10 @@ { return SimpleBrowser.getSingleton().createNode(); } + + @Override + public Link createLink() + { + return SimpleBrowser.getSingleton().createLink(this); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/simple/test/SimpleEditorTest1.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,74 @@ +/** + * SimpleEditorTest1 + * + * testClone + * test monotonic-tree modification + */ +package treecms.proto.simple.test; + +import org.junit.Test; +import java.util.concurrent.atomic.AtomicReference; +import org.junit.runner.JUnitCore; +import treecms.proto.api.*; +import treecms.proto.simple.*; + +public class SimpleEditorTest1 +{ + public static void main(String _args[]) + { + JUnitCore.main(SimpleEditorTest1.class.getName()); + } + + private AtomicReference<Node> m_root; + private Node m_target1; + private Node m_target2; + + public SimpleEditorTest1() + { + //create tree + Node root = new SimpleNode(); + root.setTitle("root"); + + Node child1 = root.addChild(new SimpleNode()); + child1.setTitle("child1"); + Node child2 = root.addChild(new SimpleNode()); + child2.setTitle("child2"); + + Node child11 = child1.addChild(new SimpleNode()); + child11.setTitle("child11"); + Node child12 = child1.addChild(new SimpleNode()); + child12.setTitle("child12"); + + //AtomicReference<T> to use CompareAndSet + m_root = new AtomicReference<Node>(root); + + m_target1 = child2; + m_target2 = child11; + } + + @Test + public void testClone() + { + SimpleEditor editor = new SimpleEditor(m_root); + Node node = editor.edit(m_target1); + node.setTitle("*"+node.getTitle()); + System.out.println("-----------------------------------------------"); + print(editor.getUncommited(),0); + + node = editor.edit(m_target2); + node.setTitle("*"+node.getTitle()); + System.out.println("-----------------------------------------------"); + print(editor.getUncommited(),0); + } + + private void print(Node _root,int _indent) + { + for(int i = 0;i < _indent;i ++){ + System.out.print("\t"); + } + System.out.println(_root.getTitle()+"["+_root.getID()+"]"); + for(Node child : _root.getChildren()){ + print(child,_indent+1); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/simple/test/SimpleEditorTest2.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,123 @@ +package treecms.proto.simple.test; + +/** + * SimpleEditorTest2 + * + * testMultiThread + * test concurrent monotonic-tree modification + */ + +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.Test; + +import treecms.proto.api.Node; +import treecms.proto.simple.SimpleEditor; +import treecms.proto.simple.SimpleNode; +import treecms.proto.util.PreOrderTreeWalker; + +public class SimpleEditorTest2 +{ + public static void main(String _args[]) + { + new SimpleEditorTest2(); + } + + private AtomicReference<Node> m_contents; + + public SimpleEditorTest2() + { + Node root = new SimpleNode(); + root.setTitle("root"); + Node c1 = root.addChild(new SimpleNode()); + c1.setTitle("c1"); + Node c2 = root.addChild(new SimpleNode()); + c2.setTitle("c2"); + + + Node c11 = c1.addChild(new SimpleNode()); + c11.setTitle("c11"); + Node c12 = c1.addChild(new SimpleNode()); + c12.setTitle("c12"); + + Node c121 = c12.addChild(new SimpleNode()); + c121.setTitle("c121"); + + m_contents = new AtomicReference<Node>(root); + + testMultiThread(); + } + + @Test + public void testMultiThread() + { + int threads = 10; //number of threads. + final int modifyCount = 100; + ExecutorService service = Executors.newFixedThreadPool(threads); + + for(int i = 0;i < threads;i ++){ + service.execute(new Runnable(){ + + @Override + public void run() + { + SimpleEditor editor = new SimpleEditor(m_contents); + Random random = new Random(); + + for(int i = 0;i < modifyCount;i ++){ + List<Node> nodeList = transform(new PreOrderTreeWalker(m_contents.get())); + + //get random target from nodelist + Node target = nodeList.get(Math.abs(random.nextInt(nodeList.size()))); + + //edit target + editor.edit(target); + + //force commit. + editor.commit(true); + } + } + + /* + * convert iterator to list + */ + public List<Node> transform(PreOrderTreeWalker _walker) + { + LinkedList<Node> nodeList = new LinkedList<Node>(); + for(Node node : _walker){ + nodeList.add(node); + } + return nodeList; + } + }); + } + + service.shutdown(); + + try { + service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + print(m_contents.get(),0); + } + + private void print(Node _root,int _indent) + { + for(int i = 0;i < _indent;i ++){ + System.out.print("\t"); + } + System.out.println(_root.getTitle()+"["+_root.getID()+"]"); + for(Node child : _root.getChildren()){ + print(child,_indent+1); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/simple/test/SimpleLinkTest.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,18 @@ +package treecms.proto.simple.test; + +import org.junit.runner.JUnitCore; +import treecms.proto.simple.*; +import treecms.proto.test.LinkTest; + +public class SimpleLinkTest extends LinkTest +{ + public static void main(String _arg[]) + { + JUnitCore.main(SimpleLinkTest.class.getName()); + } + + public SimpleLinkTest() + { + super(SimpleBrowser.getSingleton().useContents().createNode()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/simple/test/SimpleNodeTest.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,19 @@ +package treecms.proto.simple.test; + +import org.junit.runner.JUnitCore; + +import treecms.proto.simple.SimpleBrowser; +import treecms.proto.test.NodeTest; + +public class SimpleNodeTest extends NodeTest +{ + public static void main(String _args[]) + { + JUnitCore.main(SimpleNodeTest.class.getName()); + } + + public SimpleNodeTest() + { + super(SimpleBrowser.getSingleton().createNode()); + } +}
--- a/src/treecms/proto/test/IncrementalNodeIDTest1.java Mon Dec 27 21:08:43 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,107 +0,0 @@ -package treecms.proto.test; - -/** - * IncrementalNodeIDTest1 - * - * testIDstartsWithZero() - * the id must be start from zero. - * - * testUpdateIDFromLatestID() - * check that id able to update correctly from latest node id. - * - * testUpdateIDFromOldID() - * check that id able to update correctly from old node id. - * - * testMultiThreadUpdateID() - * execute concurrent update using shared object. - */ - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import org.junit.Test; -import org.junit.Assert; -import org.junit.runner.JUnitCore; -import treecms.proto.api.NodeID; -import treecms.proto.id.IncrementalNodeID; - -public class IncrementalNodeIDTest1 -{ - public static void main(String _args[]) - { - JUnitCore.main(IncrementalNodeIDTest1.class.getName()); - } - - public IncrementalNodeIDTest1() - { - } - - @Test - public void testIDstartsWithZero() - { - NodeID id = new IncrementalNodeID(); - long ver = Long.parseLong(id.toString().split("@")[1]); - Assert.assertEquals(ver,0); - } - - @Test - public void testUpdateIDFromLatestID() - { - int count = 100; - NodeID id = new IncrementalNodeID(); - - for(int i = 1;i <= count;i ++){ - id = id.update(); - long ver = Long.parseLong(id.toString().split("@")[1]); - Assert.assertEquals(ver,i); - } - } - - @Test - public void testUpdateIDFromOldID() - { - int count = 100; - NodeID id0 = new IncrementalNodeID(); - - for(int i = 1;i <= count;i ++){ - NodeID id = id0.update(); - long ver = Long.parseLong(id.toString().split("@")[1]); - Assert.assertEquals(ver,i); - } - } - - @Test - public void testMultiThreadedUpdateID() - { - final int threads = 10; //number of threads - final int count = 100; //modification count - ExecutorService service = Executors.newFixedThreadPool(threads); - - final NodeID id = new IncrementalNodeID(); - - for(int i = 0;i < threads;i ++){ - service.execute(new Runnable(){ - @Override - public void run() - { - for(int j = 0;j < count;j ++){ - id.update(); - } - } - }); - } - - service.shutdown(); - - try { - service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - //check id - NodeID tip = id.update(); - long ver = Long.parseLong(tip.toString().split("@")[1]); - Assert.assertEquals(ver,threads*count + 1); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/LinkTest.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,54 @@ +package treecms.proto.test; + +import junit.framework.Assert; + +import org.junit.Test; + +import treecms.proto.api.Link; +import treecms.proto.api.Node; + +public class LinkTest +{ + private Node m_node; + + public LinkTest(Node _node) + { + m_node = _node; + } + + @Test + public void testCreateLink() + { + Link link = m_node.createLink(); + m_node.setTitle("title"); + m_node.setClassName("class"); + + Assert.assertEquals(link.getTitle(),m_node.getTitle()); + Assert.assertEquals(link.getClassName(),m_node.getClassName()); + Assert.assertEquals(false,link.getID().equals(m_node.getID())); + } + + @Test + public void testEditFromLink() + { + Link link = m_node.createLink(); + link.setClassName("class"); + link.setTitle("title"); + + Assert.assertEquals(link.getTitle(),m_node.getTitle()); + Assert.assertEquals(link.getClassName(),m_node.getClassName()); + } + + @Test + public void testClone() + { + Link link = m_node.createLink(); + m_node.setTitle("title"); + m_node.setClassName("class"); + + Node clone = link.cloneNode(); + Assert.assertEquals(clone.getClassName(),link.getClassName()); + Assert.assertEquals(clone.getTitle(),link.getTitle()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/NodeIDTest.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,61 @@ +package treecms.proto.test; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Test; + +import treecms.proto.api.NodeID; + +public class NodeIDTest +{ + private NodeID m_id; + + public NodeIDTest(NodeID _id) + { + m_id = _id; + } + + @Test + @After + public void testUpdate() + { + NodeID order = m_id.update(); + Assert.assertEquals(true,order.isOrderThen(m_id)); + } + + @Test + @After + public void testGetTip() + { + NodeID tip1 = m_id.update(); + NodeID tip2 = m_id.getTip(); + + Assert.assertEquals(true,tip1.equals(tip2)); + } + + @Test + public void testGetProperties() + { + String uuid = m_id.getUUID(); + String version = m_id.getVersion(); + Assert.assertNotNull(uuid); + Assert.assertNotNull(version); + } + + @Test + public void testCompareOperation() + { + NodeID order = m_id.update(); + Assert.assertEquals(true,m_id.equals(m_id)); + Assert.assertEquals(false,m_id.equals(order)); + + Assert.assertEquals(true,order.isOrderThen(m_id)); + Assert.assertEquals(false,m_id.isOrderThen(order)); + + NodeID newID = m_id.create(); + + Assert.assertEquals(true,m_id.isFamily(order)); + Assert.assertEquals(false,m_id.isFamily(newID)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/NodeTest.java Wed Dec 29 03:13:18 2010 +0900 @@ -0,0 +1,141 @@ +package treecms.proto.test; + +import static org.hamcrest.core.IsNull.notNullValue; +import static org.hamcrest.core.IsNot.not; + +import org.junit.Test; +import org.junit.Assert; +import treecms.proto.api.*; +import java.util.List; + +public class NodeTest +{ + private Node m_node; + + protected NodeTest(Node _node) + { + m_node = _node; + } + + @Test + public void testCreateNode() + { + Node node = m_node.cloneNode(); + Assert.assertThat(node,notNullValue()); + + String clsName = "test1"; + node.setClassName(clsName); + Assert.assertEquals(node.getClassName(),clsName); + + String title = "test2"; + node.setTitle(title); + Assert.assertEquals(node.getTitle(),title); + } + + @Test + public void testClone() + { + Node node = m_node.cloneNode(); + + String clsName = "test1"; + node.setClassName(clsName); + + String title = "test2"; + node.setTitle(title); + + Node clone = node.cloneNode(); + Assert.assertThat(clone.getID(),not(node.getID())); + Assert.assertEquals(node.getClassName(),clone.getClassName()); + Assert.assertEquals(node.getTitle(),clone.getTitle()); + } + + @Test + public void testChildOperation() + { + //check that correctly added. + Node child1 = m_node.createNode(); + m_node.addChild(child1); + Node child2 = m_node.createNode(); + m_node.addChild(child2); + Node child3 = m_node.createNode(); + m_node.addChild(child3); + + List<Node> children = m_node.getChildren(); + Assert.assertEquals(children.size(),3); + Assert.assertEquals(children.get(0),child1); + Assert.assertEquals(children.get(1),child2); + Assert.assertEquals(children.get(2),child3); + } + + @Test + public void testRemoveChild() + { + //remove + Node child1 = m_node.createNode(); + m_node.addChild(child1); + Node child2 = m_node.createNode(); + m_node.addChild(child2); + Node child3 = m_node.createNode(); + m_node.addChild(child3); + + m_node.removeChild(child2); + + List<Node> children = m_node.getChildren(); + Assert.assertEquals(children.get(0),child1); + Assert.assertEquals(children.get(1),child3); + } + + @Test + public void testUpOperation() + { + //up + Node child1 = m_node.createNode(); + m_node.addChild(child1); + Node child2 = m_node.createNode(); + m_node.addChild(child2); + Node child3 = m_node.createNode(); + m_node.addChild(child3); + m_node.up(child2); + + List<Node> children = m_node.getChildren(); + Assert.assertEquals(children.get(0),child2); + Assert.assertEquals(children.get(1),child1); + Assert.assertEquals(children.get(2),child3); + } + + @Test + public void testDownOperation() + { + Node child1 = m_node.createNode(); + m_node.addChild(child1); + Node child2 = m_node.createNode(); + m_node.addChild(child2); + Node child3 = m_node.createNode(); + m_node.addChild(child3); + m_node.down(child2); + + List<Node> children = m_node.getChildren(); + Assert.assertEquals(children.get(0),child1); + Assert.assertEquals(children.get(1),child3); + Assert.assertEquals(children.get(2),child2); + } + + @Test + public void testReplaceOperation() + { + //replace + Node child1 = m_node.createNode(); + m_node.addChild(child1); + Node child2 = m_node.createNode(); + m_node.addChild(child2); + Node child3 = m_node.createNode(); + m_node.addChild(child3); + Node node = m_node.cloneNode(); + m_node.replace(child2,node); + + List<Node> children = m_node.getChildren(); + Assert.assertEquals(children.get(0),child1); + Assert.assertEquals(children.get(1),node); + Assert.assertEquals(children.get(2),child3); + } +}
--- a/src/treecms/proto/test/SimpleEditorTest1.java Mon Dec 27 21:08:43 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -/** - * SimpleEditorTest1 - * - * testClone - * test monotonic-tree modification - */ -package treecms.proto.test; - -import org.junit.Test; -import java.util.concurrent.atomic.AtomicReference; -import org.junit.runner.JUnitCore; -import treecms.proto.api.*; -import treecms.proto.simple.*; - -public class SimpleEditorTest1 -{ - public static void main(String _args[]) - { - JUnitCore.main(SimpleEditorTest1.class.getName()); - } - - private AtomicReference<Node> m_root; - private Node m_target1; - private Node m_target2; - - public SimpleEditorTest1() - { - //create tree - Node root = new SimpleNode(); - root.setTitle("root"); - - Node child1 = root.addChild(new SimpleNode()); - child1.setTitle("child1"); - Node child2 = root.addChild(new SimpleNode()); - child2.setTitle("child2"); - - Node child11 = child1.addChild(new SimpleNode()); - child11.setTitle("child11"); - Node child12 = child1.addChild(new SimpleNode()); - child12.setTitle("child12"); - - //AtomicReference<T> to use CompareAndSet - m_root = new AtomicReference<Node>(root); - - m_target1 = child2; - m_target2 = child11; - } - - @Test - public void testClone() - { - SimpleEditor editor = new SimpleEditor(m_root); - Node node = editor.edit(m_target1); - node.setTitle("*"+node.getTitle()); - System.out.println("-----------------------------------------------"); - print(editor.getUncommited(),0); - - node = editor.edit(m_target2); - node.setTitle("*"+node.getTitle()); - System.out.println("-----------------------------------------------"); - print(editor.getUncommited(),0); - } - - private void print(Node _root,int _indent) - { - for(int i = 0;i < _indent;i ++){ - System.out.print("\t"); - } - System.out.println(_root.getTitle()+"["+_root.getID()+"]"); - for(Node child : _root.getChildren()){ - print(child,_indent+1); - } - } -}
--- a/src/treecms/proto/test/SimpleEditorTest2.java Mon Dec 27 21:08:43 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,123 +0,0 @@ -package treecms.proto.test; - -/** - * SimpleEditorTest2 - * - * testMultiThread - * test concurrent monotonic-tree modification - */ - -import java.util.LinkedList; -import java.util.List; -import java.util.Random; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; - -import org.junit.Test; - -import treecms.proto.api.Node; -import treecms.proto.simple.SimpleEditor; -import treecms.proto.simple.SimpleNode; -import treecms.proto.util.PreOrderTreeWalker; - -public class SimpleEditorTest2 -{ - public static void main(String _args[]) - { - new SimpleEditorTest2(); - } - - private AtomicReference<Node> m_contents; - - public SimpleEditorTest2() - { - Node root = new SimpleNode(); - root.setTitle("root"); - Node c1 = root.addChild(new SimpleNode()); - c1.setTitle("c1"); - Node c2 = root.addChild(new SimpleNode()); - c2.setTitle("c2"); - - - Node c11 = c1.addChild(new SimpleNode()); - c11.setTitle("c11"); - Node c12 = c1.addChild(new SimpleNode()); - c12.setTitle("c12"); - - Node c121 = c12.addChild(new SimpleNode()); - c121.setTitle("c121"); - - m_contents = new AtomicReference<Node>(root); - - testMultiThread(); - } - - @Test - public void testMultiThread() - { - int threads = 10; //number of threads. - final int modifyCount = 100; - ExecutorService service = Executors.newFixedThreadPool(threads); - - for(int i = 0;i < threads;i ++){ - service.execute(new Runnable(){ - - @Override - public void run() - { - SimpleEditor editor = new SimpleEditor(m_contents); - Random random = new Random(); - - for(int i = 0;i < modifyCount;i ++){ - List<Node> nodeList = transform(new PreOrderTreeWalker(m_contents.get())); - - //get random target from nodelist - Node target = nodeList.get(Math.abs(random.nextInt(nodeList.size()))); - - //edit target - editor.edit(target); - - //force commit. - editor.commit(true); - } - } - - /* - * convert iterator to list - */ - public List<Node> transform(PreOrderTreeWalker _walker) - { - LinkedList<Node> nodeList = new LinkedList<Node>(); - for(Node node : _walker){ - nodeList.add(node); - } - return nodeList; - } - }); - } - - service.shutdown(); - - try { - service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - print(m_contents.get(),0); - } - - private void print(Node _root,int _indent) - { - for(int i = 0;i < _indent;i ++){ - System.out.print("\t"); - } - System.out.println(_root.getTitle()+"["+_root.getID()+"]"); - for(Node child : _root.getChildren()){ - print(child,_indent+1); - } - } -}
--- a/src/treecms/proto/test/SimpleLinkTest2.java Mon Dec 27 21:08:43 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,70 +0,0 @@ -package treecms.proto.test; - -import static org.hamcrest.core.IsNot.not; - -import org.junit.Test; -import org.junit.Assert; -import org.junit.runner.JUnitCore; -import treecms.proto.api.*; -import treecms.proto.simple.*; - -public class SimpleLinkTest2 -{ - public static void main(String _arg[]) - { - JUnitCore.main(SimpleLinkTest2.class.getName()); - } - - private Node m_root1; - - private Node m_root2; - - public SimpleLinkTest2() - { - m_root1 = new SimpleNode(); - m_root1.addChild(new SimpleNode()); - m_root1.addChild(new SimpleNode()); - m_root1.addChild(new SimpleNode()); - - m_root2 = new SimpleNode(); - m_root2.addChild(new SimpleNode()); - m_root2.addChild(new SimpleNode()); - } - - @Test - public void testCreateLink() - { - Link link = (Link)m_root1.addChild(new SimpleLink(m_root2)); - m_root2.setTitle("title"); - m_root2.setClassName("class"); - - Assert.assertEquals(link.getTitle(),m_root2.getTitle()); - Assert.assertEquals(link.getClassName(),m_root2.getClassName()); - Assert.assertThat(link.getID(),not(m_root2.getID())); - } - - @Test - public void testEditFromLink() - { - Link link = new SimpleLink(m_root2); - link.setClassName("class"); - link.setTitle("title"); - - Assert.assertEquals(link.getTitle(),m_root2.getTitle()); - Assert.assertEquals(link.getClassName(),m_root2.getClassName()); - Assert.assertThat(link.getID(),not(m_root2.getID())); - } - - @Test - public void testClone() - { - Link link = (Link)m_root1.addChild(new SimpleLink(m_root2)); - m_root2.setTitle("title"); - m_root2.setClassName("class"); - - Link clone = (Link)link.cloneNode(); - Assert.assertThat(clone.getID(),not(link.getID())); - Assert.assertEquals(m_root2.getClassName(),clone.getClassName()); - Assert.assertEquals(m_root2.getTitle(),clone.getTitle()); - } -}
--- a/src/treecms/proto/test/SimpleNodeTest1.java Mon Dec 27 21:08:43 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,122 +0,0 @@ -package treecms.proto.test; - -import static org.hamcrest.core.IsNull.notNullValue; -import static org.hamcrest.core.IsNot.not; - -import org.junit.Test; -import org.junit.Assert; -import org.junit.runner.JUnitCore; -import treecms.proto.api.*; -import treecms.proto.simple.*; -import java.util.List; - -public class SimpleNodeTest1 -{ - public static void main(String _arg[]) - { - JUnitCore.main(SimpleNodeTest1.class.getName()); - } - - private Node m_target; - private Node m_child1,m_child2,m_child3; - - public SimpleNodeTest1() - { - m_target = new SimpleNode(); - m_child1 = m_target.addChild(new SimpleNode()); - m_child2 = m_target.addChild(new SimpleNode()); - m_child3 = m_target.addChild(new SimpleNode()); - } - - @Test - public void testCreateNode() - { - Node node = new SimpleNode(); - Assert.assertThat(node,notNullValue()); - - String clsName = "test1"; - node.setClassName(clsName); - Assert.assertEquals(node.getClassName(),clsName); - - String title = "test2"; - node.setTitle(title); - Assert.assertEquals(node.getTitle(),title); - } - - @Test - public void testClone() - { - Node node = new SimpleNode(); - - String clsName = "test1"; - node.setClassName(clsName); - - String title = "test2"; - node.setTitle(title); - - Node clone = node.cloneNode(); - Assert.assertThat(clone.getID(),not(node.getID())); - Assert.assertEquals(node.getClassName(),clone.getClassName()); - Assert.assertEquals(node.getTitle(),clone.getTitle()); - } - - @Test - public void testChildOperation() - { - //check that correctly added. - List<Node> children = m_target.getChildren(); - Assert.assertEquals(children.size(),3); - Assert.assertEquals(children.get(0),m_child1); - Assert.assertEquals(children.get(1),m_child2); - Assert.assertEquals(children.get(2),m_child3); - - } - - @Test - public void testRemoveChild() - { - //remove - m_target.removeChild(m_child3); - - List<Node> children = m_target.getChildren(); - Assert.assertEquals(children.get(0),m_child1); - Assert.assertEquals(children.get(1),m_child2); - } - - @Test - public void testUpOperation() - { - //up - m_target.up(m_child2); - - List<Node> children = m_target.getChildren(); - Assert.assertEquals(children.get(0),m_child2); - Assert.assertEquals(children.get(1),m_child1); - Assert.assertEquals(children.get(2),m_child3); - } - - @Test - public void testDownOperation() - { - //up - m_target.down(m_child2); - - List<Node> children = m_target.getChildren(); - Assert.assertEquals(children.get(0),m_child1); - Assert.assertEquals(children.get(1),m_child3); - Assert.assertEquals(children.get(2),m_child2); - } - - @Test - public void testReplaceOperation() - { - //replace - Node node = new SimpleNode(); - m_target.replace(m_child2,node); - - List<Node> children = m_target.getChildren(); - Assert.assertEquals(children.get(0),m_child1); - Assert.assertEquals(children.get(1),node); - Assert.assertEquals(children.get(2),m_child3); - } -}