01: package org.apache.ojb.broker;
02:
03: import org.apache.ojb.broker.util.ObjectModification;
04: import org.apache.ojb.junit.PBTestCase;
05:
06: /**
07: * @author Matthew Baird
08: *
09: */
10: public class KeyConstraintViolationTest extends PBTestCase {
11: public static void main(String[] args) {
12: String[] arr = { KeyConstraintViolationTest.class.getName() };
13: junit.textui.TestRunner.main(arr);
14: }
15:
16: public KeyConstraintViolationTest(String name) {
17: super (name);
18: }
19:
20: /**
21: * Test creating two objects with the same ID, should fail with
22: * key constraint error
23: **/
24: public void testKeyViolation() throws Exception {
25: broker.beginTransaction();
26: Article obj = new Article();
27: obj.setProductGroupId(new Integer(1));
28: obj.articleName = "repeated Article";
29: // storing once should be ok.
30: broker.store(obj, ObjectModification.INSERT);
31: broker.commitTransaction();
32:
33: broker.clearCache();
34: try {
35: broker.beginTransaction();
36: Article obj2 = new Article();
37: obj2.articleId = obj.getArticleId();
38: obj2.setProductGroupId(new Integer(1));
39: obj2.articleName = "repeated Article";
40:
41: // store it again!
42: broker.store(obj2, ObjectModification.INSERT);
43: broker.commitTransaction();
44:
45: fail("Should have thrown a KeyConstraintViolatedException");
46: } catch (KeyConstraintViolatedException t) {
47: // this is a success.
48: broker.abortTransaction();
49: }
50: }
51: }
|