1 10 11 package com.triactive.jdo.test.views; 12 13 import com.triactive.jdo.store.ViewNotSupportedException; 14 import com.triactive.jdo.test.*; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import javax.jdo.Extent; 19 import javax.jdo.PersistenceManager; 20 import javax.jdo.Query; 21 import javax.jdo.Transaction; 22 import org.apache.log4j.Category; 23 24 25 31 32 public class ViewTest extends StorageTestCase 33 { 34 private static final Category LOG = Category.getInstance(ViewTest.class); 35 36 private boolean schemaInitialized = false; 37 38 39 45 46 public ViewTest(String name) 47 { 48 super(name); 49 } 50 51 52 protected void setUp() throws Exception 53 { 54 super.setUp(); 55 56 if (!schemaInitialized) 57 { 58 addClassesToSchema(new Class [] 59 { 60 DateWidget.class, 61 ElementWidget.class, 62 FloatWidget.class, 63 SetWidget.class, 64 SetWidgetCounts.class, 65 StringWidget.class, 66 Widget.class 67 } 68 ); 69 70 74 if (!"sqlserver".equals(vendorID)) 75 addClassesToSchema(new Class [] { MinMaxWidgetValues.class }); 76 77 schemaInitialized = true; 78 } 79 } 80 81 82 public void testViewOfWidgets() throws Exception 83 { 84 88 if ("sqlserver".equals(vendorID)) 89 return; 90 91 LOG.info("Testing view derived from of " + TEST_OBJECT_COUNT + " " + Widget.class.getName() + " objects"); 92 insertObjects(Widget.class); 93 94 MinMaxWidgetValues trueValues = new MinMaxWidgetValues(true); 95 MinMaxWidgetValues falseValues = new MinMaxWidgetValues(false); 96 97 for (int i = 0; i < objs.length; ++i) 98 { 99 Widget w = (Widget)objs[i]; 100 MinMaxWidgetValues tfv = w.getBooleanField() ? trueValues : falseValues; 101 102 if (tfv.getMinByteValue() > w.getByteField()) 103 tfv.setMinByteValue(w.getByteField()); 104 105 if (tfv.getMinShortValue() > w.getShortField()) 106 tfv.setMinShortValue(w.getShortField()); 107 108 if (tfv.getMaxIntValue() < w.getIntField()) 109 tfv.setMaxIntValue(w.getIntField()); 110 111 if (tfv.getMaxLongValue() < w.getLongField()) 112 tfv.setMaxLongValue(w.getLongField()); 113 } 114 115 PersistenceManager pm = pmf.getPersistenceManager(); 116 Transaction tx = pm.currentTransaction(); 117 118 try 119 { 120 tx.begin(); 121 122 Extent ext = pm.getExtent(MinMaxWidgetValues.class, false); 123 Iterator exti = ext.iterator(); 124 int count = 0; 125 126 while (exti.hasNext()) 127 { 128 MinMaxWidgetValues wv = (MinMaxWidgetValues)exti.next(); 129 MinMaxWidgetValues tfv; 130 131 if (wv.getBooleanValue()) 132 { 133 tfv = trueValues; 134 trueValues = null; 135 } 136 else 137 { 138 tfv = falseValues; 139 falseValues = null; 140 } 141 142 assertFieldsEqual(tfv, wv); 143 ++count; 144 } 145 146 assertEquals("Iteration over view extent returned wrong number of rows", 2, count); 147 148 tx.commit(); 149 150 154 155 try 156 { 157 tx.begin(); 158 159 MinMaxWidgetValues wv = (MinMaxWidgetValues)ext.iterator().next(); 160 wv.fillRandom(); 161 162 tx.commit(); 163 164 fail("Writing to a persistent view object succeeded"); 165 } 166 catch (ViewNotSupportedException e) 167 { 168 if (tx.isActive()) 169 tx.rollback(); 170 } 171 172 176 177 try 178 { 179 tx.begin(); 180 181 MinMaxWidgetValues wv = new MinMaxWidgetValues(true); 182 pm.makePersistent(wv); 183 184 tx.commit(); 185 186 fail("Making a view object persistent succeeded"); 187 } 188 catch (ViewNotSupportedException e) 189 { 190 if (tx.isActive()) 191 tx.rollback(); 192 } 193 194 198 199 try 200 { 201 tx.begin(); 202 203 MinMaxWidgetValues wv = (MinMaxWidgetValues)ext.iterator().next(); 204 pm.deletePersistent(wv); 205 206 tx.commit(); 207 208 fail("Deleting a persistent view object succeeded"); 209 } 210 catch (ViewNotSupportedException e) 211 { 212 if (tx.isActive()) 213 tx.rollback(); 214 } 215 } 216 finally 217 { 218 if (tx.isActive()) 219 tx.rollback(); 220 221 pm.close(); 222 } 223 224 removeObjects(); 225 } 226 227 228 public void testViewOfSetWidgets() throws Exception 229 { 230 236 if ("cloudscape".equals(vendorID)) 237 return; 238 239 LOG.info("Testing view derived from of " + TEST_OBJECT_COUNT + " " + SetWidget.class.getName() + " objects"); 240 insertObjects(SetWidget.class); 241 242 PersistenceManager pm = pmf.getPersistenceManager(); 243 Transaction tx = pm.currentTransaction(); 244 245 try 246 { 247 tx.begin(); 248 249 Extent ext = pm.getExtent(SetWidgetCounts.class, true); 250 Iterator exti = ext.iterator(); 251 int count = 0; 252 253 while (exti.hasNext()) 254 { 255 SetWidgetCounts actual = (SetWidgetCounts)exti.next(); 256 SetWidgetCounts expected = new SetWidgetCounts(actual.getSetWidget()); 257 258 assertFieldsEqual(expected, actual); 259 ++count; 260 } 261 262 tx.commit(); 263 264 assertEquals("Iteration over view extent returned wrong number of rows", TEST_OBJECT_COUNT, count); 265 266 tx.begin(); 267 268 Query query = pm.newQuery(pm.getExtent(SetWidgetCounts.class, true)); 269 query.setFilter("normalSetSize != 0"); 270 query.setOrdering("sw.numElementWidgets descending"); 271 Collection results = (Collection )query.execute(); 272 273 try 274 { 275 HashSet expected = new HashSet (); 276 277 for (int i = 0; i < objs.length; ++i) 278 { 279 SetWidget sw = (SetWidget)objs[i]; 280 281 if (sw.getNormalSet().size() != 0) 282 expected.add(new SetWidgetCounts(sw)); 283 } 284 285 assertTrue("Query has no expected results (test is broken)", !expected.isEmpty()); 286 assertTrue("Query returned no rows", !results.isEmpty()); 287 288 HashSet actual = new HashSet (results); 289 290 assertEquals("Query returned duplicate rows", results.size(), actual.size()); 291 assertTrue("Query did not return expected results: expected " + expected + ", but was " + actual, TestObject.compareCollection(expected, actual)); 292 } 293 finally 294 { 295 query.closeAll(); 296 } 297 298 tx.commit(); 299 } 300 finally 301 { 302 if (tx.isActive()) 303 tx.rollback(); 304 305 pm.close(); 306 } 307 308 removeObjects(); 309 } 310 } 311 | Popular Tags |