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 / TestS02Commit.java @ 3319
History | View | Annotate | Download (6.32 KB)
1 |
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.Writer; |
7 |
import java.net.URL; |
8 |
import java.util.ArrayList; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
import junit.framework.TestCase; |
12 |
import static junit.framework.TestCase.assertEquals; |
13 |
import org.apache.commons.io.FileUtils; |
14 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
15 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
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.VCSGisEntity; |
20 |
import org.gvsig.vcsgis.lib.VCSGisLocator; |
21 |
import org.gvsig.vcsgis.lib.VCSGisManager; |
22 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR; |
23 |
import org.gvsig.vcsgis.lib.impl.TestUtils; |
24 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
25 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData; |
26 |
import org.gvsig.vcsgis.lib.repository.remoteclient.requests.CommitRequestClient; |
27 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest; |
28 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisEntitiesRequest; |
29 |
import org.gvsig.vcsgis.lib.server.VCSGisServerHandler; |
30 |
import org.gvsig.vcsgis.lib.server.handlers.CommitHandler; |
31 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
32 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange; |
33 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChanges; |
34 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceImpl; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
|
39 |
public class TestS02Commit extends TestCase { |
40 |
|
41 |
private static final Logger LOGGER = LoggerFactory.getLogger(TestS02Commit.class); |
42 |
|
43 |
public TestS02Commit(String testName) { |
44 |
super(testName);
|
45 |
} |
46 |
|
47 |
@Override
|
48 |
protected void setUp() throws Exception { |
49 |
super.setUp();
|
50 |
new DefaultLibrariesInitializer().fullInitialize();
|
51 |
} |
52 |
|
53 |
@Override
|
54 |
protected void tearDown() throws Exception { |
55 |
super.tearDown();
|
56 |
} |
57 |
|
58 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
59 |
// public void testHello() {}
|
60 |
|
61 |
public void testCommit() throws Exception { |
62 |
final String testid = "CommitHandler"; |
63 |
final URL serverUrl = new URL("http://localhost:9810/"); |
64 |
|
65 |
VCSGisManager manager = VCSGisLocator.getVCSGisManager(); |
66 |
manager.setCodeGenerator(new SequentialCodeGenerator());
|
67 |
|
68 |
JDBCServerExplorerParameters server = TestUtils.getServerExplorerParameters("srv-"+testid);
|
69 |
File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws-"+testid)); |
70 |
|
71 |
int r = manager.initRepository(server, null); |
72 |
assertEquals("initRepository status", ERR_NO_ERROR, r);
|
73 |
|
74 |
VCSGisRepository repo = manager.openRepository(server); |
75 |
TestUtils.h2sql_repository(repo); |
76 |
|
77 |
// -------------------------
|
78 |
// Prepare the data in workspace
|
79 |
r = manager.initWorkspace(ws1file, repo, "Test "+testid, null); |
80 |
assertEquals("initWorkspace status", ERR_NO_ERROR, r);
|
81 |
|
82 |
VCSGisWorkspace ws1 = manager.openWorkspace(ws1file); |
83 |
assertNotNull("openWorkspace status", ws1);
|
84 |
TestUtils.h2sql_workspace(ws1); |
85 |
|
86 |
FeatureStore store2 = TestUtils.openSourceStore2(); |
87 |
ws1.add("test", store2, "text", null, "test"); |
88 |
assertEquals("ws1.add status", ERR_NO_ERROR, r);
|
89 |
|
90 |
DisposeUtils.disposeQuietly(store2); |
91 |
|
92 |
// -------------------------
|
93 |
// Check that in ws are the expected changes
|
94 |
VCSGisWorkspaceChanges<VCSGisWorkspaceChange> wschanges = ws1.getLocalChanges(); |
95 |
assertEquals("Changes before commit", 4, wschanges.size64()); |
96 |
|
97 |
// -------------------------
|
98 |
// Check that in the repo are not tables
|
99 |
VCSGisEntitiesRequest localdbEntityRequest = repo.createEntitiesRequest(); |
100 |
localdbEntityRequest.execute(); |
101 |
List<VCSGisEntity> entities = localdbEntityRequest.getRepositoryEntities();
|
102 |
assertEquals("Entities in repo before commit", 0, entities.size()); |
103 |
|
104 |
// -------------------------
|
105 |
// Check that the CommitRequestClient produce the expected json output
|
106 |
Map<String, String> expected = TestUtils.getExpecteds("S02Commit.txt"); |
107 |
|
108 |
VCSGisRepository client = manager.openRepository(serverUrl); |
109 |
CommitRequestClient request = (CommitRequestClient) client.createCommitRequest(); |
110 |
((VCSGisWorkspaceImpl)ws1).prepareCommitRequest(request, null, null, null, null, null); |
111 |
|
112 |
ByteArrayOutputStream request_stream = new ByteArrayOutputStream(10024); |
113 |
request.requestProducer(request_stream); |
114 |
String request_s = request_stream.toString().trim();
|
115 |
assertEquals("request data", expected.get("request"), request_s); |
116 |
|
117 |
// -------------------------
|
118 |
// Check that CommitHandler produce the expected json as response to request
|
119 |
ByteArrayInputStream is = new ByteArrayInputStream(request_stream.toByteArray()); |
120 |
VCSGisServerHandler handler = new CommitHandler(repo);
|
121 |
|
122 |
Writer response_writer = new ByteArrayWriter(); |
123 |
handler.handle(null, is, response_writer);
|
124 |
String response_s = response_writer.toString().trim();
|
125 |
|
126 |
assertEquals("response data", expected.get("response"), response_s); |
127 |
|
128 |
|
129 |
// -------------------------
|
130 |
// Check that in the repository are created the new table
|
131 |
localdbEntityRequest = repo.createEntitiesRequest(); |
132 |
localdbEntityRequest.execute(); |
133 |
entities = localdbEntityRequest.getRepositoryEntities(); |
134 |
assertEquals("Entities in repo after commit", 1, entities.size()); |
135 |
VCSGisEntity entity = entities.get(0);
|
136 |
assertEquals("Entity[0].name in repo after commit", "test", entity.getEntityName()); |
137 |
|
138 |
// -------------------------
|
139 |
// Check that the table in the repository have the expecteds values
|
140 |
VCSGisCheckoutRequest localdbCheckoutRequest = repo.createCheckoutRequest("test");
|
141 |
localdbCheckoutRequest.execute(); |
142 |
List<VCSGisRepositoryData> data = new ArrayList<>(); |
143 |
for (VCSGisRepositoryData repoData : localdbCheckoutRequest.getData()) {
|
144 |
data.add(repoData); |
145 |
} |
146 |
assertEquals("Num rows in repo after commit", 3, data.size()); |
147 |
} |
148 |
} |