1   /*
2    * Joey and its relative products are published under the terms
3    * of the Apache Software License.
4    */
5   /*
6    * Created on 2004/03/02
7    */
8   package test.org.asyrinx.joey.om.hibernate;
9   
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  import org.asyrinx.brownie.core.lang.enum.ValuedEnum;
14  import org.asyrinx.brownie.core.lang.enum.ValuedEnumSet;
15  import org.asyrinx.brownie.core.sql.Operator;
16  import org.asyrinx.joey.om.AbstractSearchCondition;
17  import org.asyrinx.joey.om.hibernate.HqlTranslator;
18  
19  import junit.framework.TestCase;
20  
21  /***
22   * @author akima
23   */
24  public class HqlTranslatorTest extends TestCase {
25  
26  	/***
27  	 * Constructor for HqlTranslatorTest.
28  	 * @param name
29  	 */
30  	public HqlTranslatorTest(String name) {
31  		super(name);
32  	}
33  
34  	public static void main(String[] args) {
35  		junit.swingui.TestRunner.run(HqlTranslatorTest.class);
36  	}
37  
38  	public void testToHQL() {
39  		final TestCondition condition = new TestCondition();
40  		condition.setIntValueMax("999");
41  		condition.getIntEnumSet().getEntryByName("int1").setSelected(true);
42  		condition.getIntEnumSet().getEntryByName("int3").setSelected(true);
43  		final HqlTranslator translator =
44  			new HqlTranslator(new HashMap(), getTableMap());
45  		final String hql = translator.toHQL(condition);
46  		System.out.println(hql);
47  		assertEquals(
48  			"from testTable where intValue <= 999 and intEnum in (1, 3)",
49  			hql);
50  	}
51  
52  	private Map getTableMap() {
53  		final Map result = new HashMap();
54  		result.put("testTable", "testTable");
55  		return result;
56  	}
57  
58  }
59  class TestCondition extends AbstractSearchCondition {
60  
61  	private String intValueMin;
62  	private String intValueMax;
63  
64  	private String stringValue;
65  
66  	private ValuedEnumSet intEnumSet =
67  		new ValuedEnumSet(TestIntValuedEnum.class);
68  	private ValuedEnumSet stringEnumSet =
69  		new ValuedEnumSet(TestStringValuedEnum.class);
70  
71  	/***
72  	 * @see org.asyrinx.joey.om.AbstractSearchCondition#prepare()
73  	 */
74  	protected void prepare() {
75  		addFrom("testTable");
76  		addIntMin("intValue", intValueMin);
77  		addIntMax("intValue", intValueMax);
78  		addIntMax("stringValue", stringValue);
79  		addInt("intEnum", intEnumSet.getSelectedEnumValues(), Operator.IN);
80  		addString(
81  			"stringEnum",
82  			stringEnumSet.getSelectedEnumValues(),
83  			Operator.IN);
84  	}
85  
86  	/***
87  	 * @return
88  	 */
89  	public ValuedEnumSet getIntEnumSet() {
90  		return intEnumSet;
91  	}
92  
93  	/***
94  	 * @return
95  	 */
96  	public String getIntValueMax() {
97  		return intValueMax;
98  	}
99  
100 	/***
101 	 * @return
102 	 */
103 	public String getIntValueMin() {
104 		return intValueMin;
105 	}
106 
107 	/***
108 	 * @return
109 	 */
110 	public ValuedEnumSet getStringEnumSet() {
111 		return stringEnumSet;
112 	}
113 
114 	/***
115 	 * @return
116 	 */
117 	public String getStringValue() {
118 		return stringValue;
119 	}
120 
121 	/***
122 	 * @param set
123 	 */
124 	public void setIntEnumSet(ValuedEnumSet set) {
125 		intEnumSet = set;
126 	}
127 
128 	/***
129 	 * @param string
130 	 */
131 	public void setIntValueMax(String string) {
132 		intValueMax = string;
133 	}
134 
135 	/***
136 	 * @param string
137 	 */
138 	public void setIntValueMin(String string) {
139 		intValueMin = string;
140 	}
141 
142 	/***
143 	 * @param set
144 	 */
145 	public void setStringEnumSet(ValuedEnumSet set) {
146 		stringEnumSet = set;
147 	}
148 
149 	/***
150 	 * @param string
151 	 */
152 	public void setStringValue(String string) {
153 		stringValue = string;
154 	}
155 
156 }
157 class TestIntValuedEnum extends ValuedEnum {
158 	/***
159 	 * @param name
160 	 * @param value
161 	 */
162 	public TestIntValuedEnum(String name, int value) {
163 		super(name, new Integer(value));
164 	}
165 
166 	public static final TestIntValuedEnum VALUE_1 =
167 		new TestIntValuedEnum("int1", 1);
168 	public static final TestIntValuedEnum VALUE_2 =
169 		new TestIntValuedEnum("int2", 2);
170 	public static final TestIntValuedEnum VALUE_3 =
171 		new TestIntValuedEnum("int3", 3);
172 }
173 class TestStringValuedEnum extends ValuedEnum {
174 	/***
175 	 * @param name
176 	 * @param value
177 	 */
178 	public TestStringValuedEnum(String name, String value) {
179 		super(name, value);
180 	}
181 
182 	public static final TestStringValuedEnum VALUE_1 =
183 		new TestStringValuedEnum("stringA", "aaa");
184 	public static final TestStringValuedEnum VALUE_2 =
185 		new TestStringValuedEnum("stringB", "bbb");
186 	public static final TestStringValuedEnum VALUE_3 =
187 		new TestStringValuedEnum("stringC", "ccc");
188 }