1 25 26 package org.objectweb.speedo.runtime.cap; 27 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 34 import javax.jdo.JDOException; 35 import javax.jdo.PersistenceManager; 36 import javax.jdo.Query; 37 38 import junit.framework.Assert; 39 40 import org.hsqldb.Server; 41 import org.objectweb.speedo.SpeedoTestHelper; 42 import org.objectweb.speedo.api.ExceptionHelper; 43 import org.objectweb.speedo.pobjects.cap.JDOAction; 44 import org.objectweb.speedo.pobjects.cap.JDOAdminUser; 45 import org.objectweb.speedo.pobjects.cap.JDORole; 46 import org.objectweb.speedo.pobjects.cap.JDOScope; 47 import org.objectweb.util.monolog.api.BasicLevel; 48 49 50 53 public class TestHsql extends SpeedoTestHelper { 54 55 56 public TestHsql(String s) { 57 super(s); 58 } 59 60 61 protected String getLoggerName() { 62 return LOG_NAME + ".rt.cap.TestHsql"; 63 } 64 65 66 70 String serverProps; 71 String url; 72 String user = "sa"; 73 String password = ""; 74 Server server; 75 boolean isNetwork = true; 76 77 protected void startServer() { 78 System.out.println("setup db server"); 79 if (isNetwork) { 80 serverProps = "database.0=mem:memorydb"; 81 url = "jdbc:hsqldb:hsql://localhost/memorydb"; 82 server = new Server(); 83 server.putPropertiesFromString(serverProps); 84 server.setLogWriter(null); 85 server.setErrWriter(null); 86 server.start(); 87 System.out.println("server " + url + " started"); 88 } else { 89 url = "jdbc:hsqldb:file:memorydb"; 90 } 91 92 try { 93 Class.forName("org.hsqldb.jdbcDriver"); 94 } catch (Exception e) { 95 e.printStackTrace(); 96 System.out.println(this + ".setUp() error: " + e.getMessage()); 97 } 98 } 99 100 protected void tearDown() { 101 102 if (isNetwork) { 103 server.stop(); 104 server = null; 105 } 106 } 107 108 Connection newConnection() throws Exception { 109 return DriverManager.getConnection("jdbc:hsqldb:file:memorydb", user, password); 110 } 111 112 113 116 public void testCapGemini(){ 117 118 119 JDOScope scope1 = new JDOScope(1, "scope1"); 120 JDOScope scope2 = new JDOScope(2, "scope2"); 121 JDOScope scope3 = new JDOScope(3, "scope3"); 122 JDOScope scope4 = new JDOScope(4, "scope4"); 123 124 Collection scopes1 = new ArrayList (); 125 scopes1.add(scope1); 126 scopes1.add(scope3); 127 128 Collection scopes2 = new ArrayList (); 129 scopes2.add(scope2); 130 scopes2.add(scope4); 131 132 JDOAction action1 = new JDOAction(1,"action1","/action1/"); 133 JDOAction action2 = new JDOAction(2,"action2","/action2/"); 134 JDOAction action3 = new JDOAction(3,"action3","/action3/"); 135 JDOAction action4 = new JDOAction(4,"action4","/action4/"); 136 137 JDORole role1 = new JDORole(1, "role1", scopes1, new ArrayList ()); 138 JDORole role2 = new JDORole(2, "role2", scopes2, new ArrayList ()); 139 140 role1.addAction(action1, true, 1); 141 role1.addAction(action3, true, 1); 142 143 role2.addAction(action2, true, 2); 144 role2.addAction(action4, true, 2); 145 146 Collection roles = new ArrayList (); 147 roles.add(role1); 148 roles.add(role2); 149 150 JDOAdminUser adminUser = new JDOAdminUser("id","login", "mail", roles); 151 152 PersistenceManager pm = pmf.getPersistenceManager(); 153 pm.currentTransaction().begin(); 154 pm.makePersistent(adminUser); 155 pm.currentTransaction().commit(); 156 pm.close(); 157 } 158 159 public void testRemovingOfPersistentObject() { 160 PersistenceManager pm = pmf.getPersistenceManager(); 161 try { 162 Class [] cs = new Class []{JDOAction.class,JDOAdminUser.class,JDORole.class, JDOScope.class}; 163 pm.currentTransaction().begin(); 164 for(int i=0; i<cs.length; i++) { 165 Query query = pm.newQuery(cs[i]); 166 Collection col = (Collection ) query.execute(); 167 Iterator it = col.iterator(); 168 while(it.hasNext()) { 169 Object o = it.next(); 170 Assert.assertNotNull("null object in the query result" 171 + cs[i].getName(), o); 172 pm.deletePersistent(o); 173 174 } 175 query.close(col); 176 } 177 pm.currentTransaction().commit(); 178 } catch (JDOException e) { 179 Exception ie = ExceptionHelper.getNested(e); 180 logger.log(BasicLevel.ERROR, "", ie); 181 fail(ie.getMessage()); 182 } finally { 183 pm.close(); 184 } 185 } 186 } 187 | Popular Tags |