gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / test / java / org / gvsig / vcsgis / lib / impl / Test13Merge.java @ 3391
History | View | Annotate | Download (10.2 KB)
1 |
package org.gvsig.vcsgis.lib.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.List; |
5 |
import java.util.Map; |
6 |
import junit.framework.TestCase; |
7 |
import static junit.framework.TestCase.assertEquals; |
8 |
import org.apache.commons.io.FileUtils; |
9 |
import org.gvsig.expressionevaluator.Expression; |
10 |
import org.gvsig.fmap.dal.exception.DataException; |
11 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
12 |
import org.gvsig.fmap.dal.feature.Feature; |
13 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
14 |
import static org.gvsig.fmap.dal.feature.FeatureStore.MODE_PASS_THROUGH; |
15 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
16 |
import org.gvsig.tools.dispose.DisposeUtils; |
17 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
18 |
import org.gvsig.vcsgis.lib.SequentialCodeGenerator; |
19 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
20 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
21 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR; |
22 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK; |
23 |
import static org.gvsig.vcsgis.lib.VCSGisManager.FEATURECODE_FIELD_NAME; |
24 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE; |
25 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_INSERT; |
26 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_UPDATE; |
27 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
28 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
29 |
import org.gvsig.vcsgis.lib.workspace.tables.RemoteChangesTable; |
30 |
import org.gvsig.vcsgis.lib.workspace.tables.WorkspaceChangesTable; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
public class Test13Merge extends TestCase { |
35 |
|
36 |
private static final Logger LOGGER = LoggerFactory.getLogger(Test13Merge.class); |
37 |
|
38 |
public Test13Merge(String testName) { |
39 |
super(testName);
|
40 |
} |
41 |
|
42 |
@Override
|
43 |
protected void setUp() throws Exception { |
44 |
super.setUp();
|
45 |
new DefaultLibrariesInitializer().fullInitialize();
|
46 |
} |
47 |
|
48 |
@Override
|
49 |
protected void tearDown() throws Exception { |
50 |
super.tearDown();
|
51 |
} |
52 |
|
53 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
54 |
// public void testHello() {}
|
55 |
|
56 |
private void insert(FeatureStore store, int id, String text) throws DataException{ |
57 |
EditableFeature ef = store.createNewFeature(); |
58 |
ef.set("id",id);
|
59 |
ef.set("text",text);
|
60 |
store.insert(ef); |
61 |
} |
62 |
|
63 |
private void update(FeatureStore store, int id, String text) throws DataException{ |
64 |
Feature f = store.findFirst("id="+id);
|
65 |
EditableFeature ef = f.getEditable(); |
66 |
ef.set("text", text);
|
67 |
store.update(ef); |
68 |
} |
69 |
|
70 |
private void delete(FeatureStore store, int id, String text) throws DataException{ |
71 |
Feature f = store.findFirst("id="+id);
|
72 |
store.delete(f); |
73 |
} |
74 |
|
75 |
private void check(List<Feature> features, int index, int id, String text){ |
76 |
assertEquals(id , features.get(index).getInt("id"));
|
77 |
assertEquals(text, features.get(index).getString("text"));
|
78 |
} |
79 |
|
80 |
private void check(List<Feature> features, Map<Integer,String> testValues){ |
81 |
features.forEach(feature -> { |
82 |
assertEquals(testValues.get(feature.getInt("id")), feature.getString("text")); |
83 |
}); |
84 |
} |
85 |
|
86 |
private void selectRemoteChange(VCSGisWorkspace ws, boolean select, int id, String text, VCSGisWorkspace wsSource) throws Exception { |
87 |
Feature fTest; |
88 |
FeatureStore testStore = ws.getFeatureStore("test");
|
89 |
FeatureStore testStoreSource = wsSource.getFeatureStore("test");
|
90 |
|
91 |
fTest = testStore.findFirst("id = "+id);
|
92 |
if(fTest == null){ |
93 |
fTest = testStoreSource.findFirst("id = "+id);
|
94 |
} |
95 |
FeatureStore rchangesStore = ws.getFeatureStore(RemoteChangesTable.TABLE_NAME); |
96 |
Feature fChanges = rchangesStore.findFirst("RCH_DATACODE = '"+fTest.getString(FEATURECODE_FIELD_NAME)+"'"); |
97 |
EditableFeature efChanges = fChanges.getEditable(); |
98 |
rchangesStore.edit(MODE_PASS_THROUGH); |
99 |
efChanges.set("RCH_SELECTED",select);
|
100 |
rchangesStore.update(efChanges); |
101 |
rchangesStore.finishEditing(); |
102 |
DisposeUtils.dispose(testStore); |
103 |
DisposeUtils.dispose(testStoreSource); |
104 |
DisposeUtils.dispose(rchangesStore); |
105 |
} |
106 |
|
107 |
private void checkLocalChange(FeatureStore data, FeatureStore changes, String code, int operation, int id, String text) throws DataException{ |
108 |
Feature change = changes.findFirst("WSCH_FEATURECODE = '"+code+"'"); |
109 |
assertNotNull("Change code not exists", change);
|
110 |
assertEquals(operation, change.getInt(WorkspaceChangesTable.OPERATION)); |
111 |
// Uf, ojo con operation delete, no tiene data. en la primera pasada lo saltaria, y a ver que pasa con
|
112 |
// los inserts y updates.
|
113 |
if(operation != OP_DELETE){
|
114 |
Feature f = data.findFirst("VCSGISCODE = '"+code+"'"); |
115 |
assertEquals(id , f.getInt("id"));
|
116 |
assertEquals(text, f.getString("text"));
|
117 |
} |
118 |
|
119 |
} |
120 |
|
121 |
public void testMerge() throws Exception { |
122 |
JDBCServerExplorer server = TestUtils.openServerExplorer("srv-merge");
|
123 |
File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws1-merge")); |
124 |
File ws2file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws2-merge")); |
125 |
|
126 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
127 |
manager.setCodeGenerator(new SequentialCodeGenerator());
|
128 |
|
129 |
int r;
|
130 |
Feature f; |
131 |
EditableFeature ef; |
132 |
List<Feature> features;
|
133 |
FeatureStore store1; |
134 |
FeatureStore store2; |
135 |
|
136 |
// ------------------------------------------------------------
|
137 |
// Inicializamos el repositorio y lo abrimos
|
138 |
r = manager.initRepository(server.getParameters(), null);
|
139 |
assertEquals("init_server status", ERR_NO_ERROR, r);
|
140 |
|
141 |
VCSGisRepository repo = manager.openRepository(server.getParameters()); |
142 |
TestUtils.h2sql_repository(repo); |
143 |
|
144 |
// ------------------------------------------------------------
|
145 |
// Creamos workspace1 y lo abrimos.
|
146 |
r = manager.initWorkspace(ws1file, repo, "Test update1", null); |
147 |
assertEquals("init_ws1 status", ERR_NO_ERROR, r);
|
148 |
|
149 |
VCSGisWorkspace ws1 = manager.openWorkspace(ws1file); |
150 |
TestUtils.h2sql_workspace(ws1); |
151 |
|
152 |
// ------------------------------------------------------------
|
153 |
// Adicionamos al workspace1 la tabla "test" y la commitamos.
|
154 |
//REV 0
|
155 |
FeatureStore sourceTest = TestUtils.openSourceStore2(); |
156 |
r = ws1.add("test", sourceTest, "text"); |
157 |
DisposeUtils.disposeQuietly(sourceTest); |
158 |
assertEquals("ws1.add status", ERR_NO_ERROR, r);
|
159 |
|
160 |
store1 = ws1.getFeatureStore("test");
|
161 |
store1.edit(); |
162 |
insert(store1, 4, "DDD"); |
163 |
insert(store1, 5, "EEE"); |
164 |
|
165 |
store1.finishEditing(); |
166 |
DisposeUtils.disposeQuietly(store1); |
167 |
|
168 |
r = ws1.commit(); |
169 |
assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
|
170 |
|
171 |
// ------------------------------------------------------------
|
172 |
// Inicializamos el workspace2 asociandolo al mismo repositorio que
|
173 |
// workspace1 y lo abrimos.
|
174 |
r = manager.initWorkspace(ws2file, repo, "Test update2",null); |
175 |
assertEquals("init_ws2 status", ERR_NO_ERROR, r);
|
176 |
VCSGisWorkspace ws2 = manager.openWorkspace(ws2file); |
177 |
TestUtils.h2sql_workspace(ws2); |
178 |
|
179 |
ws2.checkout("test");
|
180 |
|
181 |
//REV 1
|
182 |
store1 = ws1.getFeatureStore("test");
|
183 |
store1.edit(); |
184 |
update(store1, 1, "AA2"); |
185 |
update(store1, 2, "BB2"); |
186 |
insert(store1, 6, "FFF"); |
187 |
insert(store1, 7, "GGG"); |
188 |
delete(store1, 3, "CCC"); |
189 |
delete(store1, 5, "EEE"); |
190 |
|
191 |
store1.finishEditing(); |
192 |
DisposeUtils.disposeQuietly(store1); |
193 |
|
194 |
r = ws1.commit(); |
195 |
assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
|
196 |
|
197 |
|
198 |
store2 = ws2.getFeatureStore("test");
|
199 |
|
200 |
// ------------------------------------------------------------
|
201 |
// Actualizamos (update) la tabla "test" en el workspace2
|
202 |
// y comprobamos que tiene lo que toca.
|
203 |
r = ws2.updatePrepare("test");
|
204 |
assertEquals("ws2.update-1 status", 0, r); |
205 |
store2.edit(); |
206 |
insert(store2, 8, "HHH"); |
207 |
store2.finishEditing(); |
208 |
DisposeUtils.disposeQuietly(store2); |
209 |
// selectRemoteChange(ws2, true, 1, "AAA", ws1);
|
210 |
// selectRemoteChange(ws2, true, 6, "FFF", ws1);
|
211 |
selectRemoteChange(ws2, true, 3, "CCC", ws1); |
212 |
|
213 |
selectRemoteChange(ws2, false, 2, "BB2", ws1); |
214 |
selectRemoteChange(ws2, false, 5, "EEE", ws1); |
215 |
selectRemoteChange(ws2, false, 7, "GGG", ws1); |
216 |
|
217 |
r = ws2.merge("test");
|
218 |
assertEquals("ws2.update-merge-1 status", ERR_OK, r);
|
219 |
|
220 |
assertEquals("ws2-update sizes", 6, store2.size64()); |
221 |
features = store2.getFeatures((Expression)null, "id", true); |
222 |
|
223 |
check(features, 0, 1, "AA2"); |
224 |
check(features, 1, 2, "BBB"); |
225 |
check(features, 2, 4, "DDD"); |
226 |
check(features, 3, 5, "EEE"); |
227 |
check(features, 4, 6, "FFF"); |
228 |
check(features, 5, 8, "HHH"); |
229 |
|
230 |
FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME); |
231 |
checkLocalChange(store2, changes, "00000000000066abcdef0123456789", OP_INSERT, 8, "HHH"); |
232 |
checkLocalChange(store2, changes, "00000000000008abcdef0123456789", OP_UPDATE, 2, "BBB"); |
233 |
checkLocalChange(store2, changes, "00000000000069abcdef0123456789", OP_INSERT, 5, "EEE"); |
234 |
checkLocalChange(store2, changes, "00000000000035abcdef0123456789", OP_DELETE, 7, "GGG"); |
235 |
|
236 |
r = ws2.commit(); |
237 |
assertEquals("ws2.commit-1 status", ERR_NO_ERROR, r);
|
238 |
|
239 |
r = ws1.updatePrepare("test");
|
240 |
assertEquals("ws2.update-2 status", ERR_NO_ERROR, r);
|
241 |
r = ws1.update("test");
|
242 |
assertEquals("ws1.update-1 status", 0, r); |
243 |
|
244 |
features = store1.getFeatures((Expression)null, "id", true); |
245 |
|
246 |
check(features, 0, 1, "AA2"); |
247 |
check(features, 1, 2, "BBB"); |
248 |
check(features, 2, 4, "DDD"); |
249 |
check(features, 3, 5, "EEE"); |
250 |
check(features, 4, 6, "FFF"); |
251 |
check(features, 5, 8, "HHH"); |
252 |
|
253 |
|
254 |
DisposeUtils.disposeQuietly(features); |
255 |
DisposeUtils.disposeQuietly(store2); |
256 |
DisposeUtils.disposeQuietly(store1); |
257 |
|
258 |
} |
259 |
|
260 |
|
261 |
} |