1 package org.hibernate.test.legacy; 3 4 import java.util.ArrayList ; 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 19 public class ComponentNotNullTest extends TestCase { 20 21 public ComponentNotNullTest(String x) { 22 super(x); 23 } 24 25 28 protected String [] getMappings() { 29 return new String [] { 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 [] args) throws Exception { 41 TestRunner.run(suite()); 42 } 43 44 public void testComponentNotNull() throws Exception { 45 46 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 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 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 } 92 t.rollback(); 93 s.close(); 94 95 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 111 112 try { 113 s.save(master); 114 t.commit(); 115 fail("Inserting not-null null property should fail"); 116 } catch (PropertyValueException e) { 117 } 119 t.rollback(); 120 s.close(); 121 } 122 123 public void testCompositeElement() throws Exception { 124 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 ()); 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 } 149 t.rollback(); 150 s.close(); 151 152 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 ()); 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 } 182 t.rollback(); 183 s.close(); 184 185 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 ()); 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 |