KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > ComponentNotNullTest


1 //{Id:}
2
package org.hibernate.test.legacy;
3
4 import java.util.ArrayList JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8 import junit.textui.TestRunner;
9 import org.hibernate.PropertyValueException;
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.hibernate.test.TestCase;
13
14 /**
15  * Test some cases of not-null properties inside components.
16  *
17  * @author Emmanuel Bernard
18  */

19 public class ComponentNotNullTest extends TestCase {
20
21     public ComponentNotNullTest(String JavaDoc x) {
22         super(x);
23     }
24
25     /**
26      * @see org.hibernate.test.TestCase#getMappings()
27      */

28     protected String JavaDoc[] getMappings() {
29         return new String JavaDoc[] {
30             "legacy/ComponentNotNullMaster.hbm.xml",
31             "legacy/One.hbm.xml",
32             "legacy/Many.hbm.xml",
33             "legacy/Simple.hbm.xml" };
34     }
35
36     public static Test suite() {
37         return new TestSuite(ComponentNotNullTest.class);
38     }
39
40     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
41         TestRunner.run(suite());
42     }
43
44     public void testComponentNotNull() throws Exception JavaDoc {
45
46         //everything not null
47
//
48
Session s = openSession();
49         Transaction t = s.beginTransaction();
50         ComponentNotNullMaster master = new ComponentNotNullMaster();
51         ComponentNotNull nullable = new ComponentNotNull();
52         ComponentNotNull supercomp = new ComponentNotNull();
53         ComponentNotNull subcomp = new ComponentNotNull();
54
55         master.setNullable(nullable);
56         subcomp.setProp1Subcomp("test");
57         supercomp.setSubcomp(subcomp);
58         master.setSupercomp(supercomp);
59         try {
60             s.save(master);
61             t.commit();
62         } catch (PropertyValueException e) {
63             fail("Inserting non null components should not fail\n" + e);
64         }
65         t.rollback();
66         s.close();
67
68         //null prop of a subcomp
69
//
70
s = openSession();
71         t = s.beginTransaction();
72
73         master = new ComponentNotNullMaster();
74         nullable = new ComponentNotNull();
75         supercomp = new ComponentNotNull();
76         subcomp = new ComponentNotNull();
77
78         master.setNullable(nullable);
79         // do not set property
80
//subcomp.setProp1Subcomp("test");
81
supercomp.setSubcomp(subcomp);
82         master.setSupercomp(supercomp);
83
84
85         try {
86             s.save(master);
87             t.commit();
88             fail("Inserting not-null null property should fail");
89         } catch (PropertyValueException e) {
90             //succeed
91
}
92         t.rollback();
93         s.close();
94
95         //null component having not-null column
96
//
97
s = openSession();
98         t = s.beginTransaction();
99
100         master = new ComponentNotNullMaster();
101         nullable = new ComponentNotNull();
102         supercomp = new ComponentNotNull();
103         subcomp = new ComponentNotNull();
104
105         master.setNullable(nullable);
106         // do not set supercomp for master
107
//subcomp.setProp1Subcomp("test");
108
//supercomp.setSubcomp(subcomp);
109
//master.setSupercomp(supercomp);
110

111
112         try {
113             s.save(master);
114             t.commit();
115             fail("Inserting not-null null property should fail");
116         } catch (PropertyValueException e) {
117             //succeed
118
}
119         t.rollback();
120         s.close();
121     }
122
123     public void testCompositeElement() throws Exception JavaDoc {
124         //composite-element nullable
125
Session s = openSession();
126         Transaction t = s.beginTransaction();
127         ComponentNotNullMaster master = new ComponentNotNullMaster();
128         ComponentNotNull nullable = new ComponentNotNull();
129         ComponentNotNull supercomp = new ComponentNotNull();
130         ComponentNotNull subcomp = new ComponentNotNull();
131
132         master.setNullable(nullable);
133         subcomp.setProp1Subcomp("test");
134         supercomp.setSubcomp(subcomp);
135         master.setSupercomp(supercomp);
136
137         master.setComponents(new ArrayList JavaDoc());
138         ComponentNotNullMaster.ContainerInnerClass cc =
139             new ComponentNotNullMaster.ContainerInnerClass();
140         master.getComponents().add(cc);
141
142         try {
143             s.save(master);
144             t.commit();
145             fail("Inserting not-null many-to-one should fail");
146         } catch (PropertyValueException e) {
147             //success
148
}
149         t.rollback();
150         s.close();
151
152         //null nested component having not-null column
153
//
154
s = openSession();
155         t = s.beginTransaction();
156
157         master = new ComponentNotNullMaster();
158         nullable = new ComponentNotNull();
159         supercomp = new ComponentNotNull();
160         subcomp = new ComponentNotNull();
161
162         master.setNullable(nullable);
163         subcomp.setProp1Subcomp("test");
164         supercomp.setSubcomp(subcomp);
165         master.setSupercomp(supercomp);
166
167         master.setComponentsImplicit(new ArrayList JavaDoc());
168         ComponentNotNullMaster.ContainerInnerClass nestedCc =
169             new ComponentNotNullMaster.ContainerInnerClass();
170         cc =
171             new ComponentNotNullMaster.ContainerInnerClass();
172         cc.setNested(nestedCc);
173         master.getComponentsImplicit().add(cc);
174
175         try {
176             s.save(master);
177             t.commit();
178             fail("Inserting not-null null property should fail");
179         } catch (PropertyValueException e) {
180             //succeed
181
}
182         t.rollback();
183         s.close();
184
185         //nested component having not-null column
186
//
187
s = openSession();
188         t = s.beginTransaction();
189
190         master = new ComponentNotNullMaster();
191         nullable = new ComponentNotNull();
192         supercomp = new ComponentNotNull();
193         subcomp = new ComponentNotNull();
194
195         master.setNullable(nullable);
196         subcomp.setProp1Subcomp("test");
197         supercomp.setSubcomp(subcomp);
198         master.setSupercomp(supercomp);
199
200         master.setComponentsImplicit(new ArrayList JavaDoc());
201         nestedCc =
202             new ComponentNotNullMaster.ContainerInnerClass();
203         cc =
204             new ComponentNotNullMaster.ContainerInnerClass();
205         cc.setNested(nestedCc);
206         nestedCc.setNestedproperty("test");
207         master.getComponentsImplicit().add(cc);
208
209         try {
210             s.save(master);
211             t.commit();
212         } catch (PropertyValueException e) {
213             fail("Inserting not-null nested components should not fail");
214         }
215         t.rollback();
216         s.close();
217     }
218
219 }
220
Popular Tags