1 package org.sapia.regis.hibernate; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.InputStream ; 6 import java.util.Properties ; 7 8 import junit.framework.TestCase; 9 10 import org.hibernate.SessionFactory; 11 import org.sapia.regis.Path; 12 import org.sapia.regis.Node; 13 import org.sapia.regis.RWNode; 14 import org.sapia.regis.RWSession; 15 import org.sapia.regis.RegisSession; 16 import org.sapia.regis.loader.RegistryConfigLoader; 17 18 public class HibernateRegistryTest extends TestCase { 19 20 private HibernateRegistry regis; 21 22 protected void setUp() throws Exception { 23 regis = openRegis(); 25 } 26 27 private HibernateRegistry openRegis() throws Exception { 28 Properties props = new Properties (); 29 InputStream is = null; 30 props.load(is = new FileInputStream (new File ("etc/hibernate.properties"))); 31 is.close(); 32 return (HibernateRegistry)new HibernateRegistryFactory().connect(props); 33 } 34 35 private HibernateRegistry openRegis(SessionFactory fac) throws Exception { 36 return new HibernateRegistry(fac); 37 } 38 39 public void testGetRoot() { 40 RegisSession s = regis.open(); 41 Node root = regis.getRoot(); 42 super.assertEquals(true, root.isRoot()); 43 s.close(); 44 regis.close(); 45 } 46 47 public void testCreateNode() throws Exception { 48 RWSession s; 49 s = (RWSession)regis.open(); 50 s.begin(); 51 Node node = regis.createNode(Path.parse("child1/child2")); 52 s.commit(); 53 s.close(); 54 55 regis = openRegis(regis.getSessionFactory()); 56 57 s = (RWSession)regis.open(); 58 s.begin(); 59 node = regis.getRoot().getChild(Path.parse("child1/child2")); 60 super.assertTrue(node != null); 61 s.commit(); 62 s.close(); 63 regis.close(); 64 } 65 66 public void testConfiguration() throws Exception { 67 RWSession s; 68 s = (RWSession)regis.open(); 69 s.begin(); 70 RWNode node = (RWNode)regis.createNode(Path.parse("child1/child2")); 71 node.setProperty("string", "stringValue"); 72 node.setProperty("int", "100"); 73 s.commit(); 74 s.close(); 75 76 regis = openRegis(regis.getSessionFactory()); 77 s = (RWSession)regis.open(); 78 s.begin(); 79 node = (RWNode)regis.getRoot().getChild(Path.parse("child1/child2")); 80 super.assertEquals("stringValue", node.getProperty("string").asString()); 81 super.assertEquals(100, node.getProperty("int").asInt()); 82 s.commit(); 83 s.close(); 84 regis.close(); 85 } 86 87 public void testXmlConfiguration() throws Exception { 88 RWSession s = (RWSession)regis.open(); 89 s.begin(); 90 RWNode node = (RWNode)regis.getRoot(); 91 RegistryConfigLoader loader = new RegistryConfigLoader(node); 92 loader.load(new File ("etc/configCreateExample.xml")); 93 s.commit(); 94 95 Node db000 = node.getChild(Path.parse("databases/000")); 96 assertEquals("jsmith", db000.getProperty("username").asString()); 97 assertEquals("foo123", db000.getProperty("password").asString()); 98 assertEquals("jdbc://saturn:5432/", db000.getProperty("url").asString()); 99 100 Node db001 = node.getChild(Path.parse("databases/001")); 101 assertEquals("stiger", db001.getProperty("username").asString()); 102 assertEquals("bar123", db001.getProperty("password").asString()); 103 assertEquals("jdbc://pluto:5432/", db001.getProperty("url").asString()); 104 105 Node db002 = node.getChild(Path.parse("databases/002")); 106 assertEquals("stiger", db002.getProperty("username").asString()); 107 assertEquals("bar123", db002.getProperty("password").asString()); 108 assertEquals("jdbc://mercury:5432/", db002.getProperty("url").asString()); 109 110 Node account1 = node.getChild(Path.parse("users/backoffice/account1")); 111 assertEquals("cbrown", account1.getProperty("username").asString()); 112 assertEquals("Charlie", account1.getProperty("firstName").asString()); 113 assertEquals("Brown", account1.getProperty("lastName").asString()); 114 assertEquals("lupus9890!", account1.getProperty("password").asString()); 115 116 Node account2 = node.getChild(Path.parse("users/backoffice/account2")); 117 assertEquals("dmenace", account2.getProperty("username").asString()); 118 assertEquals("Dennis", account2.getProperty("firstName").asString()); 119 assertEquals("Menace", account2.getProperty("lastName").asString()); 120 assertEquals("canis$2677", account2.getProperty("password").asString()); 121 122 s.close(); 123 124 } 125 126 } 127 | Popular Tags |