View Javadoc
1   /**
2    * Copyright (c) 2009-2010 by Glodon
3    * All rights reserved.
4    */
5   package gboat2.base.dao.impl;
6   
7   import gboat2.base.core.dao.QuerySupport;
8   
9   import java.util.HashMap;
10  import java.util.Map;
11  
12  import junit.framework.TestCase;
13  
14  /**
15   * @author lysming
16   * 2010-2-24
17   */
18  public class QuerySupportTest extends TestCase {
19  	public void setUp(){
20  		
21  	}
22  	/**
23  	 * Test method for {@link com.glodon.gets.dao.QuerySupport#generateHQL(java.util.HashMap)}.
24  	 */
25  	public void testGenerateHQLForException() {
26  		Map<String,Object> params = new HashMap<String,Object>();
27  		try{
28  			new GenerateQL().generateHQL(params,null);
29  			fail("RuntimeExceptin isn't throwed while the table isn't setted.");
30  		}catch(RuntimeException e){
31  			assertTrue(true);
32  		}
33  		
34  		params.put(QuerySupport.PARAM_TABLENAME, "NoExistClass");
35  		try{
36  			new GenerateQL().generateHQL(params,null);
37  			fail("RuntimeExceptin isn't throwed while the PO isn't existed.");
38  		}catch(RuntimeException e){
39  			assertTrue(true);
40  		}
41  		
42  		params.put(QuerySupport.PARAM_TABLENAME, "com.glodon.gets.qms.util.TestClazz");
43  		params.put("privateField_invalidOpera", "randomValue");
44  		try{
45  			new GenerateQL().generateHQL(params,null);
46  			fail("RuntimeExceptin isn't throwed while the opera is invalid.");
47  		}catch(RuntimeException e){
48  			assertTrue(true);
49  		}
50  	}
51  	
52  	public void testGenerateHQLOnlyTable(){
53  		GenerateQL qm = new GenerateQL();
54  		Map<String,Object> params = new HashMap<String,Object>();
55  		params.put(QuerySupport.PARAM_TABLENAME, "gboat2.base.dao.impl.TGroup");
56  		
57  		String expected = " from gboat2.base.dao.impl.TGroup";
58  		assertEquals(qm.generateHQL(params,null), expected);
59  		
60  		params.put(QuerySupport.PARAM_TABLENAME, "TGroup");
61  		assertEquals(qm.generateHQL(params,null), " from gboat2.base.dao.impl.TGroup");
62  		
63  		assertEquals(qm.getQueryParamsAsUrlString(params),"");
64  	}
65  	
66  	public void testGenerateHQLWithOrderby(){
67  		GenerateQL qm = new GenerateQL();
68  		Map<String,Object> params = new HashMap<String,Object>();
69  		params.put(QuerySupport.PARAM_TABLENAME, "gboat2.base.dao.impl.TGroup");
70  		params.put(QuerySupport.PARAM_ORDERBY, "name desc");
71  		String expected = " from gboat2.base.dao.impl.TGroup order by name desc";
72  		assertEquals(qm.generateHQL(params,null), expected);
73  	}
74  	
75  	public void testGenerateHQLWithFields(){
76  		GenerateQL qm = new GenerateQL();
77  		Map<String,Object> params = new HashMap<String,Object>();
78  		params.put(QuerySupport.PARAM_TABLENAME, "CCity");
79  		params.put("_cityname", "%南通%");
80  		params.put("_citytype", "2");
81  		params.put("_Provice", "江苏");
82  		params.put("_citycode", "");
83  		params.put("_useless", "该参数应该忽略");
84  		String expected = " from gboat2.base.dao.impl.CCity where cityname like '%南通%' " +
85  				"and citytype = :_citytype and provice = :_Provice";
86  		assertEquals(qm.generateHQL(params,null), expected);
87  		
88  		assertEquals(qm.getQueryParamsAsUrlString(params),"_cityname=%25%E5%8D%97%E9%80%9A%25&_citycode=&_useless=%E8%AF%A5%E5%8F%82%E6%95%B0%E5%BA%94%E8%AF%A5%E5%BF%BD%E7%95%A5&_citytype=2&_Provice=%E6%B1%9F%E8%8B%8F");
89  	}
90  
91  	public void testGenerateHQLWithFieldOperas(){
92  		Map<String,Object> params = new HashMap<String,Object>();
93  		params.put(QuerySupport.PARAM_TABLENAME, "gboat2.base.dao.impl.TestClazz");
94  		params.put("_stringParam_like", "南通");
95  		params.put("_publicParam_like", "%ed");
96  		params.put("_intParam_le", "2");
97  		params.put("_privateParam_ne", "");
98  		params.put("_doubleParam_ge", "3.5");
99  		params.put("_dateParam_gt","2010-2-23");
100 		String expected = " from gboat2.base.dao.impl.TestClazz " +
101 				"where doubleParam >= :_doubleParam_ge and intParam <= :_intParam_le " +
102 				"and to_char(dateParam,'yyyy-MM-dd') > '2010-2-23' and stringParam like '%南通%'";
103 		assertEquals(new GenerateQL().generateHQL(params,null), expected);
104 	}
105 	
106 	public void testGenerateHQLWithOperaOr1(){
107 		Map<String,Object> params = new HashMap<String,Object>();
108 		params.put(QuerySupport.PARAM_TABLENAME, "gboat2.base.dao.impl.TestClazz");
109 		params.put("_stringParam_or", "||valueseted");
110 		String expected = " from gboat2.base.dao.impl.TestClazz " +
111 				"where (stringParam is null or stringParam = valueseted)";
112 		assertEquals(new GenerateQL().generateHQL(params,null),expected);
113 	}
114 	
115 	public void testGenerateHQLWithOperaOr2(){
116 		Map<String,Object> params = new HashMap<String,Object>();
117 		params.put(QuerySupport.PARAM_TABLENAME, "gboat2.base.dao.impl.TestClazz");
118 		params.put("_stringParam_or", "null||valueseted");
119 		String expected = " from gboat2.base.dao.impl.TestClazz " +
120 				"where (stringParam is null or stringParam = valueseted)";
121 		assertEquals(new GenerateQL().generateHQL(params,null),expected);
122 	}
123 	
124 	public void testGenerateHQLWithOperaOr3(){
125 		Map<String,Object> params = new HashMap<String,Object>();
126 		params.put(QuerySupport.PARAM_TABLENAME, "gboat2.base.dao.impl.TestClazz");
127 		params.put("_stringParam_or", "value1||value2");
128 		params.put("_intParam_le", "2");
129 		String expected = " from gboat2.base.dao.impl.TestClazz " +
130 				"where (stringParam = value1 or stringParam = value2) and intParam <= :_intParam_le";
131 		assertEquals(new GenerateQL().generateHQL(params,null),expected);
132 	}
133 	
134 	public void testGenerateHQLFromVO(){
135 		Map<String,Object> params = new HashMap<String,Object>();
136 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
137 		String expected = "select TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
138 				"from t_po1 TestPoA0_ inner join t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id";
139 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
140 	}
141 	
142 	public void testGenerateHQLFromVO2(){
143 		Map<String,Object> params = new HashMap<String,Object>();
144 		params.put(QuerySupport.PARAM_TABLENAME, TestVO2.class.getName());
145 		String expected = "select TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
146 				"from t_po1 TestPoA0_ inner join t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id "+
147 				"left join C_CITY CCity0_ on TestPoB0_.field12=CCity0_.CITYID";
148 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
149 	}
150 	
151 	public void testGenerateHQLFromVOWithFieldOperas(){
152 		Map<String,Object> params = new HashMap<String,Object>();
153 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
154 		params.put("_field01", "equalValue");
155 		params.put("_field11_or","||value1");
156 		String expected = "select TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
157 				"from t_po1 TestPoA0_ inner join t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id " +
158 				"where TestPoA0_.field01 = :field01 " +
159 				"and (TestPoB0_.field11 is null or TestPoB0_.field11 = value1)";
160 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
161 	}
162 	
163 	public void testGenerateSQLWithOrderby(){
164 		Map<String,Object> params = new HashMap<String,Object>();
165 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
166 		params.put("_field01", "equalValue");
167 		params.put(QuerySupport.PARAM_ORDERBY, "field11");
168 		String expected = "select TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
169 				"from t_po1 TestPoA0_ inner join t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id " +
170 				"where TestPoA0_.field01 = :field01 order by field11";
171 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
172 	}
173 	
174 	public void testGenerateSQLWithOrderby2(){
175 		Map<String,Object> params = new HashMap<String,Object>();
176 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
177 		params.put("_field01", "equalValue");
178 		params.put(QuerySupport.PARAM_ORDERBY, "field11   desc");
179 		String expected = "select TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
180 				"from t_po1 TestPoA0_ inner join t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id " +
181 				"where TestPoA0_.field01 = :field01 order by field11 desc";
182 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
183 	}
184 	
185 	public void testGenerateSQLWithOrderbyWithException(){
186 		Map<String,Object> params = new HashMap<String,Object>();
187 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
188 		params.put("_field01", "equalValue");
189 		params.put(QuerySupport.PARAM_ORDERBY, "TestPoB.nofield   desc");
190 		try{
191 			new GenerateQL().generateSQL(params,null);
192 			fail("RuntimeException should be thrown.");
193 		}catch(Exception e){
194 			assertTrue(true);
195 		}
196 	}
197 	
198 	public void testGenerateSQLWithOrderbyWithException2(){
199 		Map<String,Object> params = new HashMap<String,Object>();
200 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
201 		params.put("field01", "equalValue");
202 		params.put(QuerySupport.PARAM_ORDERBY, "TestPoB.field11.more desc");
203 		try{
204 			new GenerateQL().generateSQL(params,null);
205 			fail("RuntimeException should be thrown.");
206 		}catch(Exception e){
207 			assertTrue(true);
208 		}
209 	}
210 	
211 	public void testGenerateSQLWithOrderby_POfield(){
212 		Map<String,Object> params = new HashMap<String,Object>();
213 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
214 		params.put("_field01", "equalValue");
215 		params.put(QuerySupport.PARAM_ORDERBY, "TestPoB.field11   desc");
216 		String expected = "select TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
217 				"from t_po1 TestPoA0_ inner join t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id " +
218 				"where TestPoA0_.field01 = :field01 order by TestPoB0_.field11 desc";
219 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
220 	}
221 	
222 	public void testGenerateSQLWithOrderby_VOfield(){
223 		Map<String,Object> params = new HashMap<String,Object>();
224 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
225 		params.put("_field01", "equalValue");
226 		params.put(QuerySupport.PARAM_ORDERBY, "TestVO.field11   desc");
227 		String expected = "select TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
228 				"from t_po1 TestPoA0_ inner join t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id " +
229 				"where TestPoA0_.field01 = :field01 order by TestPoB0_.field11 desc";
230 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
231 	}
232 	
233 	public void testGenerateSQLWithTop_VOfield_Int(){
234 		Map<String,Object> params = new HashMap<String,Object>();
235 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
236 		params.put(QuerySupport.PARAM_TOP, 1);
237 		String expected = "select  top 1 TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
238 				"from common.t_po1 TestPoA0_ inner join common.t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id";
239 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
240 	}
241 	
242 	public void testGenerateSQLWithTop_VOfield_String(){
243 		Map<String,Object> params = new HashMap<String,Object>();
244 		params.put(QuerySupport.PARAM_TABLENAME, TestVO.class.getName());
245 		params.put(QuerySupport.PARAM_TOP, "1");
246 		String expected = "select  top 1 TestPoB0_.po1_id as id,TestPoA0_.field01 as field01,TestPoB0_.field11 as field11 " +
247 				"from common.t_po1 TestPoA0_ inner join common.t_po2 TestPoB0_ on TestPoA0_.id=TestPoB0_.po1_id";
248 		assertEquals(new GenerateQL().generateSQL(params,null),expected);
249 	}	
250 }