1 package org.hibernate.test.legacy; 3 4 import java.util.List ; 5 6 import junit.framework.Test; 7 import junit.framework.TestSuite; 8 import junit.textui.TestRunner; 9 import org.hibernate.Criteria; 10 import org.hibernate.classic.Session; 11 import org.hibernate.criterion.Example; 12 import org.hibernate.criterion.Expression; 13 import org.hibernate.Transaction; 14 import org.hibernate.dialect.HSQLDialect; 15 import org.hibernate.test.TestCase; 16 17 18 23 public class QueryByExampleTest extends TestCase { 24 25 28 public QueryByExampleTest(String name) { 29 super(name); 30 } 31 32 public void setUp() throws Exception { 33 super.setUp(); 34 } 35 36 public void tearDown() throws Exception { 37 super.tearDown(); 38 } 39 40 43 protected String [] getMappings() { 44 return new String [] { "legacy/Componentizable.hbm.xml" }; 45 } 46 47 public void testSimpleQBE() throws Exception { 48 deleteData(); 49 initData(); 50 51 Session s = openSession(); 52 53 Transaction t = s.beginTransaction(); 54 Componentizable master = getMaster("hibernate", "open sourc%", "open source1"); 55 Criteria crit = s.createCriteria(Componentizable.class); 56 Example ex = Example.create(master).enableLike(); 57 crit.add(ex); 58 List result = crit.list(); 59 assertNotNull(result); 60 assertEquals(1, result.size()); 61 62 t.commit(); 63 s.close(); 64 } 65 66 public void testJunctionNotExpressionQBE() throws Exception { 67 deleteData(); 68 initData(); 69 Session s = openSession(); 70 Transaction t = s.beginTransaction(); 71 Componentizable master = getMaster("hibernate", null, "ope%"); 72 Criteria crit = s.createCriteria(Componentizable.class); 73 Example ex = Example.create(master).enableLike(); 74 75 crit.add(Expression.or(Expression.not(ex), ex)); 76 77 List result = crit.list(); 78 assertNotNull(result); 79 if ( !(getDialect() instanceof HSQLDialect) ) assertEquals(2, result.size()); 80 t.commit(); 81 s.close(); 82 83 } 84 85 public void testExcludingQBE() throws Exception { 86 deleteData(); 87 initData(); 88 Session s = openSession(); 89 Transaction t = s.beginTransaction(); 90 Componentizable master = getMaster("hibernate", null, "ope%"); 91 Criteria crit = s.createCriteria(Componentizable.class); 92 Example ex = Example.create(master).enableLike() 93 .excludeProperty("component.subComponent"); 94 crit.add(ex); 95 List result = crit.list(); 96 assertNotNull(result); 97 assertEquals(3, result.size()); 98 99 master = getMaster("hibernate", "ORM tool", "fake stuff"); 100 crit = s.createCriteria(Componentizable.class); 101 ex = Example.create(master).enableLike() 102 .excludeProperty("component.subComponent.subName1"); 103 crit.add(ex); 104 result = crit.list(); 105 assertNotNull(result); 106 assertEquals(1, result.size()); 107 t.commit(); 108 s.close(); 109 110 111 } 112 113 private void initData() throws Exception { 114 Session s = openSession(); 115 Transaction t = s.beginTransaction(); 116 Componentizable master = getMaster("hibernate", "ORM tool", "ORM tool1"); 117 s.saveOrUpdate(master); 118 master = getMaster("hibernate", "open source", "open source1"); 119 s.saveOrUpdate(master); 120 master = getMaster("hibernate", null, null); 121 s.saveOrUpdate(master); 122 t.commit(); 123 s.close(); 124 } 125 126 private void deleteData() throws Exception { 127 Session s = openSession(); 128 Transaction t = s.beginTransaction(); 129 s.delete("from Componentizable"); 130 t.commit(); 131 s.close(); 132 } 133 134 private Componentizable getMaster(String name, String subname, String subname1) { 135 Componentizable master = new Componentizable(); 136 if (name != null) { 137 Component masterComp = new Component(); 138 masterComp.setName(name); 139 if (subname != null || subname1 != null) { 140 SubComponent subComponent = new SubComponent(); 141 subComponent.setSubName(subname); 142 subComponent.setSubName1(subname1); 143 masterComp.setSubComponent(subComponent); 144 } 145 master.setComponent(masterComp); 146 } 147 return master; 148 } 149 150 public static Test suite() { 151 return new TestSuite(QueryByExampleTest.class); 152 } 153 154 public static void main(String [] args) throws Exception { 155 TestRunner.run( suite() ); 156 } 157 } 158 | Popular Tags |