01: package org.compass.annotations.test.reference.poly;
02:
03: import java.util.HashSet;
04: import java.util.Set;
05:
06: import org.compass.annotations.test.AbstractAnnotationsTestCase;
07: import org.compass.core.CompassSession;
08: import org.compass.core.CompassTransaction;
09: import org.compass.core.config.CompassConfiguration;
10:
11: /**
12: * Composite tests
13: */
14: public class CompositeTests extends AbstractAnnotationsTestCase {
15:
16: protected void addExtraConf(CompassConfiguration conf) {
17: conf.addClass(Category.class).addClass(CategoryGroup.class);
18: }
19:
20: public void testCompositeRelationships() {
21: // Build model
22: CategoryGroup sports = new CategoryGroup(10l, "Sports");
23: Set<Category> categories = new HashSet<Category>();
24: categories.add(new Category(100l, "Fishing"));
25: categories.add(new Category(101l, "Golf"));
26: categories.add(new Category(102l, "Extreme Ironing"));
27: sports.setCategories(categories);
28:
29: assertSportsCategoryGroup(sports);
30:
31: // Index
32: CompassSession compassSession = openSession();
33: CompassTransaction compassTransaction = compassSession
34: .beginTransaction();
35:
36: compassSession.save(sports);
37: for (Category category : sports.getCategories()) {
38: compassSession.save(category);
39: }
40:
41: compassTransaction.commit();
42: compassSession.close();
43:
44: // Search
45: compassSession = openSession();
46: compassTransaction = compassSession.beginTransaction();
47:
48: Category category = (Category) compassSession.get(
49: Category.class, 100l);
50: assertNotNull(category);
51: assertEquals("Fishing", category.getName());
52: assertEquals("Golf", ((Category) compassSession.get(
53: Category.class, 101l)).getName());
54: assertEquals("Extreme Ironing", ((Category) compassSession.get(
55: Category.class, 102l)).getName());
56:
57: // Load composite and check composed instances
58: CategoryGroup sportsLoaded = (CategoryGroup) compassSession
59: .get(CategoryGroup.class, 10l);
60: assertSportsCategoryGroup(sportsLoaded);
61:
62: compassTransaction.commit();
63: compassSession.close();
64:
65: }
66:
67: private void assertSportsCategoryGroup(CategoryGroup sports) {
68: assertNotNull(sports);
69: assertEquals("Sports", sports.getName());
70: assertNotNull(sports.getCategories());
71: assertEquals(3, sports.getCategories().size());
72: }
73: }
|