1 21 package com.db4o.test; 22 23 import com.db4o.*; 24 import com.db4o.foundation.*; 25 26 public class CallbacksTestCase 27 { 28 static boolean returnValue = true; 29 static final Object lock = new Object (); 30 31 static final int ACTIVATE = 0; 32 static final int DEACTIVATE = 1; 33 static final int DELETE = 2; 34 static final int NEW = 3; 35 static final int UPDATE = 4; 36 static final int CAN_ACTIVATE = 5; 37 static final int CAN_DEACTIVATE = 6; 38 static final int CAN_DELETE = 7; 39 static final int CAN_NEW = 8; 40 static final int CAN_UPDATE = 9; 41 42 static boolean[] called = new boolean[CAN_UPDATE + 1]; 43 44 45 46 public String name; 47 public CallbackHelper helper; 48 49 public void storeOne(){ 50 name = "stored"; 53 noneCalled(); 54 } 55 56 public void testOne(){ 57 58 ObjectContainer oc = Test.objectContainer(); 59 60 ensure(ACTIVATE); 61 ensureNot(DEACTIVATE); 62 ensureNot(DELETE); 63 ensure(NEW); 64 ensureNot(UPDATE); 65 66 ensure(CAN_ACTIVATE); 67 ensureNot(CAN_DEACTIVATE); 68 ensureNot(CAN_DELETE); 69 ensure(CAN_NEW); 70 ensureNot(CAN_UPDATE); 71 noneCalled(); 72 73 setReturn(false); 74 oc.deactivate(this,3); 75 ensure(CAN_DEACTIVATE); 76 ensureNot(DEACTIVATE); 77 Test.ensure(name.equals("stored")); 78 noneCalled(); 79 80 setReturn(true); 81 oc.deactivate(this,3); 82 ensure(CAN_DEACTIVATE); 83 ensure(DEACTIVATE); 84 Test.ensure(name == null); 85 noneCalled(); 86 87 setReturn(false); 88 oc.activate(this,3); 89 ensure(CAN_ACTIVATE); 90 ensureNot(ACTIVATE); 91 Test.ensure(name == null); 92 noneCalled(); 93 94 setReturn(true); 95 oc.activate(this,3); 96 ensure(CAN_ACTIVATE); 97 ensure(ACTIVATE); 98 Test.ensure(name.equals("stored")); 99 noneCalled(); 100 101 setReturn(false); 102 name = "modified"; 103 oc.set(this); 104 ensure(CAN_UPDATE); 105 ensureNot(UPDATE); 106 setReturn(true); 107 oc.ext().refresh(this, 3); 108 Test.ensure(name.equals("stored")); 109 noneCalled(); 110 111 setReturn(true); 112 name = "modified"; 113 oc.set(this); 114 ensure(CAN_UPDATE); 115 ensure(UPDATE); 116 oc.ext().refresh(this, 3); 117 Test.ensure(name.equals("modified")); 118 noneCalled(); 119 120 helper = new CallbackHelper(); 122 helper.name = "helper"; 123 helper.parent = this; 124 oc.set(this); 125 oc.activate(this, 3); 126 oc.deactivate(this, 3); 127 128 oc.activate(this, 1); 129 oc.deactivate(this.helper, 1); 130 setReturn(false); 131 noneCalled(); 132 oc.activate(this, 3); 133 ensureNot(ACTIVATE); 134 135 setReturn(true); 136 noneCalled(); 137 oc.delete(this); 138 oc.commit(); 139 140 Cool.sleepIgnoringInterruption(100); 141 142 ensure(CAN_DELETE); 143 ensure(DELETE); 144 145 noneCalled(); 146 setReturn(true); 147 } 148 149 static void setReturn(boolean val){ 150 synchronized(lock){ 151 returnValue = val; 152 } 153 } 154 155 static boolean getReturn(){ 156 synchronized(lock){ 157 return returnValue; 158 } 159 } 160 161 public boolean objectCanActivate(ObjectContainer container){ 162 called[CAN_ACTIVATE] = true; 163 return getReturn(); 164 } 165 166 public boolean objectCanDeactivate(ObjectContainer container){ 167 called[CAN_DEACTIVATE] = true; 168 return getReturn(); 169 } 170 171 public boolean objectCanDelete(ObjectContainer container){ 172 called[CAN_DELETE] = true; 173 return getReturn(); 174 } 175 176 public boolean objectCanNew(ObjectContainer container){ 177 called[CAN_NEW] = true; 178 return getReturn(); 179 } 180 181 public boolean objectCanUpdate(ObjectContainer container){ 182 called[CAN_UPDATE] = true; 183 return getReturn(); 184 } 185 186 public void objectOnActivate(ObjectContainer container){ 187 called[ACTIVATE] = true; 188 if(helper != null){ 189 container.activate(helper, 3); 190 } 191 } 192 193 public void objectOnDeactivate(ObjectContainer container){ 194 called[DEACTIVATE] = true; 195 if(helper != null){ 196 container.deactivate(helper, 3); 197 } 198 } 199 200 public void objectOnDelete(ObjectContainer container){ 201 called[DELETE] = true; 202 if(helper != null){ 203 container.delete(helper); 204 } 205 } 206 207 public void objectOnNew(ObjectContainer container){ 208 called[NEW] = true; 209 if(helper != null){ 210 container.set(helper); 211 } 212 } 213 214 public void objectOnUpdate(ObjectContainer container){ 215 called[UPDATE] = true; 216 if(helper != null){ 217 container.set(helper); 218 } 219 } 220 221 private void ensure(int eventPos){ 222 Test.ensure(called[eventPos]); 223 } 224 225 private void ensureNot(int eventPos){ 226 Test.ensure(! called[eventPos]); 227 } 228 229 230 private void noneCalled(){ 231 for (int i = 0; i <= CAN_UPDATE; i++) { 232 called[i] = false; 233 } 234 } 235 236 237 } 238 239 | Popular Tags |