1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.nuiton.topia.framework;
26
27 import org.junit.Assert;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.nuiton.topia.TopiaContext;
31 import org.nuiton.topia.TopiaContextFactory;
32 import org.nuiton.topia.TopiaDatabase;
33 import org.nuiton.topia.TopiaTestDAOHelper;
34 import org.nuiton.topia.test.entities.Person;
35 import org.nuiton.topia.test.entities.PersonDAO;
36 import org.nuiton.topia.test.entities.Pet;
37 import org.nuiton.topia.test.entities.PetDAO;
38
39 import java.io.File;
40 import java.util.Properties;
41
42
43
44
45
46
47
48 public class TopiaContextReplicateTest {
49
50 @Rule
51 public final TopiaDatabase dbSource =
52 new TopiaDatabase() {
53
54 @Override
55 protected void onDbConfigurationCreate(Properties configuration, File testDir, String dbPath) {
56 configuration.setProperty(
57 TopiaContextFactory.CONFIG_URL, "jdbc:h2:file:" + dbPath + "-source");
58
59 }
60 };
61
62 @Rule
63 public final TopiaDatabase dbTarget =
64 new TopiaDatabase() {
65
66 @Override
67 protected void onDbConfigurationCreate(Properties configuration, File testDir, String dbPath) {
68 configuration.setProperty(
69 TopiaContextFactory.CONFIG_URL, "jdbc:h2:file:" + dbPath + "-target");
70
71 }
72 };
73
74 @Test
75 public void replicateEntity() throws Exception {
76
77
78
79
80
81
82
83
84
85
86
87
88 TopiaContext contextSource = dbSource.getRootCtxt();
89 TopiaContext contextTarget = dbTarget.getRootCtxt();
90
91
92
93
94
95 TopiaContext txSource;
96 TopiaContext txTarget;
97 PersonDAO daoSource, daoTarget;
98 PetDAO petDAOSource, petDAOTarget;
99 Person personSource, personTarget;
100 Pet petSource, petTarget;
101
102 txSource = contextSource.beginTransaction();
103 daoSource = TopiaTestDAOHelper.getPersonDAO(txSource);
104 petDAOSource = TopiaTestDAOHelper.getPetDAO(txSource);
105
106 personSource = daoSource.create(Person.PROPERTY_FIRSTNAME, " firstName",
107 Person.PROPERTY_NAME, " name"
108 );
109
110 petSource = petDAOSource.create(Pet.PROPERTY_NAME, "name",
111 Pet.PROPERTY_TYPE, "type",
112 Pet.PROPERTY_PERSON, personSource
113 );
114
115 personSource.addPet(petSource);
116
117 txSource.commitTransaction();
118
119 daoSource = TopiaTestDAOHelper.getPersonDAO(txSource);
120
121 personSource = daoSource.findByTopiaId(personSource.getTopiaId());
122 Assert.assertNotNull(personSource);
123
124 petSource = petDAOSource.findByTopiaId(petSource.getTopiaId());
125 Assert.assertNotNull(petSource);
126 Assert.assertEquals(1, personSource.sizePet());
127 Assert.assertEquals(petSource, personSource.getPet().iterator().next());
128
129 txTarget = contextTarget.beginTransaction();
130
131 txSource.replicateEntity(txTarget, petSource);
132 txSource.replicateEntity(txTarget, personSource);
133
134 txTarget.commitTransaction();
135
136 daoTarget = TopiaTestDAOHelper.getPersonDAO(txTarget);
137 petDAOTarget = TopiaTestDAOHelper.getPetDAO(txTarget);
138
139 personTarget = daoTarget.findByTopiaId(personSource.getTopiaId());
140 Assert.assertNotNull(personTarget);
141 Assert.assertEquals(personSource, personTarget);
142 Assert.assertEquals(1, personTarget.sizePet());
143
144 petTarget = petDAOTarget.findByTopiaId(petSource.getTopiaId());
145 Assert.assertNotNull(petTarget);
146 Assert.assertEquals(petSource, petTarget);
147
148 Assert.assertEquals(petTarget, personTarget.getPet().iterator().next());
149
150
151
152
153
154
155
156 }
157 }