PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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