/tests/com/google/appengine/datanucleus/jdo/JDONotNullConstraintsTest.java
Java | 303 lines | 257 code | 21 blank | 25 comment | 1 complexity | 985260a605bbe1b7e3fe8133c1d29f2e MD5 | raw file
1/********************************************************************** 2 Copyright (c) 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 **********************************************************************/ 16package com.google.appengine.datanucleus.jdo; 17 18import com.google.appengine.datanucleus.test.jdo.HasNotNullConstraintsJDO; 19 20import javax.jdo.JDOUserException; 21 22public class JDONotNullConstraintsTest extends JDOTestCase { 23 24 private static final Boolean VAL_BOOL = Boolean.TRUE; 25 private static final Character VAL_CHAR = 'c'; 26 private static final Byte VAL_BYTE = 0x1; 27 private static final Short VAL_SHORT = (short) 1; 28 private static final Integer VAL_INT = 2; 29 private static final Long VAL_LONG = 3L; 30 private static final Float VAL_FLOAT = 4f; 31 private static final Double VAL_DOUBLE = 5d; 32 private static final String VAL_STRING = "yam"; 33 34 public void testInsertNotNull() { 35 HasNotNullConstraintsJDO pc = createHasConstraintsJDO(VAL_BOOL, VAL_CHAR, 36 VAL_BYTE, VAL_SHORT, VAL_INT, VAL_LONG, 37 VAL_FLOAT, VAL_DOUBLE, VAL_STRING); 38 makePersistentInTxn(pc, TXN_START_END); 39 40 beginTxn(); 41 pc = pm.getObjectById(HasNotNullConstraintsJDO.class, pc.getId()); 42 assertEquals(VAL_BOOL, pc.getBool()); 43 assertEquals(VAL_CHAR, pc.getC()); 44 assertEquals(VAL_BYTE, pc.getB()); 45 assertEquals(VAL_SHORT, pc.getS()); 46 assertEquals(VAL_INT, pc.getI()); 47 assertEquals(VAL_LONG, pc.getL()); 48 assertEquals(VAL_FLOAT, pc.getF()); 49 assertEquals(VAL_DOUBLE, pc.getD()); 50 assertEquals(VAL_STRING, pc.getStr()); 51 commitTxn(); 52 } 53 54 public void testInsertNull() { 55 try { 56 makePersistentInTxn(createHasConstraintsJDO(null, 'c', (byte) 0x1, 57 (short) 1, 2, 3L, 4f, 5d, "yam"), TXN_START_END); 58 fail("expected Exception"); 59 } catch (JDOUserException e) { 60 // good 61 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 62 } 63 try { 64 makePersistentInTxn(createHasConstraintsJDO(true, null, (byte) 0x1, 65 (short) 1, 2, 3L, 4f, 5d, "yam"), TXN_START_END); 66 fail("expected Exception"); 67 } catch (JDOUserException e) { 68 // good 69 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 70 } 71 try { 72 makePersistentInTxn(createHasConstraintsJDO(true, 'c', null, (short) 1, 73 2, 3L, 4f, 5d, "yam"), TXN_START_END); 74 fail("expected Exception"); 75 } catch (JDOUserException e) { 76 // good 77 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 78 } 79 try { 80 makePersistentInTxn(createHasConstraintsJDO(true, 'c', (byte) 0x1, null, 81 2, 3L, 4f, 5d, "yam"), TXN_START_END); 82 fail("expected Exception"); 83 } catch (JDOUserException e) { 84 // good 85 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 86 } 87 try { 88 makePersistentInTxn(createHasConstraintsJDO(true, 'c', (byte) 0x1, 89 (short) 1, null, 3L, 4f, 5d, "yam"), 90 TXN_START_END); 91 fail("expected Exception"); 92 } catch (JDOUserException e) { 93 // good 94 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 95 } 96 try { 97 makePersistentInTxn(createHasConstraintsJDO(true, 'c', (byte) 0x1, 98 (short) 1, 2, null, 4f, 5d, "yam"), 99 TXN_START_END); 100 fail("expected Exception"); 101 } catch (JDOUserException e) { 102 // good 103 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 104 } 105 try { 106 makePersistentInTxn(createHasConstraintsJDO(true, 'c', (byte) 0x1, 107 (short) 1, 2, 3L, null, 5d, "yam"), 108 TXN_START_END); 109 fail("expected Exception"); 110 } catch (JDOUserException e) { 111 // good 112 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 113 } 114 try { 115 makePersistentInTxn(createHasConstraintsJDO(true, 'c', (byte) 0x1, 116 (short) 1, 2, 3L, 4f, null, "yam"), 117 TXN_START_END); 118 fail("expected Exception"); 119 } catch (JDOUserException e) { 120 // good 121 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 122 } 123 try { 124 makePersistentInTxn(createHasConstraintsJDO(true, 'c', (byte) 0x1, 125 (short) 1, 2, 3L, 4f, 5d, null), TXN_START_END); 126 fail("expected Exception"); 127 } catch (JDOUserException e) { 128 // good 129 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 130 } 131 } 132 133 public void testUpdateNotNull() { 134 HasNotNullConstraintsJDO obj = create(); 135 136 beginTxn(); 137 obj = pm.getObjectById(HasNotNullConstraintsJDO.class, obj.getId()); 138 assertTrue(obj.getBool()); 139 obj.setBool(false); 140 commitTxn(); 141 142 beginTxn(); 143 obj = pm.getObjectById(HasNotNullConstraintsJDO.class, obj.getId()); 144 assertFalse(obj.getBool()); 145 assertEquals(VAL_CHAR, obj.getC()); 146 assertEquals(VAL_BYTE, obj.getB()); 147 assertEquals(VAL_SHORT, obj.getS()); 148 assertEquals(VAL_INT, obj.getI()); 149 assertEquals(VAL_LONG, obj.getL()); 150 assertEquals(VAL_FLOAT, obj.getF()); 151 assertEquals(VAL_DOUBLE, obj.getD()); 152 assertEquals(VAL_STRING, obj.getStr()); 153 commitTxn(); 154 } 155 156 public void testUpdateNull() { 157 HasNotNullConstraintsJDO pc = create(); 158 159 doUpdate(pc.getId(), new Update() { 160 public void update(HasNotNullConstraintsJDO pc) { 161 pc.setBool(null); 162 } 163 }); 164 doUpdate(pc.getId(), new Update() { 165 public void update(HasNotNullConstraintsJDO pc) { 166 pc.setC(null); 167 } 168 }); 169 doUpdate(pc.getId(), new Update() { 170 public void update(HasNotNullConstraintsJDO pc) { 171 pc.setB(null); 172 } 173 }); 174 doUpdate(pc.getId(), new Update() { 175 public void update(HasNotNullConstraintsJDO pc) { 176 pc.setS(null); 177 } 178 }); 179 doUpdate(pc.getId(), new Update() { 180 public void update(HasNotNullConstraintsJDO pc) { 181 pc.setI(null); 182 } 183 }); 184 doUpdate(pc.getId(), new Update() { 185 public void update(HasNotNullConstraintsJDO pc) { 186 pc.setL(null); 187 } 188 }); 189 doUpdate(pc.getId(), new Update() { 190 public void update(HasNotNullConstraintsJDO pc) { 191 pc.setF(null); 192 } 193 }); 194 doUpdate(pc.getId(), new Update() { 195 public void update(HasNotNullConstraintsJDO pc) { 196 pc.setD(null); 197 } 198 }); 199 doUpdate(pc.getId(), new Update() { 200 public void update(HasNotNullConstraintsJDO pc) { 201 pc.setStr(null); 202 } 203 }); 204 } 205 206 public void testDeleteNull() { 207 doRemove(create().getId(), new Update() { 208 public void update(HasNotNullConstraintsJDO obj) { 209 obj.setBool(null); 210 } 211 }); 212 doRemove(create().getId(), new Update() { 213 public void update(HasNotNullConstraintsJDO obj) { 214 obj.setC(null); 215 } 216 }); 217 doRemove(create().getId(), new Update() { 218 public void update(HasNotNullConstraintsJDO obj) { 219 obj.setB(null); 220 } 221 }); 222 doRemove(create().getId(), new Update() { 223 public void update(HasNotNullConstraintsJDO obj) { 224 obj.setS(null); 225 } 226 }); 227 doRemove(create().getId(), new Update() { 228 public void update(HasNotNullConstraintsJDO obj) { 229 obj.setI(null); 230 } 231 }); 232 doRemove(create().getId(), new Update() { 233 public void update(HasNotNullConstraintsJDO obj) { 234 obj.setL(null); 235 } 236 }); 237 doRemove(create().getId(), new Update() { 238 public void update(HasNotNullConstraintsJDO obj) { 239 obj.setF(null); 240 } 241 }); 242 doRemove(create().getId(), new Update() { 243 public void update(HasNotNullConstraintsJDO obj) { 244 obj.setStr(null); 245 } 246 }); 247 } 248 249 private HasNotNullConstraintsJDO create() { 250 HasNotNullConstraintsJDO pc = createHasConstraintsJDO(VAL_BOOL, VAL_CHAR, 251 VAL_BYTE, VAL_SHORT, VAL_INT, VAL_LONG, 252 VAL_FLOAT, VAL_DOUBLE, VAL_STRING); 253 makePersistentInTxn(pc, TXN_START_END); 254 return pc; 255 } 256 257 private void doUpdate(Long id, Update update) { 258 try { 259 beginTxn(); 260 HasNotNullConstraintsJDO pc = pm.getObjectById(HasNotNullConstraintsJDO.class, id); 261 update.update(pc); 262 commitTxn(); 263 fail("expected Exception"); 264 } catch (JDOUserException e) { 265 // good 266 if (pm.currentTransaction().isActive()) { 267 rollbackTxn(); 268 } 269 } 270 } 271 272 private void doRemove(Long id, Update update) { 273 beginTxn(); 274 HasNotNullConstraintsJDO pc = pm.getObjectById(HasNotNullConstraintsJDO.class, id); 275 update.update(pc); 276 pm.deletePersistent(pc); 277 commitTxn(); 278 279 assertEquals(0, countForClass(HasNotNullConstraintsJDO.class)); 280 } 281 282 private HasNotNullConstraintsJDO createHasConstraintsJDO(Boolean bool, Character c, Byte b, 283 Short s, Integer i, Long l, 284 Float f, Double d, String str) { 285 HasNotNullConstraintsJDO pc = new HasNotNullConstraintsJDO(); 286 pc.setBool(bool); 287 pc.setC(c); 288 pc.setB(b); 289 pc.setS(s); 290 pc.setI(i); 291 pc.setL(l); 292 pc.setF(f); 293 pc.setD(d); 294 pc.setStr(str); 295 return pc; 296 } 297 298 interface Update { 299 300 void update(HasNotNullConstraintsJDO pc); 301 } 302 303}