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 / Test11GetLocalChanges.java @ 2741
History | View | Annotate | Download (6.11 KB)
1 |
package org.gvsig.vcsgis.lib.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.List; |
5 |
import junit.framework.TestCase; |
6 |
import static junit.framework.TestCase.assertEquals; |
7 |
import org.apache.commons.io.FileUtils; |
8 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
9 |
import org.gvsig.fmap.dal.feature.Feature; |
10 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
11 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
12 |
import org.gvsig.tools.dispose.DisposeUtils; |
13 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
14 |
import org.gvsig.tools.util.GetItemWithSize64; |
15 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
16 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
17 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR; |
18 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE; |
19 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_INSERT; |
20 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_UPDATE; |
21 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
22 |
import org.gvsig.vcsgis.lib.vcsgisadmin; |
23 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
24 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange; |
25 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity; |
26 |
import org.slf4j.Logger; |
27 |
import org.slf4j.LoggerFactory; |
28 |
|
29 |
public class Test11GetLocalChanges extends TestCase { |
30 |
|
31 |
private static final Logger LOGGER = LoggerFactory.getLogger(Test11GetLocalChanges.class); |
32 |
|
33 |
public Test11GetLocalChanges(String testName) { |
34 |
super(testName);
|
35 |
} |
36 |
|
37 |
@Override
|
38 |
protected void setUp() throws Exception { |
39 |
super.setUp();
|
40 |
new DefaultLibrariesInitializer().fullInitialize();
|
41 |
} |
42 |
|
43 |
@Override
|
44 |
protected void tearDown() throws Exception { |
45 |
super.tearDown();
|
46 |
} |
47 |
|
48 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
49 |
// public void testHello() {}
|
50 |
|
51 |
public void testUpdate() throws Exception { |
52 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
53 |
|
54 |
JDBCServerExplorer server = TestUtils.openServerExplorer("srv-update");
|
55 |
File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws1-update")); |
56 |
|
57 |
int r;
|
58 |
Feature f; |
59 |
EditableFeature ef; |
60 |
List<Feature> features;
|
61 |
FeatureStore store1; |
62 |
|
63 |
// ------------------------------------------------------------
|
64 |
// Inicializamos el repositorio y lo abrimos
|
65 |
r = vcsgisadmin.init_repository(server); |
66 |
assertEquals("init_server status", ERR_NO_ERROR, r);
|
67 |
|
68 |
VCSGisRepository repo = vcsgisadmin.open_repository(server); |
69 |
TestUtils.h2sql_repository(repo); |
70 |
|
71 |
// ------------------------------------------------------------
|
72 |
// Creamos workspace1 y lo abrimos.
|
73 |
r = manager.initWorkspace(ws1file, repo, "Test update1",null); |
74 |
assertEquals("init_ws1 status", ERR_NO_ERROR, r);
|
75 |
|
76 |
VCSGisWorkspace ws1 = manager.openWorkspace(ws1file); |
77 |
TestUtils.h2sql_workspace(ws1); |
78 |
|
79 |
// ------------------------------------------------------------
|
80 |
// Adicionamos al workspace1 la tabla "test" y la commitamos.
|
81 |
FeatureStore sourceTest = TestUtils.openSourceStore2(); |
82 |
r = ws1.add("test", sourceTest, "text"); |
83 |
assertEquals("ws1.add status", ERR_NO_ERROR, r);
|
84 |
r = ws1.commit(); |
85 |
assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
|
86 |
DisposeUtils.disposeQuietly(sourceTest); |
87 |
|
88 |
|
89 |
// ------------------------------------------------------------
|
90 |
// Modificamos la tabla "test" en el workspace1 y no la commitamos.
|
91 |
store1 = ws1.getFeatureStore("test");
|
92 |
store1.edit(); |
93 |
f = store1.findFirst("id=2");
|
94 |
ef = f.getEditable(); |
95 |
ef.set("text", "BB2"); |
96 |
store1.update(ef); |
97 |
|
98 |
String code1 = ef.getString(VCSGisManager.FEATURECODE_FIELD_NAME);
|
99 |
assertNotNull("featureCode 1", code1);
|
100 |
|
101 |
ef = store1.createNewFeature(); |
102 |
ef.set("id",4); |
103 |
ef.set("text","DDD"); |
104 |
store1.insert(ef); |
105 |
|
106 |
String code2 = ef.getString(VCSGisManager.FEATURECODE_FIELD_NAME);
|
107 |
assertNotNull("featureCode 2", code2);
|
108 |
|
109 |
f = store1.findFirst("id=3");
|
110 |
store1.delete(f); |
111 |
|
112 |
String code3 = f.getString(VCSGisManager.FEATURECODE_FIELD_NAME);
|
113 |
assertNotNull("featureCode 3", code3);
|
114 |
|
115 |
|
116 |
store1.finishEditing(); |
117 |
DisposeUtils.disposeQuietly(store1); |
118 |
|
119 |
GetItemWithSize64<VCSGisWorkspaceChange> localChanges = ws1.getLocalChangesByEntityName("test");
|
120 |
|
121 |
assertEquals("localChangesSize", 3L, localChanges.size64()); |
122 |
VCSGisWorkspaceEntity entity = ws1.getWorkspaceEntityByName("test");
|
123 |
|
124 |
for(long i = 0; i<localChanges.size64(); i++){ |
125 |
VCSGisWorkspaceChange change = localChanges.get64(i); |
126 |
assertEquals(entity.getEntityCode(), change.getEntityCode()); |
127 |
|
128 |
if(change.getRelatedFeatureCode().equalsIgnoreCase(code1)){
|
129 |
assertEquals(OP_UPDATE, change.getOperation()); |
130 |
assertEquals("BB2", change.getLabel());
|
131 |
f = change.getRelatedFeature(); |
132 |
assertEquals(2, f.getInt("id")); |
133 |
assertEquals("BB2", f.getString("text")); |
134 |
|
135 |
|
136 |
} else if(change.getRelatedFeatureCode().equalsIgnoreCase(code2)){ |
137 |
assertEquals(OP_INSERT, change.getOperation()); |
138 |
assertEquals("DDD", change.getLabel());
|
139 |
f = change.getRelatedFeature(); |
140 |
assertEquals(4, f.getInt("id")); |
141 |
assertEquals("DDD", f.getString("text")); |
142 |
|
143 |
} else if(change.getRelatedFeatureCode().equalsIgnoreCase(code3)){ |
144 |
assertEquals(OP_DELETE, change.getOperation()); |
145 |
assertEquals("CCC", change.getLabel());
|
146 |
boolean ok = true; |
147 |
try {
|
148 |
change.getRelatedFeature(); |
149 |
ok = false;
|
150 |
} catch (Exception e) { |
151 |
} |
152 |
assertTrue("code3 deleted", ok);
|
153 |
|
154 |
} else {
|
155 |
assertTrue("Incorrect code.", false); |
156 |
} |
157 |
} |
158 |
|
159 |
} |
160 |
} |