/tests/com/google/appengine/datanucleus/jpa/JPANotNullConstraintsTest.java

http://datanucleus-appengine.googlecode.com/ · Java · 329 lines · 282 code · 21 blank · 26 comment · 0 complexity · 07fa86a5950c75deab4c3eda5e29ca9f MD5 · raw file

  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.jpa;
  14. import com.google.appengine.datanucleus.test.jpa.HasNotNullConstraintsJPA;
  15. import javax.persistence.PersistenceException;
  16. public class JPANotNullConstraintsTest extends JPATestCase {
  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. HasNotNullConstraintsJPA obj = create();
  28. obj = em.find(HasNotNullConstraintsJPA.class, obj.getId());
  29. assertEquals(VAL_BOOL, obj.getBool());
  30. assertEquals(VAL_CHAR, obj.getC());
  31. assertEquals(VAL_BYTE, obj.getB());
  32. assertEquals(VAL_SHORT, obj.getS());
  33. assertEquals(VAL_INT, obj.getI());
  34. assertEquals(VAL_LONG, obj.getL());
  35. assertEquals(VAL_FLOAT, obj.getF());
  36. assertEquals(VAL_DOUBLE, obj.getD());
  37. assertEquals(VAL_STRING, obj.getStr());
  38. }
  39. public void testInsertNull() {
  40. try {
  41. beginTxn();
  42. em.persist(createHasNotNullConstraintsJPA(null, VAL_CHAR, VAL_BYTE,
  43. VAL_SHORT, VAL_INT, VAL_LONG, VAL_FLOAT, VAL_DOUBLE,
  44. VAL_STRING));
  45. commitTxn();
  46. fail("expected Exception");
  47. } catch (PersistenceException e) {
  48. // good
  49. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  50. }
  51. try {
  52. beginTxn();
  53. em.persist(createHasNotNullConstraintsJPA(VAL_BOOL, null, VAL_BYTE,
  54. VAL_SHORT, VAL_INT, VAL_LONG, VAL_FLOAT, VAL_DOUBLE,
  55. VAL_STRING));
  56. commitTxn();
  57. fail("expected Exception");
  58. } catch (PersistenceException e) {
  59. // good
  60. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  61. }
  62. try {
  63. beginTxn();
  64. em.persist(createHasNotNullConstraintsJPA(VAL_BOOL, VAL_CHAR, null,
  65. VAL_SHORT, VAL_INT, VAL_LONG, VAL_FLOAT, VAL_DOUBLE,
  66. VAL_STRING));
  67. commitTxn();
  68. fail("expected Exception");
  69. } catch (PersistenceException e) {
  70. // good
  71. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  72. }
  73. try {
  74. beginTxn();
  75. em.persist(createHasNotNullConstraintsJPA(VAL_BOOL, VAL_CHAR, VAL_BYTE,
  76. null, VAL_INT, VAL_LONG, VAL_FLOAT, VAL_DOUBLE,
  77. VAL_STRING));
  78. commitTxn();
  79. fail("expected Exception");
  80. } catch (PersistenceException e) {
  81. // good
  82. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  83. }
  84. try {
  85. beginTxn();
  86. em.persist(createHasNotNullConstraintsJPA(VAL_BOOL, VAL_CHAR, VAL_BYTE,
  87. VAL_SHORT, null, VAL_LONG, VAL_FLOAT, VAL_DOUBLE,
  88. VAL_STRING));
  89. commitTxn();
  90. fail("expected Exception");
  91. } catch (PersistenceException e) {
  92. // good
  93. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  94. }
  95. try {
  96. beginTxn();
  97. em.persist(createHasNotNullConstraintsJPA(null, VAL_CHAR, VAL_BYTE,
  98. VAL_SHORT, VAL_INT, null, VAL_FLOAT, VAL_DOUBLE,
  99. VAL_STRING));
  100. commitTxn();
  101. fail("expected Exception");
  102. } catch (PersistenceException e) {
  103. // good
  104. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  105. }
  106. try {
  107. beginTxn();
  108. em.persist(createHasNotNullConstraintsJPA(null, VAL_CHAR, VAL_BYTE,
  109. VAL_SHORT, VAL_INT, VAL_LONG, null, VAL_DOUBLE,
  110. VAL_STRING));
  111. commitTxn();
  112. fail("expected Exception");
  113. } catch (PersistenceException e) {
  114. // good
  115. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  116. }
  117. try {
  118. beginTxn();
  119. em.persist(createHasNotNullConstraintsJPA(null, VAL_CHAR, VAL_BYTE,
  120. VAL_SHORT, VAL_INT, VAL_LONG, VAL_FLOAT, null,
  121. VAL_STRING));
  122. commitTxn();
  123. fail("expected Exception");
  124. } catch (PersistenceException e) {
  125. // good
  126. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  127. }
  128. try {
  129. beginTxn();
  130. em.persist(createHasNotNullConstraintsJPA(null, VAL_CHAR, VAL_BYTE,
  131. VAL_SHORT, VAL_INT, VAL_LONG, VAL_FLOAT, VAL_DOUBLE,
  132. null));
  133. commitTxn();
  134. fail("expected Exception");
  135. } catch (PersistenceException e) {
  136. // good
  137. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  138. }
  139. }
  140. public void testUpdateNotNull() {
  141. HasNotNullConstraintsJPA obj = create();
  142. beginTxn();
  143. obj = em.find(HasNotNullConstraintsJPA.class, obj.getId());
  144. assertTrue(obj.getBool());
  145. obj.setBool(false);
  146. commitTxn();
  147. obj = em.find(HasNotNullConstraintsJPA.class, obj.getId());
  148. assertFalse(obj.getBool());
  149. assertEquals(VAL_CHAR, obj.getC());
  150. assertEquals(VAL_BYTE, obj.getB());
  151. assertEquals(VAL_SHORT, obj.getS());
  152. assertEquals(VAL_INT, obj.getI());
  153. assertEquals(VAL_LONG, obj.getL());
  154. assertEquals(VAL_FLOAT, obj.getF());
  155. assertEquals(VAL_DOUBLE, obj.getD());
  156. assertEquals(VAL_STRING, obj.getStr());
  157. }
  158. public void testUpdateNull() {
  159. HasNotNullConstraintsJPA obj = create();
  160. doUpdate(obj.getId(), new Update() {
  161. public void update(HasNotNullConstraintsJPA obj) {
  162. obj.setBool(null);
  163. }
  164. });
  165. doUpdate(obj.getId(), new Update() {
  166. public void update(HasNotNullConstraintsJPA obj) {
  167. obj.setC(null);
  168. }
  169. });
  170. doUpdate(obj.getId(), new Update() {
  171. public void update(HasNotNullConstraintsJPA obj) {
  172. obj.setB(null);
  173. }
  174. });
  175. doUpdate(obj.getId(), new Update() {
  176. public void update(HasNotNullConstraintsJPA obj) {
  177. obj.setS(null);
  178. }
  179. });
  180. doUpdate(obj.getId(), new Update() {
  181. public void update(HasNotNullConstraintsJPA obj) {
  182. obj.setI(null);
  183. }
  184. });
  185. doUpdate(obj.getId(), new Update() {
  186. public void update(HasNotNullConstraintsJPA obj) {
  187. obj.setL(null);
  188. }
  189. });
  190. doUpdate(obj.getId(), new Update() {
  191. public void update(HasNotNullConstraintsJPA obj) {
  192. obj.setF(null);
  193. }
  194. });
  195. doUpdate(obj.getId(), new Update() {
  196. public void update(HasNotNullConstraintsJPA obj) {
  197. obj.setStr(null);
  198. }
  199. });
  200. }
  201. public void testDeleteNull() {
  202. doRemove(create().getId(), new Update() {
  203. public void update(HasNotNullConstraintsJPA obj) {
  204. obj.setBool(null);
  205. }
  206. });
  207. doRemove(create().getId(), new Update() {
  208. public void update(HasNotNullConstraintsJPA obj) {
  209. obj.setC(null);
  210. }
  211. });
  212. doRemove(create().getId(), new Update() {
  213. public void update(HasNotNullConstraintsJPA obj) {
  214. obj.setB(null);
  215. }
  216. });
  217. doRemove(create().getId(), new Update() {
  218. public void update(HasNotNullConstraintsJPA obj) {
  219. obj.setS(null);
  220. }
  221. });
  222. doRemove(create().getId(), new Update() {
  223. public void update(HasNotNullConstraintsJPA obj) {
  224. obj.setI(null);
  225. }
  226. });
  227. doRemove(create().getId(), new Update() {
  228. public void update(HasNotNullConstraintsJPA obj) {
  229. obj.setL(null);
  230. }
  231. });
  232. doRemove(create().getId(), new Update() {
  233. public void update(HasNotNullConstraintsJPA obj) {
  234. obj.setF(null);
  235. }
  236. });
  237. doRemove(create().getId(), new Update() {
  238. public void update(HasNotNullConstraintsJPA obj) {
  239. obj.setStr(null);
  240. }
  241. });
  242. }
  243. private HasNotNullConstraintsJPA create() {
  244. beginTxn();
  245. HasNotNullConstraintsJPA obj = createHasNotNullConstraintsJPA(VAL_BOOL, VAL_CHAR,
  246. VAL_BYTE, VAL_SHORT, VAL_INT,
  247. VAL_LONG, VAL_FLOAT, VAL_DOUBLE,
  248. VAL_STRING);
  249. em.persist(obj);
  250. commitTxn();
  251. return obj;
  252. }
  253. private void doUpdate(Long id, Update update) {
  254. HasNotNullConstraintsJPA obj;
  255. beginTxn();
  256. try {
  257. obj = em.find(HasNotNullConstraintsJPA.class, id);
  258. update.update(obj);
  259. em.persist(obj);
  260. commitTxn();
  261. fail("expected Exception");
  262. } catch (PersistenceException e) {
  263. // good
  264. }
  265. // make sure no changes made
  266. beginTxn();
  267. obj = em.find(HasNotNullConstraintsJPA.class, id);
  268. rollbackTxn();
  269. assertEquals(VAL_BOOL, obj.getBool());
  270. assertEquals(VAL_CHAR, obj.getC());
  271. assertEquals(VAL_BYTE, obj.getB());
  272. assertEquals(VAL_SHORT, obj.getS());
  273. assertEquals(VAL_INT, obj.getI());
  274. assertEquals(VAL_LONG, obj.getL());
  275. assertEquals(VAL_FLOAT, obj.getF());
  276. assertEquals(VAL_DOUBLE, obj.getD());
  277. assertEquals(VAL_STRING, obj.getStr());
  278. }
  279. private void doRemove(Long id, Update update) {
  280. beginTxn();
  281. HasNotNullConstraintsJPA obj = em.find(HasNotNullConstraintsJPA.class, id);
  282. update.update(obj);
  283. em.remove(obj);
  284. commitTxn();
  285. assertEquals(0, countForClass(HasNotNullConstraintsJPA.class));
  286. }
  287. private HasNotNullConstraintsJPA createHasNotNullConstraintsJPA(Boolean bool, Character c, Byte b,
  288. Short s, Integer i, Long l,
  289. Float f, Double d, String str) {
  290. HasNotNullConstraintsJPA pc = new HasNotNullConstraintsJPA();
  291. pc.setBool(bool);
  292. pc.setC(c);
  293. pc.setB(b);
  294. pc.setS(s);
  295. pc.setI(i);
  296. pc.setL(l);
  297. pc.setF(f);
  298. pc.setD(d);
  299. pc.setStr(str);
  300. return pc;
  301. }
  302. interface Update {
  303. void update(HasNotNullConstraintsJPA obj);
  304. }
  305. }