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 java.io.FileReader;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collection;
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.Set;
32  
33  import net.smartlab.web.DataAccessObject.SearchInfo;
34  import net.smartlab.web.test.BusinessObjectFactoryTestCase;
35  import net.smartlab.web.BusinessObjectFactory.Paginator;
36  
37  import org.dbunit.dataset.IDataSet;
38  import org.dbunit.dataset.ITable;
39  import org.dbunit.dataset.xml.FlatXmlDataSet;
40  import org.dbunit.operation.DatabaseOperation;
41  
42  /**
43   * @TODO documentation
44   * @author rlogiacco@smartlab.net
45   */
46  public class BusinessObjectFactoryTest extends BusinessObjectFactoryTestCase {
47  
48  	private SampleFactory factory;
49  
50  
51  	/**
52  	 * @see junit.framework.TestCase#setUp()
53  	 */
54  	protected void setUp() throws Exception {
55  		super.setUp();
56  		factory = SampleFactory.getInstance();
57  	}
58  
59  	/**
60  	 * Test method for {@link net.smartlab.web.BusinessObjectFactory#close()}.
61  	 */
62  	public final void testClose() throws Exception {
63  		super.assertFalse(factory.factory.isClosed());
64  		
65  		// Remove all Sample rows
66  		Iterator samples = factory.list(null).iterator();
67  		while (samples.hasNext()) {
68  			factory.remove(samples.next());
69  		}
70  
71  		super.assertTrue(factory.current().isDirty());
72  		
73  		// Fetch database data before closing
74          ITable before = getConnection().createDataSet().getTable("PUBLIC.sample");
75          super.assertEquals(20, before.getRowCount());
76  		
77          BusinessObjectFactory.close();
78  		
79          // Fetch database data after closing
80          ITable after = getConnection().createDataSet().getTable("PUBLIC.sample");
81  		super.assertEquals(0, after.getRowCount());
82  		
83  		super.assertFalse(factory.current().isDirty());
84  		super.assertFalse(factory.factory.isClosed());
85  	}
86  
87  	/**
88  	 * Test method for {@link net.smartlab.web.BusinessObjectFactory#flush()}.
89  	 */
90  	public final void testFlush() throws Exception {
91  		// Remove all Sample rows
92  		Iterator samples = factory.list(null).iterator();
93  		while (samples.hasNext()) {
94  			factory.remove(samples.next());
95  		}
96  		
97  		super.assertTrue(factory.current().isDirty());
98  
99  		// Fetch database data before closing
100         ITable before = getConnection().createDataSet().getTable("PUBLIC.sample");
101         super.assertEquals(20, before.getRowCount());
102 		
103         BusinessObjectFactory.flush();
104 		
105         // Fetch database data after closing
106         ITable after = getConnection().createDataSet().getTable("PUBLIC.sample");
107 		super.assertEquals(0, after.getRowCount());
108 		
109 		super.assertFalse(factory.factory.isClosed());
110 		super.assertFalse(factory.current().isDirty());
111 
112 	}
113 
114 	/**
115 	 * Test method for
116 	 * {@link net.smartlab.web.BusinessObjectFactory#BusinessObjectFactory()}.
117 	 */
118 	public final void testBusinessObjectFactory() {
119 		fail("Not yet implemented");
120 	}
121 
122 	/**
123 	 * Test method for {@link net.smartlab.web.BusinessObjectFactory#current()}.
124 	 */
125 	public final void testCurrent() throws Exception {
126 		super.assertNotNull(factory.current());
127 	}
128 
129 	/**
130 	 * Test method for
131 	 * {@link net.smartlab.web.BusinessObjectFactory#findByKey(java.io.Serializable)}
132 	 * .
133 	 */
134 	public final void testFindByKeySerializable() throws Exception {
135 		super.assertNotNull(factory.findByKey("1"));
136 		super.assertNotNull(factory.findByKey("0"));
137 		super.assertNotNull(factory.findByKey(null));
138 		super.assertNotNull(factory.findByKey(new Long(5)));
139 	}
140 
141 	/**
142 	 * Test method for
143 	 * {@link net.smartlab.web.BusinessObjectFactory#findByKey(java.io.Serializable, java.lang.String[])}
144 	 * .
145 	 */
146 	public final void testFindByKeySerializableStringArray() throws Exception {
147 		Sample sample = (Sample)factory.findByKey("1", new String[] {"items"});
148 		super.assertNotNull(sample);
149 		// TODO check the collection has been initialized
150 		
151 		super.assertNotNull(factory.findByKey("0", new String[] {}));
152 		super.assertNotNull(factory.findByKey(null, null));
153 		super.assertNotNull(factory.findByKey(new Long(5), new String[] {}));
154 		
155 
156 		super.assertNotNull(factory.findByKey("1", new String[] {"x"}));
157 		// TODO check the collection has NOT been initialized
158 		
159 
160 		super.assertNotNull(factory.findByKey("1", new String[] {"x", "items", null}));
161 		// TODO check the collection has been initialized
162 	}
163 
164 	/**
165 	 * Test method for
166 	 * {@link net.smartlab.web.BusinessObjectFactory#remove(java.lang.Object)}.
167 	 */
168 	public final void testRemove() throws Exception {
169 		Sample sample = new Sample();
170 		sample.setId(3);
171 		factory.remove(sample);
172 		BusinessObjectFactory.flush();
173 		
174 		ITable after = getConnection().createDataSet().getTable("PUBLIC.sample");
175         super.assertEquals(19, after.getRowCount());
176         
177         try {
178         	factory.remove(null);
179         	super.fail();
180         } catch (Exception e) {
181         	// expected
182         }
183 	}
184 
185 	/**
186 	 * Test method for
187 	 * {@link net.smartlab.web.BusinessObjectFactory#update(java.lang.Object)}.
188 	 */
189 	public final void testUpdate() throws Exception {
190 		// INSERT
191 		Sample insert = new Sample();
192 		insert.setCode("sample");
193 		factory.update(insert);
194 		BusinessObjectFactory.flush();
195 		
196 		ITable afterInsert = getConnection().createDataSet().getTable("PUBLIC.sample");
197         super.assertEquals(21, afterInsert.getRowCount());
198         
199         // UPDATE
200         super.assertEquals("code-10", afterInsert.getValue(10, "code"));
201         Sample update = (Sample)factory.findByKey("10");
202 		update.setCode("updated");
203 		factory.update(update);
204 		BusinessObjectFactory.flush();
205 		
206 		ITable afterUpdate = getConnection().createDataSet().getTable("PUBLIC.sample");
207         super.assertEquals(21, afterUpdate.getRowCount());
208 
209         super.assertEquals("updated", afterUpdate.getValue(10, "code"));
210         
211         //FIXME clean up is needed because dbunit is unable to perform
212         // delete against hsqldb
213 		factory.remove(insert);
214 		BusinessObjectFactory.flush();
215 	}
216 
217 	/**
218 	 * Test method for
219 	 * {@link net.smartlab.web.BusinessObjectFactory#list(net.smartlab.web.DataAccessObject.SearchInfo)}
220 	 * .
221 	 */
222 	public final void testListSearchInfo() throws Exception {
223 		Collection samples = null;
224 		SearchInfo info = null;
225 		
226 		samples = factory.list(info);
227 		super.assertEquals(20, samples.size());
228 		
229 		info = new SearchInfo();
230 		samples = factory.list(info);
231 		super.assertEquals(20, samples.size());
232 		
233 		info = new SearchInfo();
234 		info.addFilter("id", SearchInfo.EQUALS, "0");
235 		samples = factory.list(info);
236 		super.assertEquals(1, samples.size());
237 		
238 		info = new SearchInfo();
239 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
240 		samples = factory.list(info);
241 		super.assertEquals(3, samples.size());
242 		
243 		info = new SearchInfo();
244 		info.addFilter("enumeration", SearchInfo.EQUALS, "1");
245 		samples = factory.list(info);
246 		super.assertEquals(11, samples.size());
247 		
248 		info = new SearchInfo();
249 		info.addFilter("enumeration", SearchInfo.EQUALS, new Object[] {Enumeration.ONE});
250 		samples = factory.list(info);
251 		super.assertEquals(11, samples.size());
252 		
253 		try {
254 			info = new SearchInfo();
255 			info.addFilter("reference.enumeration", SearchInfo.EQUALS, "1");
256 			samples = factory.list(info);
257 			super.fail();
258 		} catch (Exception e) {
259 			// Expected
260 		}
261 		
262 		try {
263 			info = new SearchInfo();
264 			info.addFilter("items.size", SearchInfo.EQUALS, "3");
265 			samples = factory.list(info);
266 			super.fail();
267 		} catch (Exception e) {
268 			// Expected
269 		}
270 		
271 		try {
272 			info = new SearchInfo();
273 			info.addFilter("items", SearchInfo.EQUALS, new Object[] {new ArrayList()});
274 			samples = factory.list(info);
275 			super.fail();
276 		} catch (Exception e) {
277 			// Expected
278 		}
279 	}
280 
281 	/**
282 	 * Test method for
283 	 * {@link net.smartlab.web.BusinessObjectFactory#list(net.smartlab.web.DataAccessObject.SearchInfo, java.lang.String[])}
284 	 * .
285 	 */
286 	public final void testListSearchInfoStringArray() throws Exception{
287 		Collection samples = null;
288 		SearchInfo info = null;
289 		
290 		samples = factory.list(info, new String[] {"items"});
291 		super.assertEquals(20, samples.size());
292 		// TODO check the collection has been initialized
293 		
294 		info = new SearchInfo();
295 		samples = factory.list(info, new String[] {"items"});
296 		super.assertEquals(20, samples.size());
297 		// TODO check the collection has been initialized
298 		
299 		info = new SearchInfo();
300 		info.addFilter("id", SearchInfo.EQUALS, "0");
301 		samples = factory.list(info, new String[] {"items"});
302 		super.assertEquals(1, samples.size());
303 		// TODO check the collection has been initialized
304 		
305 		info = new SearchInfo();
306 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
307 		samples = factory.list(info, new String[] {"items"});
308 		super.assertEquals(3, samples.size());
309 		// TODO check the collection has been initialized
310 		
311 		info = new SearchInfo();
312 		info.addFilter("enumeration", SearchInfo.EQUALS, "1");
313 		samples = factory.list(info, new String[] {"items"});
314 		super.assertEquals(11, samples.size());
315 		// TODO check the collection has been initialized
316 		
317 		info = new SearchInfo();
318 		info.addFilter("enumeration", SearchInfo.EQUALS, new Object[] {Enumeration.ONE});
319 		samples = factory.list(info, new String[] {"items"});
320 		super.assertEquals(11, samples.size());
321 		// TODO check the collection has been initialized
322 		
323 		try {
324 			info = new SearchInfo();
325 			info.addFilter("reference.enumeration", SearchInfo.EQUALS, "1");
326 			samples = factory.list(info, new String[] {"items"});
327 			super.fail();
328 		} catch (Exception e) {
329 			// Expected
330 		}
331 		
332 		try {
333 			info = new SearchInfo();
334 			info.addFilter("items.size", SearchInfo.EQUALS, "3");
335 			samples = factory.list(info, new String[] {"items"});
336 			super.fail();
337 		} catch (Exception e) {
338 			// Expected
339 		}
340 		
341 		try {
342 			info = new SearchInfo();
343 			info.addFilter("items", SearchInfo.EQUALS, new Object[] {new ArrayList()});
344 			samples = factory.list(info, new String[] {"items"});
345 			super.fail();
346 		} catch (Exception e) {
347 			// Expected
348 		}
349 	}
350 
351 	/**
352 	 * Test method for
353 	 * {@link net.smartlab.web.BusinessObjectFactory#listByKeySet(java.util.Set, net.smartlab.web.DataAccessObject.SearchInfo)}
354 	 * .
355 	 */
356 	public final void testListByKeySetSetSearchInfo() throws Exception {
357 		Set in = new HashSet();
358 		in.add("1");
359 		in.add("2");
360 		in.add("7");
361 		
362 		Collection samples = null;
363 		SearchInfo info = null;
364 		
365 		samples = factory.listByKeySet(in, info);
366 		super.assertEquals(3, samples.size());
367 		
368 		info = new SearchInfo();
369 		samples = factory.listByKeySet(in, info);
370 		super.assertEquals(3, samples.size());
371 		
372 		info = new SearchInfo();
373 		info.addFilter("id", SearchInfo.EQUALS, "0");
374 		samples = factory.listByKeySet(in, info);
375 		super.assertEquals(0, samples.size());
376 		
377 		info = new SearchInfo();
378 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
379 		samples = factory.listByKeySet(in, info);
380 		super.assertEquals(1, samples.size());
381 	
382 		info = new SearchInfo();
383 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
384 		samples = factory.listByKeySet(null, info);
385 		super.assertEquals(3, samples.size());
386 	}
387 
388 	/**
389 	 * Test method for
390 	 * {@link net.smartlab.web.BusinessObjectFactory#listByKeySet(java.util.Set, net.smartlab.web.DataAccessObject.SearchInfo, boolean)}
391 	 * .
392 	 */
393 	public final void testListByKeySetSetSearchInfoBoolean() throws Exception {
394 		Set in = new HashSet();
395 		in.add("1");
396 		in.add("2");
397 		in.add("7");
398 		
399 		Collection samples = null;
400 		SearchInfo info = null;
401 
402 		samples = factory.listByKeySet(in, info, false);
403 		super.assertEquals(3, samples.size());
404 		
405 		samples = factory.listByKeySet(in, info, true);
406 		super.assertEquals(17, samples.size());
407 
408 		info = new SearchInfo();
409 		info.addFilter("id", SearchInfo.LESSER_EQUALS, "10");
410 		samples = factory.listByKeySet(in, info, false);
411 		super.assertEquals(3, samples.size());
412 		
413 		info = new SearchInfo();
414 		info.addFilter("id", SearchInfo.LESSER_EQUALS, "10");
415 		samples = factory.listByKeySet(in, info, true);
416 		super.assertEquals(8, samples.size());
417 
418 		info = new SearchInfo();
419 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
420 		samples = factory.listByKeySet(in, info, false);
421 		super.assertEquals(1, samples.size());
422 		
423 		info = new SearchInfo();
424 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
425 		samples = factory.listByKeySet(in, info, true);
426 		super.assertEquals(2, samples.size());
427 		
428 		info = new SearchInfo();
429 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
430 		samples = factory.listByKeySet(null, info);
431 		super.assertEquals(3, samples.size());
432 	}
433 
434 	/**
435 	 * Test method for
436 	 * {@link net.smartlab.web.BusinessObjectFactory#page(net.smartlab.web.DataAccessObject.SearchInfo)}
437 	 * .
438 	 */
439 	public final void testPageSearchInfo() throws Exception {
440 		Paginator samples = null;
441 		SearchInfo info = null;
442 		
443 		samples = (Paginator)factory.page(info);
444 		samples.setPageSize(10);
445 		super.assertEquals(10, samples.toArray().length);
446 		
447 		info = new SearchInfo();
448 		samples = (Paginator)factory.page(info);
449 		super.assertEquals(20, samples.toArray().length);
450 		
451 		info = new SearchInfo();
452 		info.addFilter("id", SearchInfo.EQUALS, "0");
453 		samples = (Paginator)factory.page(info);
454 		super.assertEquals(1, samples.toArray().length);
455 		
456 		info = new SearchInfo();
457 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
458 		samples = (Paginator)factory.page(info);
459 		super.assertEquals(3, samples.toArray().length);
460 	}
461 
462 	/**
463 	 * Test method for
464 	 * {@link net.smartlab.web.BusinessObjectFactory#page(net.smartlab.web.DataAccessObject.SearchInfo, java.lang.String[])}
465 	 * .
466 	 */
467 	public final void testPageSearchInfoStringArray() throws Exception {
468 		Paginator samples = null;
469 		SearchInfo info = null;
470 		
471 		samples = (Paginator)factory.page(info, new String[] {"items"});
472 		super.assertEquals(20, samples.toArray().length);
473 		// TODO check the collection has been initialized
474 		
475 		info = new SearchInfo();
476 		samples = (Paginator)factory.page(info, new String[] {"items"});
477 		super.assertEquals(20, samples.toArray().length);
478 		// TODO check the collection has been initialized
479 		
480 		info = new SearchInfo();
481 		info.addFilter("id", SearchInfo.EQUALS, "0");
482 		samples = (Paginator)factory.page(info, new String[] {"items"});
483 		super.assertEquals(1, samples.toArray().length);
484 		// TODO check the collection has been initialized
485 		
486 		info = new SearchInfo();
487 		info.addFilter("reference.id", SearchInfo.EQUALS, "0");
488 		samples = (Paginator)factory.page(info, new String[] {"items"});
489 		super.assertEquals(3, samples.toArray().length);
490 	}
491 
492 	/**
493 	 * Test method for
494 	 * {@link net.smartlab.web.BusinessObjectFactory#convertKey(java.io.Serializable)}
495 	 * .
496 	 */
497 	public final void testConvertKey() {
498 		super.assertEquals(new Long(1), factory.convertKey("1"));
499 		super.assertEquals(null, factory.convertKey("0"));
500 		super.assertEquals(null, factory.convertKey(null));
501 	}
502 
503 	/**
504 	 * Test method for
505 	 * {@link net.smartlab.web.BusinessObjectFactory#convertKeys(java.util.Collection)}
506 	 * .
507 	 */
508 	public final void testConvertKeys() {
509 		super.assertEquals(1, 
510 				factory.convertKeys(Arrays.asList(new String[] {"1"})).size());
511 		super.assertEquals(3, factory.convertKeys(Arrays.asList(
512 				new String[] {"1", null, "2"})).size());
513 		super.assertEquals(0, factory.convertKeys(null).size());
514 	}
515 
516 	/**
517 	 * Test method for
518 	 * {@link net.smartlab.web.BusinessObjectFactory#createCriteria(net.smartlab.web.DataAccessObject.SearchInfo)}
519 	 * .
520 	 */
521 	public final void testCreateCriteria() throws Exception {
522 		factory.createCriteria(null);
523 	}
524 
525 	/**
526 	 * Test method for {@link net.smartlab.web.BusinessObjectFactory#begin()}.
527 	 */
528 	public final void testBegin() {
529 		fail("Not yet implemented");
530 	}
531 
532 	/**
533 	 * Test method for {@link net.smartlab.web.BusinessObjectFactory#commit()}.
534 	 */
535 	public final void testCommit() {
536 		fail("Not yet implemented");
537 	}
538 
539 	/**
540 	 * Test method for {@link net.smartlab.web.BusinessObjectFactory#rollback()}
541 	 * .
542 	 */
543 	public final void testRollback() {
544 		fail("Not yet implemented");
545 	}
546 
547 	protected IDataSet getDataSet() throws Exception {
548 		return new FlatXmlDataSet(new FileReader(this.getDataSetFile()), false, true, false);
549 	}
550 
551 	/**
552 	 * @see org.dbunit.DatabaseTestCase#getSetUpOperation()
553 	 */
554 	protected DatabaseOperation getSetUpOperation() throws Exception {
555 		return DatabaseOperation.CLEAN_INSERT;
556 	}
557 
558 	/**
559 	 * @see org.dbunit.DatabaseTestCase#getTearDownOperation()
560 	 */
561 	protected DatabaseOperation getTearDownOperation() throws Exception {
562 		return DatabaseOperation.DELETE;
563 	}
564 
565 	/**
566 	 * @see net.smartlab.web.test.BusinessObjectFactoryTestCase#getDataSetFile()
567 	 */
568 	protected String getDataSetFile() {
569 		return "res/dataset.xml";
570 	}
571 
572 
573 	/**
574 	 * @hibernate.class table="sample"
575 	 */
576 	public static class Sample extends BusinessObject {
577 
578 		private static final long serialVersionUID = 1L;
579 
580 		private long id;
581 
582 		private String code;
583 
584 		private Collection items;
585 
586 		private Sample ref;
587 
588 		private Enumeration enum;
589 
590 
591 		public void setId(long id) {
592 			this.id = id;
593 		}
594 
595 		/**
596 		 * @hibernate.id generator-class="native"
597 		 */
598 		public long getId() {
599 			return id;
600 		}
601 
602 		public void setCode(String code) {
603 			this.code = code;
604 		}
605 
606 		/**
607 		 * @hibernate.property
608 		 */
609 		public String getCode() {
610 			return code;
611 		}
612 
613 		public void setItems(Collection items) {
614 			this.items = items;
615 		}
616 
617 		/**
618 		 * @hibernate.set
619 		 * @hibernate.collection-one-to-many lazy="true"
620 		 *                                   class="net.smartlab.web.BusinessObjectFactoryTest$Sample"
621 		 * @hibernate.collection-key column="item" inverse="true"
622 		 */
623 		public Collection getItems() {
624 			return items;
625 		}
626 
627 		public void setReference(Sample ref) {
628 			this.ref = ref;
629 		}
630 
631 		/**
632 		 * @hibernate.many-to-one column="item" not-null="false"
633 		 *                        class="net.smartlab.web.BusinessObjectFactoryTest$Sample"
634 		 */
635 		public Sample getReference() {
636 			return ref;
637 		}
638 
639 		public void setEnumeration(Enumeration enum) {
640 			this.enum = enum;
641 		}
642 
643 		/**
644 		 * @hibernate.property column="enum"
645 		 *                     type="net.smartlab.web.BusinessObjectFactoryTest$Enumeration"
646 		 */
647 		public Enumeration getEnumeration() {
648 			return enum;
649 		}
650 	}
651 
652 	public static class Enumeration extends net.smartlab.web.Enumeration {
653 
654 		public final static Enumeration ONE = new Enumeration(1, "one");
655 
656 		public final static Enumeration TWO = new Enumeration(2, "two");
657 
658 		public final static Enumeration THREE = new Enumeration(3, "three");
659 
660 
661 		public Enumeration() {
662 		}
663 
664 		private Enumeration(int id, String display) {
665 			super(id, display);
666 		}
667 
668 		public net.smartlab.web.Enumeration decode(int id) {
669 			switch (id) {
670 				case 1:
671 					return ONE;
672 				case 2:
673 					return TWO;
674 				case 3:
675 					return THREE;
676 				default:
677 					return null;
678 			}
679 		}
680 	}
681 
682 	public static class SampleFactory extends BusinessObjectFactory {
683 
684 		public Class getMappedClass() {
685 			return Sample.class;
686 		}
687 
688 		public static SampleFactory getInstance() {
689 			return new SampleFactory();
690 		}
691 	}
692 }