KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: QueryByExampleTest.java,v 1.1 2004/09/26 05:18:25 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.util.List JavaDoc;
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 /**
19  * Query by example test to allow nested components
20  *
21  * @author Emmanuel Bernard
22  */

23 public class QueryByExampleTest extends TestCase {
24
25     /**
26      * @param name test name
27      */

28     public QueryByExampleTest(String JavaDoc name) {
29         super(name);
30     }
31
32     public void setUp() throws Exception JavaDoc {
33         super.setUp();
34     }
35
36     public void tearDown() throws Exception JavaDoc {
37         super.tearDown();
38     }
39
40     /**
41      * @see org.hibernate.test.TestCase#getMappings()
42      */

43     protected String JavaDoc[] getMappings() {
44         return new String JavaDoc[] { "legacy/Componentizable.hbm.xml" };
45     }
46
47     public void testSimpleQBE() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc name, String JavaDoc subname, String JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc {
155         TestRunner.run( suite() );
156     }
157 }
158
Popular Tags