1 25 26 package org.objectweb.speedo.runtime.inheritance; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import javax.jdo.Extent; 33 import javax.jdo.JDOException; 34 import javax.jdo.PersistenceManager; 35 import javax.jdo.Query; 36 37 import junit.framework.Assert; 38 39 import org.objectweb.speedo.SpeedoTestHelper; 40 import org.objectweb.speedo.api.ExceptionHelper; 41 import org.objectweb.speedo.pobjects.inheritance.index.Daughter; 42 import org.objectweb.speedo.pobjects.inheritance.index.Mother; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 45 50 public class TestIndex extends SpeedoTestHelper { 51 52 public TestIndex(String s) { 53 super(s); 54 } 55 56 protected String getLoggerName() { 57 return LOG_NAME + ".rt.inheritance.TestIndex"; 58 } 59 60 63 public void testCreate() { 64 PersistenceManager pm = pmf.getPersistenceManager(); 65 try { 66 Mother m1 = new Mother(1,"mother1", true); 67 Mother m2 = new Mother(2,"mother2", true); 68 Daughter d1 = new Daughter(3,"daughter1", false, new Long (3)); 69 Daughter d2 = new Daughter(4,"daughter2", false, new Long (4)); 70 Collection col = new ArrayList (); 71 col.add(m1); 72 col.add(m2); 73 col.add(d1); 74 col.add(d2); 75 pm.currentTransaction().begin(); 77 pm.makePersistentAll(col); 78 pm.currentTransaction().commit(); 79 } catch (Exception e) { 80 fail(e.getMessage()); 81 } finally { 82 if (pm.currentTransaction().isActive()) 83 pm.currentTransaction().rollback(); 84 pm.evictAll(); 86 pm.close(); 87 } 88 } 89 90 93 public void testSelectSuper() { 94 PersistenceManager pm = pmf.getPersistenceManager(); 95 try { 96 Query query = pm.newQuery(Mother.class); 97 String filter = "java1 > 0"; 98 query.setFilter(filter); 99 Collection result = (Collection ) query.execute(); 100 assertNotNull(result); 101 Iterator it = result.iterator(); 102 while (it.hasNext()) { 103 Mother m = (Mother) it.next(); 104 assertTrue("Field java1 should be superior to 0.", m.getJava1()>0); 105 } 106 } catch (Exception e) { 107 fail(e.getMessage()); 108 } finally { 109 pm.evictAll(); 110 pm.close(); 111 } 112 } 113 114 117 public void testSelectSubclass() { 118 PersistenceManager pm = pmf.getPersistenceManager(); 119 try { 120 Query query = pm.newQuery(Daughter.class); 121 String filter = "java1 > 0"; 122 query.setFilter(filter); 123 Collection result = (Collection ) query.execute(); 124 assertNotNull(result); 125 Iterator it = result.iterator(); 126 while (it.hasNext()) { 127 Mother m = (Mother) it.next(); 128 assertTrue("Field java1 should be superior to 0.", m.getJava1()>0); 129 } 130 } catch (Exception e) { 131 fail(e.getMessage()); 132 } finally { 133 pm.evictAll(); 134 pm.close(); 135 } 136 } 137 138 141 public void testExtentSuperWithSubclasses() { 142 computeExtent(Mother.class, true); 143 } 144 145 148 public void testExtentSuperWithoutSubclasses() { 149 computeExtent(Mother.class, false); 150 } 151 152 155 public void testExtentSubWithSubclasses() { 156 computeExtent(Daughter.class, true); 157 } 158 159 162 public void testExtentSubWithoutSubclasses() { 163 computeExtent(Daughter.class, false); 164 } 165 166 169 public void computeExtent(Class cl, boolean withSubclasses) { 170 PersistenceManager pm = pmf.getPersistenceManager(); 171 try { 172 Extent extent = pm.getExtent(cl, withSubclasses); 173 Iterator it = extent.iterator(); 174 while(it.hasNext()) { 175 Mother m = (Mother) it.next(); 176 assertTrue(m.getJava1()>0); 177 } 178 } catch (Exception e) { 179 fail(e.getMessage()); 180 } finally { 181 pm.evictAll(); 182 pm.close(); 183 } 184 } 185 189 public void testRemovingOfPersistentObject() { 190 PersistenceManager pm = pmf.getPersistenceManager(); 191 try { 192 Class [] cs = new Class []{Mother.class}; 193 pm.currentTransaction().begin(); 194 for(int i=0; i<cs.length; i++) { 195 Query query = pm.newQuery(cs[i]); 196 Collection col = (Collection ) query.execute(); 197 Iterator it = col.iterator(); 198 while(it.hasNext()) { 199 Object o = it.next(); 200 Assert.assertNotNull("null object in the query result" 201 + cs[i].getName(), o); 202 pm.deletePersistent(o); 203 204 } 205 query.close(col); 206 } 207 pm.currentTransaction().commit(); 208 } catch (JDOException e) { 209 Exception ie = ExceptionHelper.getNested(e); 210 logger.log(BasicLevel.ERROR, "", ie); 211 fail(ie.getMessage()); 212 } finally { 213 pm.close(); 214 } 215 } 216 } 217 | Popular Tags |