1 package test.integration;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import java.util.Collection;
8
9 import org.junit.Before;
10 import org.junit.Test;
11
12 import de.fhdw.wtf.context.core.PersistenceContext;
13 import de.fhdw.wtf.context.core.TransactionManager;
14 import de.fhdw.wtf.persistence.exception.PersistenceException;
15 import de.fhdw.wtf.persistence.exception.TypeOrAssociationNotFoundException;
16 import de.fhdw.wtf.persistence.meta.Association;
17 import de.fhdw.wtf.persistence.meta.MapAssociation;
18 import de.fhdw.wtf.persistence.meta.MapLink;
19 import de.fhdw.wtf.persistence.meta.Object;
20 import de.fhdw.wtf.persistence.meta.UnidirectionalAssociation;
21 import de.fhdw.wtf.persistence.meta.UserObject;
22 import de.fhdw.wtf.persistence.meta.UserType;
23 import de.fhdw.wtf.persistence.utils.Tuple;
24
25 public class TestAssociation3WithDB extends IntegrationTestSuperClass {
26
27 @Override
28 protected TestBase genTestBase() {
29 return new TestBaseWithDB();
30 }
31
32 @Override
33 protected TestBaseWithDB getTestBase() {
34 return (TestBaseWithDB) super.getTestBase();
35 }
36
37 @Override
38 @Before
39 public void setUp() throws Exception {
40
41 super.setUp();
42
43 initializeTypesAndSpecializationAndAssociations();
44
45 TransactionManager.setContext(new PersistenceContext(this.getTestBase()
46 .getObjectFacade(), this.getTestBase().getObjectFacade()
47 .provideAdhocTransaction()));
48 this.someUserObject = TransactionManager.getContext().create(
49 "SomeUserType");
50 this.anotherUserObject = TransactionManager.getContext().create(
51 "AnotherUserType");
52
53 }
54
55 private UserType anyType;
56 private UserType someUserType;
57 private UserType anotherUserType;
58
59 private UserType persistentMap;
60
61 private MapAssociation mapLinks;
62
63 private UserObject someUserObject;
64 private UserObject anotherUserObject;
65
66 protected void initializeTypesAndSpecializationAndAssociations()
67 throws PersistenceException {
68
69 this.anyType = this.getTestBase().getClassFacade()
70 .createUserType("AnyType", false, false);
71 this.someUserType = this.getTestBase().getClassFacade()
72 .createUserType("SomeUserType", false, false);
73 this.anotherUserType = this.getTestBase().getClassFacade()
74 .createUserType("AnotherUserType", false, false);
75
76
77 this.persistentMap = this
78 .getTestBase()
79 .getClassFacade()
80 .createUserType(
81 "de.fhdw.wtf.context.model.collections.PersistentMap",
82 false, false);
83 this.mapLinks = this
84 .getTestBase()
85 .getClassFacade()
86 .createMapAssociation("links", false, this.persistentMap,
87 this.anyType, this.anyType);
88
89 this.getTestBase().getClassFacade()
90 .createSpecializationBetween(this.anyType, this.someUserType);
91 this.getTestBase()
92 .getClassFacade()
93 .createSpecializationBetween(this.anyType, this.anotherUserType);
94 this.getTestBase().getClassFacade().finalizeSpecialization();
95
96
97 this.persistentMap = this
98 .getTestBase()
99 .getClassFacade()
100 .createUserType(
101 "de.fhdw.wtf.context.model.collections.PersistentList",
102 false, false);
103
104 this.getTestBase()
105 .getClassFacade()
106 .createUserType("generated.model.de.fhdw.partner.Haus", false,
107 false);
108 this.getTestBase()
109 .getClassFacade()
110 .createUserType(
111 "generated.model.de.fhdw.partner.NatuerlichePerson",
112 false, false);
113 this.getTestBase()
114 .getClassFacade()
115 .createUserType("generated.model.de.fhdw.partner.Postfach",
116 false, false);
117 this.getTestBase()
118 .getClassFacade()
119 .createUserType("generated.model.de.fhdw.partner.Telefon",
120 false, false);
121 this.getTestBase()
122 .getClassFacade()
123 .createUserType("generated.model.de.fhdw.partner.Telefonbuch",
124 false, false);
125 }
126
127 @Test
128 public void testAssociation3Type()
129 throws TypeOrAssociationNotFoundException {
130 final Association association = this
131 .getTestBase()
132 .getObjectFacade()
133 .getTypeManager()
134 .getMapAssociationForName(
135 "de.fhdw.wtf.context.model.collections.PersistentMap.links");
136 if (association instanceof MapAssociation) {
137 return;
138 } else if (association instanceof UnidirectionalAssociation) {
139 System.out
140 .println("association is of type UnidirectionalAssociation (and not MapAssociation)");
141 fail();
142 }
143 fail();
144 }
145
146 @Test
147 public void testCreateLink3() throws PersistenceException {
148 final UserObject owner = TransactionManager.getContext().create(
149 "de.fhdw.wtf.context.model.collections.PersistentMap");
150
151 final MapLink mapLink = TransactionManager.getContext().put(owner,
152 "de.fhdw.wtf.context.model.collections.PersistentMap.links",
153 this.someUserObject, this.anotherUserObject);
154
155 final Collection<Tuple<MapLink, Object>> dbValue = TransactionManager
156 .getContext()
157 .get(owner,
158 "de.fhdw.wtf.context.model.collections.PersistentMap.links",
159 this.anotherUserObject);
160
161 assertEquals(dbValue.size(), 1);
162 assertTrue(this.someUserObject.isTheSameAs(dbValue.iterator().next()
163 .getSecond()));
164 assertTrue(this.anotherUserObject.isTheSameAs(dbValue.iterator().next()
165 .getFirst().getKey()));
166 assertTrue(owner.isTheSameAs(dbValue.iterator().next().getFirst()
167 .getOwner()));
168 }
169
170 }