KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > KeyConstraintViolationTest


1 package org.apache.ojb.broker;
2
3 import org.apache.ojb.broker.util.ObjectModification;
4 import org.apache.ojb.junit.PBTestCase;
5
6 /**
7  * @author Matthew Baird
8  *
9  */

10 public class KeyConstraintViolationTest extends PBTestCase
11 {
12     public static void main(String JavaDoc[] args)
13     {
14         String JavaDoc[] arr = {KeyConstraintViolationTest.class.getName()};
15         junit.textui.TestRunner.main(arr);
16     }
17
18     public KeyConstraintViolationTest(String JavaDoc name)
19     {
20         super(name);
21     }
22
23     /**
24      * Test creating two objects with the same ID, should fail with
25      * key constraint error
26      **/

27     public void testKeyViolation() throws Exception JavaDoc
28     {
29         broker.beginTransaction();
30         Article obj = new Article();
31         obj.setProductGroupId(new Integer JavaDoc(1));
32         obj.articleName = "repeated Article";
33         // storing once should be ok.
34
broker.store(obj, ObjectModification.INSERT);
35         broker.commitTransaction();
36
37         broker.clearCache();
38         try
39         {
40             broker.beginTransaction();
41             Article obj2 = new Article();
42             obj2.articleId = obj.getArticleId();
43             obj2.setProductGroupId(new Integer JavaDoc(1));
44             obj2.articleName = "repeated Article";
45
46             // store it again!
47
broker.store(obj2, ObjectModification.INSERT);
48             broker.commitTransaction();
49
50             fail("Should have thrown a KeyConstraintViolatedException");
51         }
52         catch (KeyConstraintViolatedException t)
53         {
54             // this is a success.
55
broker.abortTransaction();
56         }
57     }
58 }
59
Popular Tags