1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web;
24  
25  import net.smartlab.web.page.Paginator;
26  import net.smartlab.web.test.BusinessObjectFactoryTestCase;
27  
28  import org.hibernate.Query;
29  import org.hibernate.Session;
30  
31  /**
32   * TODO documentation
33   * 
34   * @author gperrone
35   */
36  public class BusinessObjectFactoryPaginatorTest extends BusinessObjectFactoryTestCase {
37  
38  	private BusinessObjectFactory factory = null;
39  
40  
41  	protected void setUp() throws Exception {
42  		super.setUp();
43  		factory = MockFactory.getInstance();
44  	}
45  
46  	/*
47  	 * Test method for
48  	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.setPage(int)'
49  	 */
50  	public void testSetPage() {
51  	}
52  
53  	/*
54  	 * Test method for
55  	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.setSize(int)'
56  	 */
57  	public void testSetSize() {
58  	}
59  
60  	/*
61  	 * Test method for
62  	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.setArray()'
63  	 */
64  	public void testSetArray() {
65  	}
66  
67  	/*
68  	 * Test method for
69  	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.Paginator(Criteria)'
70  	 */
71  	public void testPaginatorCriteria() throws Exception {
72  		// Criteria criteria = factory.current().createCriteria(Group.class);
73  		// criteria.add(Expression.like("display", "test", MatchMode.START));
74  		// Paginator paginator = factory.new Paginator(criteria, 20, 5);
75  		// super.assertTrue(paginator.hasNext());
76  	}
77  
78  	/*
79  	 * Test method for
80  	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.Paginator(Criteria)'
81  	 * when a ResultTrasformer was specified for the criteria.
82  	 */
83  	public void testPaginatorCriteriaResultTransformer() throws Exception {
84  		/*
85  		 * Session session=factory.current(); Criteria criteria =
86  		 * session.createCriteria(Group.class); Paginator paginator =
87  		 * GroupFactory.getInstance().new Paginator(criteria, 20, 5); int
88  		 * paginatorCount = paginator.getCount(); Set set = new HashSet(); while
89  		 * (paginator.hasNext()) { Group g = (Group)paginator.next();
90  		 * g.getSize()); set.add(new Long(g.getId())); }
91  		 * super.assertEquals(paginatorCount, set.size());
92  		 */
93  	}
94  
95  	/*
96  	 * Test method for
97  	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.Paginator(Query)'
98  	 */
99  	public void testPaginatorQuery() throws Exception {
100 		// NO FILTER TEST
101 		Session session = factory.current();
102 		Query query = session.createQuery("select from Mock");
103 		Paginator paginator = factory.new Paginator(query, 20, 5);
104 		super.assertTrue(paginator.hasNext());
105 		// ORDER BY TEST
106 		query = session.createQuery("select from Mock order by code");
107 		paginator = factory.new Paginator(query, 20, 5);
108 		super.assertTrue(paginator.hasNext());
109 		// filter test
110 		query = session.createQuery("select from Mock where code LIKE :code");
111 		query.setString("code", "code%");
112 		paginator = factory.new Paginator(query, 20, 5);
113 		super.assertTrue(paginator.hasNext());
114 		// multiple filter test
115 		query = session
116 				.createQuery("select from Code where code like :code and id>:id order by code");
117 		query.setString("code", "code%");
118 		query.setInteger("id", 1);
119 		paginator = factory.new Paginator(query, 20, 5);
120 		super.assertTrue(paginator.hasNext());
121 	}
122 
123 	public void testPaginatorSQLQuery() throws Exception {
124 		Session session = factory.current();
125 		// NO FILTER TEST
126 		Query query = session.createSQLQuery("select * from mock");
127 		Paginator paginator = factory.new Paginator(query, 20, 5);
128 		super.assertTrue(paginator.hasNext());
129 		// ORDER BY TEST
130 		query = session.createSQLQuery("select * from mock order by code");
131 		paginator = factory.new Paginator(query, 20, 5);
132 		super.assertTrue(paginator.hasNext());
133 		// varchar filter test
134 		query = session.createSQLQuery("select * from mock where code like ?");
135 		query.setString(0, "code%");
136 		paginator = factory.new Paginator(query, 20, 5);
137 		super.assertTrue(paginator.hasNext());
138 		// multiple filter test
139 		query = session
140 				.createSQLQuery("select * from mock where code like :code and id > :id order by code");
141 		query.setString("code", "code%");
142 		query.setInteger("id", 1);
143 		paginator = factory.new Paginator(query, 20, 5);
144 		super.assertTrue(paginator.hasNext());
145 	}
146 
147 	/*
148 	 * Test method for
149 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.Paginator(Query, int,
150 	 * int)'
151 	 */
152 	public void testPaginatorQueryIntInt() throws Exception {
153 		//		
154 		// Group group = (Group)factory.findByKey("5");
155 		// for (int i = 0; i < 10000; i++) {
156 		// Group g = new Group();
157 		// g.setDisplay("test" + "-" + i);
158 		// Iterator roles = group.getRoles().iterator();
159 		// while (roles.hasNext()) {
160 		// g.add((Role)roles.next());
161 		// }
162 		// Iterator users = group.getUsers().iterator();
163 		// while (users.hasNext()) {
164 		// g.add((User)users.next());
165 		// }
166 		// factory.update(g);
167 		// }
168 		//		
169 	}
170 
171 	/*
172 	 * Test method for
173 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.Paginator(Criteria,
174 	 * int, int)'
175 	 */
176 	public void testPaginatorCriteriaIntInt() {
177 		fail("Not yet implemented");
178 	}
179 
180 	/*
181 	 * Test method for
182 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.add(Object)'
183 	 */
184 	public void testAdd() {
185 		fail("Not yet implemented");
186 	}
187 
188 	/*
189 	 * Test method for
190 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.addAll(Collection)'
191 	 */
192 	public void testAddAll() {
193 		fail("Not yet implemented");
194 	}
195 
196 	/*
197 	 * Test method for
198 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.clear()'
199 	 */
200 	public void testClear() {
201 		fail("Not yet implemented");
202 	}
203 
204 	/*
205 	 * Test method for
206 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.contains(Object)'
207 	 */
208 	public void testContains() {
209 		fail("Not yet implemented");
210 	}
211 
212 	/*
213 	 * Test method for
214 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.containsAll(Collection)'
215 	 */
216 	public void testContainsAll() {
217 		fail("Not yet implemented");
218 	}
219 
220 	/*
221 	 * Test method for
222 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.isEmpty()'
223 	 */
224 	public void testIsEmpty() {
225 		fail("Not yet implemented");
226 	}
227 
228 	/*
229 	 * Test method for
230 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.iterator()'
231 	 */
232 	public void testIterator() {
233 		fail("Not yet implemented");
234 	}
235 
236 	/*
237 	 * Test method for
238 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.remove(Object)'
239 	 */
240 	public void testRemoveObject() {
241 	}
242 
243 	/*
244 	 * Test method for
245 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.removeAll(Collection)'
246 	 */
247 	public void testRemoveAll() {
248 		fail("Not yet implemented");
249 	}
250 
251 	/*
252 	 * Test method for
253 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.retainAll(Collection)'
254 	 */
255 	public void testRetainAll() {
256 		fail("Not yet implemented");
257 	}
258 
259 	/*
260 	 * Test method for 'net.smartlab.web.BusinessObjectFactory.Paginator.size()'
261 	 */
262 	public void testSize() {
263 		fail("Not yet implemented");
264 	}
265 
266 	/*
267 	 * Test method for
268 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.toArray()'
269 	 */
270 	public void testToArray() {
271 		fail("Not yet implemented");
272 	}
273 
274 	/*
275 	 * Test method for
276 	 * 'net.smartlab.web.BusinessObjectFactory.Paginator.toArray(Object[])'
277 	 */
278 	public void testToArrayObjectArray() {
279 		fail("Not yet implemented");
280 	}
281 
282 	/**
283 	 * @see net.smartlab.web.test.BusinessObjectFactoryTestCase#getDataSetFile()
284 	 */
285 	protected String getDataSetFile() {
286 		return "res/dataset.xml";
287 	}
288 
289 	/**
290 	 * @hibernate.class table="mock"
291 	 */
292 	public static class Mock extends BusinessObject {
293 
294 		private static final long serialVersionUID = 1L;
295 
296 		private long id;
297 
298 		private String code;
299 
300 		public void setId(long id) {
301 			this.id = id;
302 		}
303 
304 		/**
305 		 * @hibernate.id generator-class="native"
306 		 */
307 		public long getId() {
308 			return id;
309 		}
310 
311 		public void setCode(String code) {
312 			this.code = code;
313 		}
314 
315 		/**
316 		 * @hibernate.property 
317 		 */
318 		public String getCode() {
319 			return code;
320 		}
321 	}
322 	
323 	public static class MockFactory extends BusinessObjectFactory {
324 
325 		public Class getMappedClass() {
326 			return Mock.class;
327 		}
328 
329 		public static BusinessObjectFactory getInstance() {
330 			return new MockFactory();
331 		}
332 	}
333 }