Mercurial > hg > Database > jungle-network
view src/test/java/alice/jungle/PersistentJournalTest.java @ 118:f64ff5bd66f5
Implements persistent for bbs app and Fixed bug JungleUpdater
author | one |
---|---|
date | Wed, 25 Dec 2013 20:02:26 +0900 |
parents | 895ab2907db3 |
children | 54d68cda84c4 |
line wrap: on
line source
package alice.jungle; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import alice.jungle.operations.NetworkNodePath; import alice.jungle.persistent.PersistentJournal; import alice.jungle.persistent.PersistentJungle; import alice.jungle.transaction.JungleUpdater; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListReader; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import junit.framework.TestCase; public class PersistentJournalTest extends TestCase { public void testPersistentJounal() throws IOException { PersistentJournal journal1 = new PersistentJournal(new File("./log/commit.log")); Jungle jungle = new PersistentJungle(journal1, "uuid",new DefaultTreeEditor(new DefaultTraverser())); jungle.createNewTree("hoge"); JungleTree tree1 = jungle.getTreeByName("hoge"); JungleTreeEditor editor = tree1.getTreeEditor(); NetworkNodePath path = new NetworkNodePath(); String key = "key"; ByteBuffer value = ByteBuffer.wrap("value".getBytes()); int pos = 0; Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); NodePath childPath = path.add(pos); assertFalse(either.isA()); editor = either.b(); either = editor.putAttribute(childPath, key, value); JungleTreeEditor e = either.b(); either = e.success(); assertFalse(either.isA()); PersistentJournal journal2 = new PersistentJournal(); journal2.setInputFile(new File("./log/commit.log")); journal2.setOutputFile(new File("./log/commit2.log")); Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTreeEditor(new DefaultTraverser())); ChangeListReader reader = journal2.getReader(); for (ChangeList chList : reader) { String treeName = chList.getTreeName(); JungleTree tree2 = jungle2.getTreeByName(treeName); if(tree2 == null) { tree2 = jungle2.createNewTree(treeName); } JungleTreeEditor editor2 = tree2.getTreeEditor(); Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); assertFalse(either2.isA()); editor2 = either2.b(); editor2.success(); } JungleTree tree2 = jungle2.getTreeByName("hoge"); Node node1 = tree1.getRootNode(); Node node2 = tree2.getRootNode(); Children<Node> child1 = node1.getChildren(); Children<Node> child2 = node2.getChildren(); assertEquals(child1.size(), child2.size()); Either<Error, Node> either1 = child1.at(pos); Either<Error, Node> either2 = child1.at(pos); assertFalse(either1.isA()); assertFalse(either2.isA()); Node nodeA = either1.b(); Node nodeB = either2.b(); ByteBuffer bb1 = nodeA.getAttributes().get(key); ByteBuffer bb2 = nodeB.getAttributes().get(key); String strA = new String(bb1.array()); String strB = new String(bb2.array()); assertTrue(strA.equals(strB)); journal1.close(); journal2.close(); } }