View Javadoc

1   /*
2    * #%L
3    * ToPIA :: Persistence
4    * 
5    * $Id: TopiaContextReplicateTest.java 2416 2012-03-02 10:08:59Z tchemit $
6    * $HeadURL: http://svn.nuiton.org/svn/topia/tags/topia-2.8-rc-1/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaContextReplicateTest.java $
7    * %%
8    * Copyright (C) 2004 - 2012 CodeLutin
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
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   * To test replication sugin TopiaContext.
44   *
45   * @author tchemit <chemit@codelutin.com>
46   * @since 2.6.8
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  //        Properties configSource = TestHelper.initTopiaContextConfiguration(
78  //                testBasedir,
79  //                "/TopiaContextImpl.properties",
80  //                "replicateSource");
81  //
82  //        Properties configTarget = TestHelper.initTopiaContextConfiguration(
83  //                testBasedir,
84  //                "/TopiaContextImpl.properties",
85  //                "replicateTarget");
86  //
87  
88          TopiaContext contextSource = dbSource.getRootCtxt();
89          TopiaContext contextTarget = dbTarget.getRootCtxt();
90  
91  //        try {
92  //            contextSource = TopiaContextFactory.getContext(configSource);
93  //            contextTarget = TopiaContextFactory.getContext(configTarget);
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 //        } finally {
152 //            closeDb(contextSource);
153 //            closeDb(contextTarget);
154 //        }
155 
156     }
157 }