PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jdo/JDONotNullConstraintsTest.java

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