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 / server / TestS05Update.java @ 2823
History | View | Annotate | Download (7.97 KB)
1 | 2708 | jjdelcerro | package org.gvsig.vcsgis.lib.impl.server; |
---|---|---|---|
2 | |||
3 | import java.io.ByteArrayInputStream; |
||
4 | import java.io.ByteArrayOutputStream; |
||
5 | import java.io.File; |
||
6 | import java.io.InputStream; |
||
7 | import java.net.URL; |
||
8 | 2715 | jjdelcerro | import java.sql.Timestamp; |
9 | 2823 | jjdelcerro | import java.util.List; |
10 | 2708 | jjdelcerro | import java.util.Map; |
11 | import junit.framework.TestCase; |
||
12 | import static junit.framework.TestCase.assertEquals; |
||
13 | 2712 | jjdelcerro | import static junit.framework.TestCase.assertNotNull; |
14 | 2708 | jjdelcerro | import org.apache.commons.io.FileUtils; |
15 | import org.apache.commons.io.IOUtils; |
||
16 | import org.apache.commons.lang3.StringUtils; |
||
17 | 2823 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
18 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
19 | import org.gvsig.fmap.dal.feature.Feature; |
||
20 | 2708 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
21 | 2715 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
22 | 2708 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
23 | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
||
24 | 2715 | jjdelcerro | import org.gvsig.vcsgis.lib.SequentialCodeGenerator; |
25 | 2823 | jjdelcerro | import org.gvsig.vcsgis.lib.VCSGisEntity; |
26 | 2708 | jjdelcerro | import org.gvsig.vcsgis.lib.VCSGisLocator; |
27 | import org.gvsig.vcsgis.lib.VCSGisManager; |
||
28 | import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR; |
||
29 | import org.gvsig.vcsgis.lib.impl.TestUtils; |
||
30 | import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
||
31 | 2823 | jjdelcerro | import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryChange; |
32 | 2712 | jjdelcerro | import org.gvsig.vcsgis.lib.repository.remoteclient.requests.CheckoutRequestClient; |
33 | 2823 | jjdelcerro | import org.gvsig.vcsgis.lib.repository.remoteclient.requests.UpdateRequestClient; |
34 | 2715 | jjdelcerro | import org.gvsig.vcsgis.lib.server.VCSGisServerHandler; |
35 | import org.gvsig.vcsgis.lib.server.handlers.CheckoutHandler; |
||
36 | 2823 | jjdelcerro | import org.gvsig.vcsgis.lib.server.handlers.UpdateHandler; |
37 | 2708 | jjdelcerro | import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
38 | 2823 | jjdelcerro | import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChanges; |
39 | 2708 | jjdelcerro | import org.slf4j.Logger; |
40 | import org.slf4j.LoggerFactory; |
||
41 | |||
42 | |||
43 | 2823 | jjdelcerro | public class TestS05Update extends TestCase { |
44 | 2708 | jjdelcerro | |
45 | 2823 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(TestS05Update.class); |
46 | 2708 | jjdelcerro | |
47 | 2823 | jjdelcerro | public TestS05Update(String testName) { |
48 | 2708 | jjdelcerro | super(testName);
|
49 | } |
||
50 | |||
51 | @Override
|
||
52 | protected void setUp() throws Exception { |
||
53 | super.setUp();
|
||
54 | new DefaultLibrariesInitializer().fullInitialize();
|
||
55 | } |
||
56 | |||
57 | @Override
|
||
58 | protected void tearDown() throws Exception { |
||
59 | super.tearDown();
|
||
60 | } |
||
61 | |||
62 | // TODO add test methods here. The name must begin with 'test'. For example:
|
||
63 | // public void testHello() {}
|
||
64 | |||
65 | 2823 | jjdelcerro | public void testUpdate() throws Exception { |
66 | 2715 | jjdelcerro | final String testid = "ServerCheckout"; |
67 | final URL serverUrl = new URL("http://localhost:9810/"); |
||
68 | |||
69 | VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
||
70 | manager.setCodeGenerator(new SequentialCodeGenerator());
|
||
71 | |||
72 | JDBCServerExplorerParameters serverParameters = TestUtils.getServerExplorerParameters("srv-"+testid);
|
||
73 | File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws1-"+testid)); |
||
74 | File ws2file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws2-"+testid)); |
||
75 | 2823 | jjdelcerro | |
76 | int r;
|
||
77 | Feature f; |
||
78 | EditableFeature ef; |
||
79 | List<Feature> features;
|
||
80 | FeatureStore store1; |
||
81 | FeatureStore store2; |
||
82 | 2715 | jjdelcerro | |
83 | 2823 | jjdelcerro | // ------------------------------------------------------------
|
84 | // Inicializamos el repositorio y lo abrimos
|
||
85 | r = manager.initRepository(serverParameters, null);
|
||
86 | assertEquals("init_server status", ERR_NO_ERROR, r);
|
||
87 | 2715 | jjdelcerro | |
88 | VCSGisRepository repoLocaldb = manager.openRepository(serverParameters); |
||
89 | TestUtils.h2sql_repository(repoLocaldb); |
||
90 | |||
91 | 2823 | jjdelcerro | // ------------------------------------------------------------
|
92 | // Creamos workspace1 y lo abrimos.
|
||
93 | r = manager.initWorkspace(ws1file, repoLocaldb, "Test update1",null); |
||
94 | assertEquals("init_ws1 status", ERR_NO_ERROR, r);
|
||
95 | 2715 | jjdelcerro | |
96 | VCSGisWorkspace ws1 = manager.openWorkspace(ws1file); |
||
97 | TestUtils.h2sql_workspace(ws1); |
||
98 | |||
99 | 2823 | jjdelcerro | // ------------------------------------------------------------
|
100 | // Adicionamos al workspace1 la tabla "test" y la commitamos.
|
||
101 | FeatureStore sourceTest = TestUtils.openSourceStore2(); |
||
102 | r = ws1.add("test", sourceTest, "text"); |
||
103 | 2715 | jjdelcerro | assertEquals("ws1.add status", ERR_NO_ERROR, r);
|
104 | 2823 | jjdelcerro | r = ws1.commit(); |
105 | assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
|
||
106 | DisposeUtils.disposeQuietly(sourceTest); |
||
107 | 2715 | jjdelcerro | |
108 | 2823 | jjdelcerro | |
109 | // ------------------------------------------------------------
|
||
110 | // Inicializamos el workspace2 asociandolo al mismo repositorio que
|
||
111 | // workspace1 y lo abrimos.
|
||
112 | r = manager.initWorkspace(ws2file, repoLocaldb, "Test update2",null); |
||
113 | assertEquals("init_ws2 status", ERR_NO_ERROR, r);
|
||
114 | VCSGisWorkspace ws2 = manager.openWorkspace(ws2file); |
||
115 | TestUtils.h2sql_workspace(ws2); |
||
116 | |||
117 | // ------------------------------------------------------------
|
||
118 | // Hacemos un checkout en workspace2 de la tabla "test"
|
||
119 | // y comprobamos que tiene lo que toca.
|
||
120 | r = ws2.checkout("test");
|
||
121 | assertEquals("ws2-checkout status", ERR_NO_ERROR, r);
|
||
122 | |||
123 | VCSGisEntity entity = ws2.getWorkspaceEntityByName("test");
|
||
124 | assertNotNull("ws2.update-1 entity", entity);
|
||
125 | assertEquals("ws2.update-1 label", "text", entity.getFieldForLabel()); |
||
126 | 2715 | jjdelcerro | |
127 | 2823 | jjdelcerro | store2 = ws2.getFeatureStore("test");
|
128 | assertEquals("ws2-checkout sizes", 3, store2.size64()); |
||
129 | features = store2.getFeatures((Expression)null, "id", true); |
||
130 | assertEquals(1 , features.get(0).getInt("id")); |
||
131 | assertEquals("AAA", features.get(0).getString("text")); |
||
132 | assertEquals(2 , features.get(1).getInt("id")); |
||
133 | assertEquals("BBB", features.get(1).getString("text")); |
||
134 | assertEquals(3 , features.get(2).getInt("id")); |
||
135 | assertEquals("CCC", features.get(2).getString("text")); |
||
136 | |||
137 | 2715 | jjdelcerro | DisposeUtils.disposeQuietly(store2); |
138 | |||
139 | 2823 | jjdelcerro | // ------------------------------------------------------------
|
140 | // Modificamos la tabla "test" en el workspace1 y la commitamos.
|
||
141 | store1 = ws1.getFeatureStore("test");
|
||
142 | store1.edit(); |
||
143 | f = store1.findFirst("id=2");
|
||
144 | ef = f.getEditable(); |
||
145 | ef.set("text", "BB2"); |
||
146 | store1.update(ef); |
||
147 | 2715 | jjdelcerro | |
148 | 2823 | jjdelcerro | ef = store1.createNewFeature(); |
149 | ef.set("id",4); |
||
150 | ef.set("text","DDD"); |
||
151 | store1.insert(ef); |
||
152 | store1.finishEditing(); |
||
153 | DisposeUtils.disposeQuietly(store1); |
||
154 | r = ws1.commit(Timestamp.valueOf("2020-02-17 10:11:12.123"), "Insertado DDD y modificado BBB a BB2", null); |
||
155 | assertEquals("ws1.commit-2 status", ERR_NO_ERROR, r);
|
||
156 | 2715 | jjdelcerro | |
157 | 2823 | jjdelcerro | String localRevisionCode = ws2.getWorkspaceEntity("test").getRepositoryRevisionCode(); |
158 | assertEquals("ws2 localRevisionCode", "00000000000015abcdef0123456789", localRevisionCode); |
||
159 | 2715 | jjdelcerro | |
160 | 2823 | jjdelcerro | ws1.dispose(); |
161 | ws2.dispose(); |
||
162 | ws1 = null;
|
||
163 | ws2 = null;
|
||
164 | |||
165 | 2715 | jjdelcerro | // -------------------------
|
166 | 2823 | jjdelcerro | // Check that the UpdateRequestClient produce the expected json output
|
167 | Map<String, String> expected = TestUtils.getExpecteds("S05Update.txt"); |
||
168 | 2715 | jjdelcerro | |
169 | VCSGisRepository repoRemote = manager.openRepository(serverUrl); |
||
170 | 2823 | jjdelcerro | UpdateRequestClient request = (UpdateRequestClient) repoRemote.createUpdateRequest("test");
|
171 | request.setLocalRevisionCode(localRevisionCode); |
||
172 | 2715 | jjdelcerro | |
173 | ByteArrayOutputStream request_stream = new ByteArrayOutputStream(10024); |
||
174 | request.requestProducer(request_stream); |
||
175 | String request_s = request_stream.toString().trim();
|
||
176 | assertEquals("request data", expected.get("request"), request_s); |
||
177 | |||
178 | // -------------------------
|
||
179 | // Check that CheckoutHandler produce the expected json as response to request
|
||
180 | ByteArrayInputStream is = new ByteArrayInputStream(request_stream.toByteArray()); |
||
181 | 2823 | jjdelcerro | VCSGisServerHandler handler = new UpdateHandler(repoLocaldb);
|
182 | 2715 | jjdelcerro | InputStream response_stream = handler.handle(is);
|
183 | |||
184 | String response_s = StringUtils.join(IOUtils.readLines(response_stream),'\n').trim(); |
||
185 | assertEquals("response data", expected.get("response"), response_s); |
||
186 | 2823 | jjdelcerro | |
187 | |||
188 | 2708 | jjdelcerro | } |
189 | } |