Revision 4890

View differences:

org.gvsig.vcsgis/tags/org.gvsig.vcsgis-1.0.36/org.gvsig.vcsgis.app/org.gvsig.vcsgis.app.server/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
    <modelVersion>4.0.0</modelVersion>
3
    <parent>
4
        <groupId>org.gvsig</groupId>
5
        <artifactId>org.gvsig.vcsgis.app</artifactId>
6
        <version>1.0.36</version>
7
    </parent>
8
    <artifactId>org.gvsig.vcsgis.app.server</artifactId>
9
    <packaging>jar</packaging>
10
    <name>${project.artifactId}</name>
11
    <properties>
12
        <!-- Package info property values -->
13
        <gvsig.package.info.state>testing</gvsig.package.info.state>
14
        <gvsig.package.info.official>true</gvsig.package.info.official>
15
        <gvsig.package.info.name>vcsgis-server</gvsig.package.info.name>
16
        <gvsig.package.info.description>Provides a implementation of a HTTP server of VCSGis protocol (Version Control System for GIS).</gvsig.package.info.description>
17
        <gvsig.package.info.categories />
18
        <gvsig.package.info.javaVM>j1_8</gvsig.package.info.javaVM>
19
        <gvsig.package.info.poolURL>https://devel.gvsig.org/download/projects/gvsig-vcsgis/pool</gvsig.package.info.poolURL>
20
    </properties>
21

  
22
    <dependencies>
23
        <dependency>
24
            <groupId>org.gvsig</groupId>
25
            <artifactId>org.gvsig.andami</artifactId>
26
            <scope>compile</scope>
27
        </dependency>
28
        <dependency>
29
            <groupId>org.gvsig</groupId>
30
            <artifactId>org.gvsig.vcsgis.swing.api</artifactId>
31
            <scope>compile</scope>
32
        </dependency>
33
        <dependency>
34
            <groupId>org.gvsig</groupId>
35
            <artifactId>org.gvsig.vcsgis.swing.impl</artifactId>
36
            <scope>test</scope>
37
        </dependency>
38
        <dependency>
39
            <groupId>org.gvsig</groupId>
40
            <artifactId>org.gvsig.vcsgis.lib.api</artifactId>
41
            <scope>compile</scope>
42
        </dependency>
43

  
44
        <dependency>
45
            <groupId>org.gvsig</groupId>
46
            <artifactId>org.gvsig.app.mainplugin</artifactId>
47
            <scope>compile</scope>
48
        </dependency>
49
        <dependency>
50
            <groupId>org.gvsig</groupId>
51
            <artifactId>org.gvsig.app.document.table.app.mainplugin</artifactId>
52
            <scope>compile</scope>
53
        </dependency>
54
        
55
        <dependency>
56
            <groupId>org.gvsig</groupId>
57
            <artifactId>org.gvsig.vcsgis.tomcat.webapp</artifactId>
58
            <scope>runtime</scope>
59
            <type>war</type>
60
        </dependency>
61
        
62
        <dependency>
63
            <groupId>org.eclipse.jetty</groupId>
64
            <artifactId>jetty-server</artifactId>
65
            <scope>compile</scope>
66
        </dependency>
67
        <dependency>
68
            <groupId>org.eclipse.jetty</groupId>
69
            <artifactId>jetty-servlet</artifactId>
70
            <scope>compile</scope>
71
        </dependency>
72
        <dependency>
73
            <groupId>org.eclipse.jetty</groupId>
74
            <artifactId>jetty-webapp</artifactId>
75
            <scope>compile</scope>
76
            <exclusions>
77
                <exclusion>
78
                    <groupId>javax.servlet</groupId>
79
                    <artifactId>servlet-api</artifactId>
80
                </exclusion>
81
            </exclusions>
82
        </dependency>
83
        <dependency>
84
            <groupId>org.eclipse.jetty</groupId>
85
            <artifactId>jetty-util</artifactId>
86
            <scope>compile</scope>
87
        </dependency>
88
        <dependency>
89
            <groupId>org.eclipse.jetty</groupId>
90
            <artifactId>jetty-io</artifactId>
91
            <scope>compile</scope>
92
        </dependency>
93
        <dependency>
94
            <groupId>org.eclipse.jetty</groupId>
95
            <artifactId>jetty-jndi</artifactId>
96
            <scope>compile</scope>
97
        </dependency>
98

  
99
        <dependency>
100
            <groupId>org.gvsig</groupId>
101
            <artifactId>org.gvsig.vcsgis.lib.impl</artifactId>
102
            <scope>test</scope>
103
        </dependency>
104
        <dependency>
105
            <groupId>org.gvsig</groupId>
106
            <artifactId>org.gvsig.fmap.dal.impl</artifactId>
107
            <scope>test</scope>
108
        </dependency>
109
        <dependency>
110
            <groupId>org.gvsig</groupId>
111
            <artifactId>${org.gvsig.h2spatial.provider}</artifactId>
112
            <scope>test</scope>
113
        </dependency>
114
        <dependency>
115
            <groupId>org.gvsig</groupId>
116
            <artifactId>${org.gvsig.fmap.geometry.impl}</artifactId>
117
            <scope>test</scope>
118
        </dependency>    
119
        <dependency>
120
            <groupId>org.gvsig</groupId>
121
            <artifactId>org.gvsig.expressionevaluator.lib.impl</artifactId>
122
            <scope>test</scope>
123
        </dependency>    
124
        <dependency>
125
            <groupId>org.gvsig</groupId>
126
            <artifactId>org.gvsig.expressionevaluator.geometry.lib.impl</artifactId>
127
            <scope>test</scope>
128
        </dependency>    
129
        <dependency>
130
            <groupId>org.gvsig</groupId>
131
            <artifactId>${org.gvsig.proj.lib.impl}</artifactId>
132
            <scope>test</scope>
133
        </dependency>
134
        <dependency>
135
            <groupId>org.gvsig</groupId>
136
            <artifactId>org.gvsig.compat.se</artifactId>
137
            <scope>test</scope>
138
        </dependency>
139
        <dependency>
140
            <groupId>org.gvsig</groupId>
141
            <artifactId>org.gvsig.metadata.lib.basic.impl</artifactId>
142
            <scope>test</scope>
143
        </dependency>      
144
        <dependency>
145
            <groupId>org.gvsig</groupId>
146
            <artifactId>org.gvsig.timesupport.lib.impl</artifactId>
147
            <scope>test</scope>
148
        </dependency>
149
        <dependency>
150
            <groupId>org.gvsig</groupId>
151
            <artifactId>org.gvsig.fmap.dal.file.csv</artifactId>
152
            <scope>test</scope>
153
        </dependency>
154
        <dependency>
155
            <groupId>org.gvsig</groupId>
156
            <artifactId>org.gvsig.tools.util.impl</artifactId>
157
            <scope>test</scope>
158
        </dependency>
159
        <dependency>
160
            <groupId>org.gvsig</groupId>
161
            <artifactId>org.gvsig.tools.swing.impl</artifactId>
162
            <scope>test</scope>
163
        </dependency>
164
                  
165
    </dependencies>
166
    <build>
167
        <plugins>
168

  
169
            <plugin>
170
                <groupId>org.apache.maven.plugins</groupId>
171
                <artifactId>maven-surefire-plugin</artifactId>
172
                <configuration>
173
                    <!-- Skip test execution ? -->
174
                    <skipTests>false</skipTests>
175
                    <!-- Ignore test execution failure ? -->
176
                    <testFailureIgnore>true</testFailureIgnore>
177
                </configuration>
178
            </plugin>
179
        
180
            <!-- Skip test compilation ? -->
181
            <plugin>
182
                <groupId>org.apache.maven.plugins</groupId>
183
                <artifactId>maven-compiler-plugin</artifactId>
184
                <executions>
185
                    <execution>
186
                        <id>default-testCompile</id>
187
                        <phase>process-test-sources</phase>
188
                        <goals>
189
                            <goal>testCompile</goal>
190
                        </goals>
191
                        <configuration>
192
                            <skip>false</skip>
193
                        </configuration>
194
                    </execution>
195
                </executions>
196
            </plugin>
197

  
198
        </plugins>
199
    </build>
200
    
201
</project>
org.gvsig.vcsgis/tags/org.gvsig.vcsgis-1.0.36/org.gvsig.vcsgis.app/org.gvsig.vcsgis.app.server/buildNumber.properties
1
#Thu Nov 25 20:27:03 CET 2021
2
buildNumber=43
org.gvsig.vcsgis/tags/org.gvsig.vcsgis-1.0.36/org.gvsig.vcsgis.app/org.gvsig.vcsgis.app.server/src/test/java/org/gvsig/vcsgis/app/server/TestCommons.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.vcsgis.app.server;
7

  
8
import java.sql.Timestamp;
9
import java.time.LocalDate;
10
import java.util.ArrayList;
11
import java.util.HashMap;
12
import java.util.List;
13
import java.util.Map;
14
import junit.framework.TestCase;
15
import static junit.framework.TestCase.assertEquals;
16
import static junit.framework.TestCase.assertNotNull;
17
import org.gvsig.expressionevaluator.Expression;
18
import org.gvsig.fmap.dal.exception.DataException;
19
import org.gvsig.fmap.dal.feature.EditableFeature;
20
import org.gvsig.fmap.dal.feature.Feature;
21
import org.gvsig.fmap.dal.feature.FeatureStore;
22
import static org.gvsig.tools.dataTypes.DataTypeUtils.toTimestamp;
23
import org.gvsig.tools.dispose.DisposeUtils;
24
import org.gvsig.tools.util.GetItemWithSize64;
25
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR;
26
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE;
27
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
28
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange;
29
import org.gvsig.vcsgis.lib.workspace.tables.RemoteChangesTable;
30

  
31
/**
32
 *
33
 * @author fdiaz
34
 */
35
public class TestCommons {
36
    
37
    public static void test_add(VCSGisWorkspace ws) throws Exception {
38
        FeatureStore sourceTest = TestUtils.openSourceStore2();
39
        int r = ws.add("test", sourceTest, "text");
40
        DisposeUtils.disposeQuietly(sourceTest);        
41
        TestCase.assertEquals("ws.add status", ERR_NO_ERROR, r);
42
    }
43
    
44
    public static void test_insert(FeatureStore store, int id, String text) throws DataException{
45
        EditableFeature ef = store.createNewFeature();
46
        ef.set("id",id);
47
        ef.set("text",text);
48
        store.insert(ef);
49
    }
50
    
51
    public static void test_update(FeatureStore store, int id, String text) throws DataException{
52
        Feature f = store.findFirst("id="+id);
53
        EditableFeature ef = f.getEditable();
54
        ef.set("text", text);
55
        store.update(ef);
56
    }
57
    
58
    public static void test_delete(FeatureStore store, int id, String text) throws DataException{
59
        Feature f = store.findFirst("id="+id);
60
        store.delete(f);
61
    }
62
    
63
    public static void test_check(List<Feature> features, int index, int id, String text){
64
        TestCase.assertEquals(id    , features.get(index).getInt("id"));
65
        TestCase.assertEquals(text, features.get(index).getString("text"));
66
    }
67

  
68
    public static void test_check(FeatureStore store2, String relatedFeatureCode, int id, String text) throws Exception {
69
        Feature f = store2.findFirst("VCSGISCODE = '"+relatedFeatureCode+"'");
70
        TestCase.assertEquals(id, f.getInt("id"));
71
        TestCase.assertEquals(text, f.getString("text"));
72
    }
73
    
74
    public static void test_checkall(VCSGisWorkspace ws, Object... values) throws Exception {
75
        FeatureStore store = ws.openFeatureStore("test",false);
76
        assertEquals("ws test-size", values.length/2, store.size64());
77
        List<Feature> features = store.getFeatures((Expression)null, "id", true);
78
        int index = 0;
79
        for (int i = 0; i < values.length; i+=2) {
80
            int id = (int) values[i];
81
            String text = (String) values[i+1];
82
            TestCase.assertEquals(id    , features.get(index).getInt("id"));
83
            TestCase.assertEquals(text, features.get(index).getString("text"));
84
            index++;
85
        }
86
        DisposeUtils.disposeQuietly(store);
87
    }
88
    
89
    public static void test_modify(VCSGisWorkspace ws, Object... values) throws Exception {
90
        FeatureStore store = ws.openFeatureStore("test",false);
91
        store.edit();
92
        for (int i = 0; i < values.length; i+=3) {
93
            String operation = (String) values[i];
94
            int id = (int) values[i+1];
95
            String text = (String) values[i+2];
96
            switch(operation.toLowerCase()) {
97
                case "insert":
98
                    test_insert(store, id, text);
99
                    break;
100
                case "update":
101
                    test_update(store, id, text);
102
                    break;
103
                case "delete":
104
                    test_delete(store, id, text);
105
                    break;
106
                default:
107
                    TestCase.assertTrue("Illegal argument", false);
108
            }
109
        }
110
        store.finishEditing();
111
        DisposeUtils.disposeQuietly(store);
112
    }
113

  
114
    public static void test_updatePrepare(VCSGisWorkspace ws) {
115
        int r = ws.updatePrepare("test");
116
        assertEquals("ws.updatePrepare status", ERR_NO_ERROR, r);
117
    }
118
    
119
    public static void test_update(VCSGisWorkspace ws) {
120
        int r = ws.update("test");
121
        assertEquals("ws.update status", ERR_NO_ERROR, r);
122
    }
123
    
124
    public static void test_checkout(VCSGisWorkspace ws) {
125
        int r = ws.checkout("test");
126
        assertEquals("ws.checkout status", ERR_NO_ERROR, r);
127
    }
128
    
129
    public static void test_merge(VCSGisWorkspace ws) {
130
        int r = ws.merge("test");
131
        assertEquals("ws.merge status", ERR_NO_ERROR, r);
132
    }
133
    
134
    public static void test_commit(VCSGisWorkspace ws) {
135
        int r = ws.commit();
136
        assertEquals("ws.commit status", ERR_NO_ERROR, r);
137
    }
138
    
139
    public static void test_commit(VCSGisWorkspace ws, Timestamp revisionDate, Timestamp efectiveDate, String comment) {
140
        int r = ws.commit(revisionDate, efectiveDate, comment, null);
141
        assertEquals("ws.commit status", ERR_NO_ERROR, r);
142
    }
143
    
144
    public static void test_commit(VCSGisWorkspace ws, LocalDate revisionDate, LocalDate efectiveDate, String comment) {
145
        int r = ws.commit(toTimestamp(revisionDate), toTimestamp(efectiveDate), comment, null);
146
        assertEquals("ws.commit status", ERR_NO_ERROR, r);
147
    }
148
    
149
    public static void test_commit(VCSGisWorkspace ws, int expectedStatus) {
150
        int r = ws.commit();
151
        assertEquals("ws.commit status", expectedStatus, r);
152
    }
153
    
154
    public static void test_checklocalchanges(VCSGisWorkspace ws, Object... values) {
155
        class Value { 
156
            int operation; int id; String text; String relatedFeatureCode; 
157

  
158
            @Override
159
            public String toString() {
160
                return String.format("op=%d id=%d text='%s' rfc='%s'", operation, id, text, relatedFeatureCode);
161
            }
162
            
163
        }
164
        
165
        GetItemWithSize64<VCSGisWorkspaceChange> localChanges = ws.getLocalChanges(); 
166
        if( values == null ) {
167
            assertEquals("localChangesSize", 0, localChanges.size64());
168
            return;
169
        }
170
        assertEquals("localChangesSize", values.length/3, localChanges.size64());
171
        
172
        List<Value> changes = new ArrayList<>();
173
        Map<String,Value> changes_text = new HashMap<>();
174
        Map<Integer,Value> changes_id = new HashMap<>();
175
        
176
        for(long i = 0; i<localChanges.size64(); i++){
177
            VCSGisWorkspaceChange change = localChanges.get64(i);    
178
            Value value = new Value();
179
            value.relatedFeatureCode = change.getRelatedFeatureCode();
180
            value.operation = change.getOperation();
181
            try {
182
                Feature rf = change.getRelatedFeature();
183
                if( rf == null ) {
184
                    value.id = -1;
185
                    value.text = null;
186
                } else {
187
                    value.id = rf.getInt("id");
188
                    value.text = rf.getString("text");
189
                }
190
            } catch(Exception ex) {
191
                value.id = -1;
192
                value.text = change.getLabel();
193
            }
194
            changes_text.put(value.text, value);
195
            changes_id.put(value.id, value);
196
            changes.add(value);
197
        }
198
        for (int i = 0; i < values.length; i+=3 ) {
199
            int operation = (int) values[i+0];
200
            int id = (int) values[i+1];
201
            String text = (String) values[i+2];
202
            Value change;
203
            if( operation == OP_DELETE ) {
204
                change = changes_text.get(text);
205
                assertNotNull(change);
206
                assertEquals(text, change.text);
207
                assertEquals(operation, change.operation);
208
            } else {
209
                change = changes_id.get(id);
210
                assertNotNull(change);
211
                assertEquals(text, change.text);
212
                assertEquals(id, change.id);
213
                assertEquals(operation, change.operation);
214
            }
215
        }
216
    }
217

  
218
    public static void test_selectallremotechanges(VCSGisWorkspace ws) throws Exception {
219
        FeatureStore store = ws.openFeatureStore(RemoteChangesTable.TABLE_NAME,false);
220
        store.edit();
221
        store.update(RemoteChangesTable.SELECTED, true);
222
        store.finishEditing();
223
    }
224

  
225
    public static void test_revert(VCSGisWorkspace ws, String entityNameOrCode) {
226
        int r = ws.revert(entityNameOrCode);
227
        assertEquals("ws.revert status", ERR_NO_ERROR, r);
228
    }
229

  
230
    
231
}
org.gvsig.vcsgis/tags/org.gvsig.vcsgis-1.0.36/org.gvsig.vcsgis.app/org.gvsig.vcsgis.app.server/src/test/java/org/gvsig/vcsgis/app/server/TestSrv01AddCommit.java
1
package org.gvsig.vcsgis.app.server;
2

  
3
import java.io.File;
4
import java.net.URL;
5
import java.util.Locale;
6
import junit.framework.TestCase;
7
import static junit.framework.TestCase.assertEquals;
8
import org.apache.commons.io.FileUtils;
9
import org.eclipse.jetty.server.Server;
10
import org.gvsig.fmap.dal.feature.FeatureStore;
11
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
12
import org.gvsig.tools.dispose.DisposeUtils;
13
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
14
import static org.gvsig.vcsgis.app.server.VCSGisJettyServerUtils.SERVER_PORT;
15
import org.gvsig.vcsgis.lib.VCSGisEntity;
16
import org.gvsig.vcsgis.lib.VCSGisLocator;
17
import org.gvsig.vcsgis.lib.VCSGisManager;
18
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
19
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb;
20
import org.gvsig.vcsgis.lib.repository.localdb.tables.DataRepoTable;
21
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
22
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

  
26
public class TestSrv01AddCommit extends TestCase {
27

  
28
    private static final Logger LOGGER = LoggerFactory.getLogger(TestSrv01AddCommit.class);
29

  
30
    static{
31
            Locale.setDefault(Locale.forLanguageTag("es-ES"));
32
    }
33

  
34
    public TestSrv01AddCommit(String testName) {
35
        super(testName);
36
    }
37

  
38
    @Override
39
    protected void setUp() throws Exception {
40
        super.setUp();
41
        new DefaultLibrariesInitializer().fullInitialize();
42
    }
43

  
44
    @Override
45
    protected void tearDown() throws Exception {
46
        super.tearDown();
47
    }
48

  
49
    // TODO add test methods here. The name must begin with 'test'. For example:
50
    // public void testHello() {}
51
    public void testAddCommit() throws Exception {
52
        final String testid = "ServerAddCommit";
53
        final URL serverUrl = new URL("http://localhost:" + SERVER_PORT + "/");
54

  
55
        VCSGisManager vcsgismanager = VCSGisLocator.getVCSGisManager();
56
        JDBCServerExplorerParameters explorerParams = TestUtils.getServerExplorerParameters("srv-" + testid);
57
        File wsfile = TestUtils.getFile(FileUtils.getFile("test-dbs", "ws-" + testid));
58

  
59
        // ---------------------------------------------------------
60
        // Creamos el server y obtenemos el repositorio del server
61
        int r = vcsgismanager.initRepository(explorerParams, null);
62
        assertEquals("srv-init status", 0, r);
63

  
64
        Server server = TestUtils.createServer(explorerParams);
65
        VCSGisWorkspaceEntity lentity = null;
66
        long expectedRows = -1;
67
        try {
68
            server.start();
69

  
70
            VCSGisRepository repositoryRemote = vcsgismanager.openRepository(serverUrl);
71

  
72
            // ---------------------------------------------------------
73
            // Creamos un workspace y preparamos los datos en el para hacer un commit
74
            r = vcsgismanager.initWorkspace(wsfile, repositoryRemote, "Test add and commit", null);
75
            assertEquals("ws-init status", 0, r);
76

  
77
            VCSGisWorkspace ws = vcsgismanager.openWorkspace(wsfile);
78
            TestUtils.h2sql_workspace(ws);
79

  
80
            FeatureStore sourceStore = TestUtils.openSourceStore1();
81
            expectedRows = sourceStore.getFeatureCount();
82

  
83
            r = ws.add("sourceStore1", sourceStore, "id");
84
            assertEquals("ws-add status", 0, r);
85

  
86
            DisposeUtils.disposeQuietly(sourceStore);
87

  
88
            r = ws.commit();
89
            assertEquals("ws-commit status", 0, r);
90

  
91
            lentity = ws.getWorkspaceEntity("sourceStore1");
92
        } finally {
93
            server.stop();
94
        }
95
        // ---------------------------------------------------------
96
        // Comprobamos que la tabla adicionada al espacio ed trabajo contiene
97
        // los mismos registros que la original.
98

  
99
        VCSGisRepositoryLocaldb repositoryLocal = (VCSGisRepositoryLocaldb) vcsgismanager.openRepository(explorerParams);
100
        VCSGisEntity rentity = repositoryLocal.getEntityByName("sourceStore1");
101

  
102
        assertEquals("Check revision codes 1", lentity.getLocalRevisionCode(), rentity.getRepositoryRevisionCode());
103
        assertEquals("Check revision codes 2", lentity.getRepositoryRevisionCode(), rentity.getRepositoryRevisionCode());
104

  
105
        FeatureStore dataStore = repositoryLocal.getFeatureStore(DataRepoTable.TABLE_NAME, true);
106
        assertEquals("DataStore", expectedRows, dataStore.size64());
107

  
108
        DisposeUtils.disposeQuietly(dataStore);
109

  
110
        repositoryLocal.dispose();
111
    }
112
}
org.gvsig.vcsgis/tags/org.gvsig.vcsgis-1.0.36/org.gvsig.vcsgis.app/org.gvsig.vcsgis.app.server/src/test/java/org/gvsig/vcsgis/app/server/TestSrv06Authenticate.java
1
package org.gvsig.vcsgis.app.server;
2

  
3
import java.io.File;
4
import java.net.URL;
5
import java.util.List;
6
import java.util.Locale;
7
import junit.framework.TestCase;
8
import static junit.framework.TestCase.assertEquals;
9
import org.apache.commons.io.FileUtils;
10
import org.eclipse.jetty.server.Server;
11
import org.gvsig.fmap.dal.feature.FeatureStore;
12
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
13
import org.gvsig.tools.dispose.DisposeUtils;
14
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
15
import static org.gvsig.vcsgis.app.server.VCSGisJettyServerUtils.SERVER_PORT;
16
import org.gvsig.vcsgis.lib.VCSGisEntity;
17
import org.gvsig.vcsgis.lib.VCSGisLocator;
18
import org.gvsig.vcsgis.lib.VCSGisManager;
19
import org.gvsig.vcsgis.lib.VCSGisUserIdentificationRequester;
20
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
21
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb;
22
import org.gvsig.vcsgis.lib.repository.localdb.tables.ConfigRepoTable;
23
import org.gvsig.vcsgis.lib.repository.localdb.tables.UsersRepoTable;
24
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

  
28
public class TestSrv06Authenticate extends TestCase {
29

  
30
    private static final Logger LOGGER = LoggerFactory.getLogger(TestSrv06Authenticate.class);
31

  
32
    static{
33
            Locale.setDefault(Locale.forLanguageTag("es-ES"));
34
    }
35

  
36
    public TestSrv06Authenticate(String testName) {
37
        super(testName);
38
    }
39

  
40
    @Override
41
    protected void setUp() throws Exception {
42
        super.setUp();
43
        new DefaultLibrariesInitializer().fullInitialize();
44
    }
45

  
46
    @Override
47
    protected void tearDown() throws Exception {
48
        super.tearDown();
49
    }
50

  
51
    // TODO add test methods here. The name must begin with 'test'. For example:
52
    // public void testHello() {}
53
    public void testAuthenticatedCommit() throws Exception {
54
        final String testid = "ServerAddCommit";
55
        final URL serverUrl = new URL("http://localhost:" + SERVER_PORT + "/");
56

  
57
        VCSGisManager vcsgismanager = VCSGisLocator.getVCSGisManager();
58
        JDBCServerExplorerParameters explorerParams = TestUtils.getServerExplorerParameters("srv-" + testid);
59
        File wsfile = TestUtils.getFile(FileUtils.getFile("test-dbs", "ws-" + testid));
60

  
61
        // ---------------------------------------------------------
62
        // Creamos el server y obtenemos el repositorio del server
63
        int r = vcsgismanager.initRepository(explorerParams, null);
64
        assertEquals("srv-init status", 0, r);
65

  
66
        VCSGisRepositoryLocaldb repositoryLocal = (VCSGisRepositoryLocaldb) vcsgismanager.openRepository(explorerParams);
67
        enableAuthentication(repositoryLocal);
68
        repositoryLocal.dispose();
69

  
70
        Server server = TestUtils.createServer(explorerParams);
71
        try {
72
            server.start();
73

  
74
            VCSGisRepository repositoryRemote = vcsgismanager.openRepository(serverUrl);
75

  
76
            // ---------------------------------------------------------
77
            // Creamos un workspace y preparamos los datos en el para hacer un commit
78
            r = vcsgismanager.initWorkspace(wsfile, repositoryRemote, "Test add and commit", null);
79
            assertEquals("ws-init status", 0, r);
80

  
81
            VCSGisWorkspace ws = vcsgismanager.openWorkspace(wsfile);
82
            TestUtils.h2sql_workspace(ws);
83

  
84
            ws.setUserIdentificationRequester(new VCSGisUserIdentificationRequester() {
85
                int retries = 0;
86

  
87
                @Override
88
                public boolean requestIdentification() {
89
                    if (retries++ > 5) {
90
                        return false;
91
                    }
92
                    return true;
93
                }
94

  
95
                @Override
96
                public String getUserId() {
97
                    return "test";
98
                }
99

  
100
                @Override
101
                public String getPassword() {
102
                    return "password";
103
                }
104
            });
105

  
106
            FeatureStore sourceStore = TestUtils.openSourceStore1();
107
            long expectedRows = sourceStore.getFeatureCount();
108

  
109
            r = ws.add("sourceStore1", sourceStore, "ID");
110
            assertEquals("ws-add status", 0, r);
111

  
112
            DisposeUtils.disposeQuietly(sourceStore);
113

  
114
            r = ws.commit();
115
            assertEquals("ws-commit status", 0, r);
116

  
117
            // ---------------------------------------------------------
118
            // Comprobamos que en el server esta la tabla que acabamos de adicionar
119
            ws.updateEntitiesFromRepository(null);
120
            List<VCSGisEntity> rentities = ws.getRepositoryEntities();
121
            assertEquals("Num entities in server", 1, rentities.size());
122
            assertEquals("Name of the entity in server", "sourceStore1", rentities.get(0).getEntityName());
123
        } finally {
124

  
125
            server.stop();
126
        }
127
    }
128

  
129
    private void enableAuthentication(VCSGisRepositoryLocaldb repo) {
130
        ConfigRepoTable varsTable = new ConfigRepoTable();
131
        varsTable.set(repo, "AUTHENTICATION", String.valueOf(true));
132

  
133
        UsersRepoTable.UserRepoRow userRow = new UsersRepoTable.UserRepoRow(repo);
134
        userRow.setIdentifier("test");
135
        userRow.setPassword("password");
136
        userRow.insert();
137

  
138
    }
139

  
140
}
org.gvsig.vcsgis/tags/org.gvsig.vcsgis-1.0.36/org.gvsig.vcsgis.app/org.gvsig.vcsgis.app.server/src/test/java/org/gvsig/vcsgis/app/server/TestSrv14UpdateWithConflict.java
1
package org.gvsig.vcsgis.app.server;
2

  
3
import java.io.File;
4
import java.net.URL;
5
import java.util.List;
6
import java.util.Locale;
7
import junit.framework.TestCase;
8
import static junit.framework.TestCase.assertEquals;
9
import org.apache.commons.io.FileUtils;
10
import org.eclipse.jetty.server.Server;
11
import org.gvsig.expressionevaluator.Expression;
12
import org.gvsig.fmap.dal.exception.DataException;
13
import org.gvsig.fmap.dal.feature.EditableFeature;
14
import org.gvsig.fmap.dal.feature.Feature;
15
import org.gvsig.fmap.dal.feature.FeatureStore;
16
import static org.gvsig.fmap.dal.feature.FeatureStore.MODE_PASS_THROUGH;
17
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
18
import org.gvsig.tools.dispose.DisposeUtils;
19
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
20
import static org.gvsig.vcsgis.app.server.VCSGisJettyServerUtils.SERVER_PORT;
21
import org.gvsig.vcsgis.lib.SequentialCodeGenerator;
22
import org.gvsig.vcsgis.lib.VCSGisLocator;
23
import org.gvsig.vcsgis.lib.VCSGisManager;
24
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR;
25
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_UPDATE_NEED_MERGE;
26
import static org.gvsig.vcsgis.lib.VCSGisManager.FEATURECODE_FIELD_NAME;
27
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE;
28
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_INSERT;
29
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_CONFLICT;
30
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
31
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb;
32
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
33
import org.gvsig.vcsgis.lib.workspace.tables.RemoteChangesTable;
34
import org.gvsig.vcsgis.lib.workspace.tables.WorkspaceChangesTable;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

  
38
public class TestSrv14UpdateWithConflict extends TestCase {
39

  
40
    private static final Logger LOGGER = LoggerFactory.getLogger(TestSrv14UpdateWithConflict.class);
41
    private Server server;
42

  
43
    static{
44
            Locale.setDefault(Locale.forLanguageTag("es-ES"));
45
    }
46

  
47
    public TestSrv14UpdateWithConflict(String testName) {
48
        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
    private void insert(FeatureStore store, int id, String text) throws DataException {
63
        EditableFeature ef = store.createNewFeature();
64
        ef.set("id", id);
65
        ef.set("text", text);
66
        store.insert(ef);
67
    }
68

  
69
    private void update(FeatureStore store, int id, String text) throws DataException {
70
        Feature f = store.findFirst("id=" + id);
71
        EditableFeature ef = f.getEditable();
72
        ef.set("text", text);
73
        store.update(ef);
74
    }
75

  
76
    private void delete(FeatureStore store, int id, String text) throws DataException {
77
        Feature f = store.findFirst("id=" + id);
78
        store.delete(f);
79
    }
80

  
81
    private void check(List<Feature> features, int index, int id, String text) {
82
        assertEquals(id, features.get(index).getInt("id"));
83
        assertEquals(text, features.get(index).getString("text"));
84
    }
85

  
86
    private void check(FeatureStore store2, String relatedFeatureCode, int id, String text) throws Exception {
87
        Feature f = store2.findFirst("VCSGISCODE = '" + relatedFeatureCode + "'");
88
        assertEquals(id, f.getInt("id"));
89
        assertEquals(text, f.getString("text"));
90
    }
91

  
92
    private void selectRemoteChange(VCSGisWorkspace ws, boolean select, int id, String text, VCSGisWorkspace wsSource) throws Exception {
93
        Feature fTest;
94
        FeatureStore testStore = ws.openFeatureStore("test",false);
95
        FeatureStore testStoreSource = wsSource.openFeatureStore("test",false);
96

  
97
        fTest = testStoreSource.findFirst("id = " + id);
98

  
99
        FeatureStore rchangesStore = ws.openFeatureStore(RemoteChangesTable.TABLE_NAME,false);
100
        Feature fChanges = rchangesStore.findFirst("RCH_DATACODE = '" + fTest.getString(FEATURECODE_FIELD_NAME) + "'");
101
        EditableFeature efChanges = fChanges.getEditable();
102
        rchangesStore.edit(MODE_PASS_THROUGH);
103
        efChanges.set("RCH_SELECTED", select);
104
        rchangesStore.update(efChanges);
105
        rchangesStore.finishEditing();
106
        DisposeUtils.dispose(testStore);
107
        DisposeUtils.dispose(testStoreSource);
108
        DisposeUtils.dispose(rchangesStore);
109
    }
110

  
111
    private void checkLocalChange(FeatureStore data, FeatureStore changes, String code, int operation, int id, String text, int status) throws DataException {
112
        Feature change = changes.findFirst("WSCH_FEATURECODE = '" + code + "'");
113
        assertNotNull("Change code not exists", change);
114
        assertEquals(operation, change.getInt(WorkspaceChangesTable.OPERATION));
115
        if (status >= 0) {
116
            assertEquals(status, change.getInt(WorkspaceChangesTable.STATUS));
117
        }
118
        // Uf, ojo con operation delete, no tiene data. en la primera pasada lo saltaria, y a ver que pasa con
119
        // los inserts y updates.
120
//        if(operation != OP_DELETE){
121
//            Feature f = data.findFirst("VCSGISCODE = '"+code+"'");
122
//            assertEquals(id  , f.getInt("id"));
123
//            assertEquals(text, f.getString("text"));
124
//        }
125

  
126
    }
127

  
128
    private VCSGisWorkspace[] initTest() throws Exception {
129

  
130
        final String testid = "ServerUpdateWithConflict";
131
        final URL serverUrl = new URL("http://localhost:" + SERVER_PORT + "/");
132

  
133
        VCSGisManager vcsgismanager = VCSGisLocator.getVCSGisManager();
134
//        JDBCServerExplorer server = TestUtils.openServerExplorer("srv-update");
135

  
136
        JDBCServerExplorerParameters explorerParams = TestUtils.getServerExplorerParameters("srv-" + testid);
137
        File wsfile = TestUtils.getFile(FileUtils.getFile("test-dbs", "ws-" + testid));
138

  
139
        // ---------------------------------------------------------
140
        // Creamos el server y obtenemos el repositorio del server
141
        int r = vcsgismanager.initRepository(explorerParams, null);
142
        assertEquals("srv-init status", 0, r);
143

  
144
        VCSGisRepositoryLocaldb repositoryLocal = (VCSGisRepositoryLocaldb) vcsgismanager.openRepository(explorerParams);
145

  
146
        repositoryLocal.setAllowAssignTheRevisionDate(true);
147

  
148
        TestUtils.h2sql_repository(repositoryLocal);
149

  
150
        repositoryLocal.dispose();
151

  
152
        server = TestUtils.createServer(explorerParams);
153

  
154
        server.start();
155

  
156
        VCSGisRepository repo = vcsgismanager.openRepository(serverUrl);
157

  
158
        File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs", "ws1-update"));
159
        File ws2file = TestUtils.getFile(FileUtils.getFile("test-dbs", "ws2-update"));
160
        File ws3file = TestUtils.getFile(FileUtils.getFile("test-dbs", "ws3-update"));
161

  
162
        vcsgismanager.setCodeGenerator(new SequentialCodeGenerator());
163

  
164
        List<Feature> features;
165
        FeatureStore store2;
166
        FeatureStore store3;
167

  
168
        // ------------------------------------------------------------
169
        // Creamos workspace1 y lo abrimos.
170
        r = vcsgismanager.initWorkspace(ws1file, repo, "Test update1", null);
171
        assertEquals("init_ws1 status", ERR_NO_ERROR, r);
172

  
173
        VCSGisWorkspace ws1 = vcsgismanager.openWorkspace(ws1file);
174
        TestUtils.h2sql_workspace(ws1);
175

  
176
        // ------------------------------------------------------------
177
        // Adicionamos al workspace1 la tabla "test" y la commitamos.
178
        //REV 0
179
        FeatureStore sourceTest = TestUtils.openSourceStore2();
180
        r = ws1.add("test", sourceTest, "text");
181
        DisposeUtils.disposeQuietly(sourceTest);
182
        assertEquals("ws1.add status", ERR_NO_ERROR, r);
183

  
184
        r = ws1.commit();
185
        assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
186

  
187
        // ------------------------------------------------------------
188
        // Inicializamos el workspace2 asociandolo al mismo repositorio que 
189
        // workspace1 y lo abrimos.
190
        r = vcsgismanager.initWorkspace(ws2file, repo, "Test update2", null);
191
        assertEquals("init_ws2 status", ERR_NO_ERROR, r);
192
        VCSGisWorkspace ws2 = vcsgismanager.openWorkspace(ws2file);
193
        TestUtils.h2sql_workspace(ws2);
194

  
195
        // ------------------------------------------------------------
196
        // Actualizamos (update) la tabla "test" en el workspace2 
197
        // y comprobamos que tiene lo que toca.
198
        r = ws2.checkout("test");
199
        assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
200

  
201
        store2 = ws2.openFeatureStore("test",false);
202
        assertEquals("ws2-upfate sizes", 3, store2.size64());
203
        features = store2.getFeatures((Expression) null, "id", true);
204

  
205
        check(features, 0, 1, "AAA");
206
        check(features, 1, 2, "BBB");
207
        check(features, 2, 3, "CCC");
208

  
209
        DisposeUtils.disposeQuietly(features);
210
        DisposeUtils.disposeQuietly(store2);
211

  
212
        // ------------------------------------------------------------
213
        // Inicializamos el workspace3 asociandolo al mismo repositorio que 
214
        // workspace1 y lo abrimos.
215
        r = vcsgismanager.initWorkspace(ws3file, repo, "Test update2", null);
216
        assertEquals("init_ws2 status", ERR_NO_ERROR, r);
217
        VCSGisWorkspace ws3 = vcsgismanager.openWorkspace(ws3file);
218
        TestUtils.h2sql_workspace(ws3);
219

  
220
        // ------------------------------------------------------------
221
        // Actualizamos (update) la tabla "test" en el workspace3
222
        // y comprobamos que tiene lo que toca.
223
        r = ws3.checkout("test");
224
        assertEquals("ws3.update-1 status", ERR_NO_ERROR, r);
225

  
226
        store3 = ws3.openFeatureStore("test",false);
227
        assertEquals("ws3-upfate sizes", 3, store3.size64());
228
        features = store3.getFeatures((Expression) null, "id", true);
229

  
230
        check(features, 0, 1, "AAA");
231
        check(features, 1, 2, "BBB");
232
        check(features, 2, 3, "CCC");
233

  
234
        DisposeUtils.disposeQuietly(features);
235
        DisposeUtils.disposeQuietly(store2);
236
        DisposeUtils.disposeQuietly(store3);
237

  
238
        return new VCSGisWorkspace[]{ws1, ws2, ws3};
239
    }
240

  
241
    public void testTwoUpdatesAndMerge() throws Exception {
242

  
243
        try {
244
            VCSGisWorkspace[] wss = initTest();
245
            VCSGisWorkspace ws1 = wss[0];
246
            VCSGisWorkspace ws2 = wss[1];
247

  
248
            int r;
249
            Feature f;
250
            EditableFeature ef;
251
            FeatureStore store1;
252
            FeatureStore store2;
253

  
254
            // Añadimos al workspace1 una feature con una id concreta y la commitamos
255
            store1 = ws1.openFeatureStore("test",false);
256
            store1.edit();
257
            update(store1, 3, "CC1");
258

  
259
            store1.finishEditing();
260
            DisposeUtils.disposeQuietly(store1);
261

  
262
            r = ws1.commit();
263
            assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
264

  
265
            // Añadimos al workspace2 una feature con la misma id
266
            store2 = ws2.openFeatureStore("test",false);
267
            store2.edit();
268
            update(store2, 3, "CC2");
269

  
270
            store2.finishEditing();
271
            DisposeUtils.disposeQuietly(store2);
272

  
273
            // ------------------------------------------------------------
274
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace2 
275
            // comprobamos que funciona y que tiene lo que toca.
276
            r = ws2.updatePrepare("test");
277
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
278

  
279
//        assertTrue(false);
280
            // Actualizamos (update) la tabla "test" en el workspace2 
281
            // comprobamos que falla
282
            r = ws2.update("test");
283
            assertEquals("ws2.update-1 status", ERR_UPDATE_NEED_MERGE, r);
284

  
285
            // Actualizamos (merge) la tabla "test" en el workspace2 
286
            // comprobamos que no falla
287
            r = ws2.merge("test");
288
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
289

  
290
            //Comprobar que en la tabla de cambios locales hay 1 elemento
291
            FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
292
            assertEquals("ws2-changes size", 1, changes.size64());
293
            DisposeUtils.disposeQuietly(changes);
294
        } finally {
295
            server.stop();
296
        }
297

  
298
    }
299

  
300
    public void testTwoUpdatesMergeAndUpdate() throws Exception {
301
        try {
302
            VCSGisWorkspace[] wss = initTest();
303
            VCSGisWorkspace ws1 = wss[0];
304
            VCSGisWorkspace ws2 = wss[1];
305
            VCSGisWorkspace ws3 = wss[2];
306

  
307
            int r;
308
            Feature f;
309
            EditableFeature ef;
310
            FeatureStore store1;
311
            FeatureStore store2;
312
            FeatureStore store3;
313

  
314
            // Editamos en el workspace1 una feature con una id concreta y la commitamos
315
            store1 = ws1.openFeatureStore("test",false);
316
            store1.edit();
317
            update(store1, 3, "CC1");
318

  
319
            store1.finishEditing();
320
            DisposeUtils.disposeQuietly(store1);
321

  
322
            r = ws1.commit();
323
            assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
324

  
325
            // Editamos en el workspace2 la feature con la misma id
326
            store2 = ws2.openFeatureStore("test",false);
327
            store2.edit();
328
            update(store2, 3, "CC2");
329

  
330
            store2.finishEditing();
331
            DisposeUtils.disposeQuietly(store2);
332

  
333
            // ------------------------------------------------------------
334
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace2 
335
            // comprobamos que funciona y que tiene lo que toca.
336
            r = ws2.updatePrepare("test");
337
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
338

  
339
//        assertTrue(false);
340
            // Actualizamos (update) la tabla "test" en el workspace2 
341
            // comprobamos que falla
342
            r = ws2.update("test");
343
            assertEquals("ws2.update-1 status", ERR_UPDATE_NEED_MERGE, r);
344

  
345
            // Actualizamos (merge) la tabla "test" en el workspace2 
346
            // comprobamos que no falla
347
            r = ws2.merge("test");
348
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
349

  
350
            //Comprobar que en la tabla de cambios locales hay 1 elemento
351
            FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
352
            assertEquals("ws2-changes size", 1, changes.size64());
353
            DisposeUtils.disposeQuietly(changes);
354
            
355
            // Commitamos el workspace2
356
            r = ws2.commit();
357
            assertEquals("ws2.commit-1 status", ERR_NO_ERROR, r);
358

  
359
            // ------------------------------------------------------------
360
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace3
361
            // comprobamos que funciona y que tiene lo que toca.
362
            r = ws3.updatePrepare("test");
363
            assertEquals("ws3.update-1 status", ERR_NO_ERROR, r);
364

  
365
            //Comprobar que en la tabla de cambios remotos hay 1 elemento
366
            FeatureStore remoteChanges = ws3.getFeatureStore(RemoteChangesTable.TABLE_NAME);
367
            assertEquals("ws3-remoteChanges size", 1, remoteChanges.size64());
368
            DisposeUtils.disposeQuietly(remoteChanges);
369
            
370
            // Actualizamos (update) la tabla "test" en el workspace3 
371
            // comprobamos que no falla
372
            r = ws3.update("test");
373
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
374

  
375
            store3 = ws3.openFeatureStore("test",false);
376
            assertEquals("ws3-update sizes", 3, store3.size64());
377
            List<Feature> features = store3.getFeatures((Expression) null, "id", true);
378

  
379
            check(features, 0, 1, "AAA");
380
            check(features, 1, 2, "BBB");
381
            check(features, 2, 3, "CC2");
382

  
383
            DisposeUtils.disposeQuietly(store3);
384
        } finally {
385
            server.stop();
386
        }
387

  
388
    }
389

  
390
    public void testTwoUpdatesAndUpdate() throws Exception {
391
        try {
392

  
393
            VCSGisWorkspace[] wss = initTest();
394
            VCSGisWorkspace ws1 = wss[0];
395
            VCSGisWorkspace ws2 = wss[1];
396

  
397
            int r;
398
            Feature f;
399
            EditableFeature ef;
400
            FeatureStore store1;
401
            FeatureStore store2;
402

  
403
            // Añadimos al workspace1 una feature con una id concreta y la commitamos
404
            store1 = ws1.openFeatureStore("test",false);
405
            store1.edit();
406
            update(store1, 3, "CC1");
407

  
408
            store1.finishEditing();
409
            DisposeUtils.disposeQuietly(store1);
410

  
411
            r = ws1.commit();
412
            assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
413

  
414
            // Añadimos al workspace2 una feature con la misma id
415
            store2 = ws2.openFeatureStore("test",false);
416
            store2.edit();
417
            update(store2, 3, "CC2");
418

  
419
            store2.finishEditing();
420
            DisposeUtils.disposeQuietly(store2);
421

  
422
            // ------------------------------------------------------------
423
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace2 
424
            // comprobamos que funciona y que tiene lo que toca.
425
            r = ws2.updatePrepare("test");
426
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
427

  
428
//        assertTrue(false);
429
            // Actualizamos (update) la tabla "test" en el workspace2 
430
            // comprobamos que falla
431
            r = ws2.update("test");
432
            assertEquals("ws2.update-1 status", ERR_UPDATE_NEED_MERGE, r);
433

  
434
            //Seleccionar el cambio conflictivo id=4
435
            selectRemoteChange(ws2, true, 3, "CC1", ws1);
436

  
437
            // Actualizamos (merge) la tabla "test" en el workspace2 
438
            // comprobamos que no falla
439
            r = ws2.update("test");
440
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
441

  
442
            //Comprobar que en la tabla de cambios locales no hay nada
443
            FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
444
            assertEquals("ws2-changes size", 0, changes.size64());
445
            DisposeUtils.disposeQuietly(changes);
446
            
447
        } finally {
448
            server.stop();
449
        }
450

  
451
    }
452

  
453
    public void testTwoInsertsAndUpdate() throws Exception {
454
        try {
455

  
456
            VCSGisWorkspace[] wss = initTest();
457
            VCSGisWorkspace ws1 = wss[0];
458
            VCSGisWorkspace ws2 = wss[1];
459

  
460
            int r;
461
            Feature f;
462
            EditableFeature ef;
463
            List<Feature> features;
464
            FeatureStore store1;
465
            FeatureStore store2;
466

  
467
            // Añadimos al workspace1 una feature con una id concreta y la commitamos
468
            store1 = ws1.openFeatureStore("test",false);
469
            store1.edit();
470
            insert(store1, 4, "DD1");
471

  
472
            store1.finishEditing();
473
            DisposeUtils.disposeQuietly(store1);
474

  
475
            r = ws1.commit();
476
            assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
477

  
478
            // Añadimos al workspace2 una feature con la misma id
479
            store2 = ws2.openFeatureStore("test",false);
480
            store2.edit();
481
            insert(store2, 4, "DD2");
482

  
483
            store2.finishEditing();
484
            DisposeUtils.disposeQuietly(store2);
485

  
486
            // ------------------------------------------------------------
487
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace2 
488
            // comprobamos que funciona y que tiene lo que toca.
489
            r = ws2.updatePrepare("test");
490
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
491

  
492
            FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
493
            // See in console debug message with 
494
            // "===: UPDATE_PREPARE: conflicts.  {     "id":4,     ..." 
495
            // and use here the value of the field VCSGISCODE
496
            checkLocalChange(store2, changes, "00000000000035abcdef0123456789", OP_INSERT, 4, "DD2", STATE_CONFLICT);
497
            DisposeUtils.disposeQuietly(changes);
498
            
499
            // Actualizamos (update) la tabla "test" en el workspace2 
500
            // comprobamos que no falla
501
            r = ws2.update("test");
502
            assertEquals("ws2.update-1 status", ERR_UPDATE_NEED_MERGE, r);
503

  
504
            //Seleccionar el cambio conflictivo id=4
505
            selectRemoteChange(ws2, true, 4, "DD1", ws1);
506

  
507
            //update y comprobar que funciona y se ha borrado la feature conflictiva nuestra
508
            r = ws2.update("test");
509
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
510

  
511
            store2 = ws2.openFeatureStore("test",false);
512
            assertEquals("ws2-upfate sizes", 4, store2.size64());
513
            features = store2.getFeatures((Expression) null, "id", true);
514

  
515
            check(features, 0, 1, "AAA");
516
            check(features, 1, 2, "BBB");
517
            check(features, 2, 3, "CCC");
518
            check(features, 3, 4, "DD1");
519

  
520
            //Comprobar que en la tabla de cambios locales no hay nada
521
            changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
522
            assertEquals("ws2-changes size", 0, changes.size64());
523
            DisposeUtils.disposeQuietly(changes);
524
        } finally {
525
            server.stop();
526
        }
527

  
528
    }
529

  
530
    public void testTwoInsertsAndMerge() throws Exception {
531
        try {
532

  
533
            VCSGisWorkspace[] wss = initTest();
534
            VCSGisWorkspace ws1 = wss[0];
535
            VCSGisWorkspace ws2 = wss[1];
536

  
537
            int r;
538
            Feature f;
539
            EditableFeature ef;
540
            List<Feature> features;
541
            FeatureStore store1;
542
            FeatureStore store2;
543

  
544
            //Insertamos dos registros nuevos y repetirlo todo sin seleccionar cambio conflictivo y hacer un merge y que funcione
545
            // Añadimos al workspace1 una feature con una id concreta y la commitamos
546
            store1 = ws1.openFeatureStore("test",false);
547
            store1.edit();
548
            insert(store1, 4, "DD1");
549

  
550
            store1.finishEditing();
551
            DisposeUtils.disposeQuietly(store1);
552

  
553
            r = ws1.commit();
554
            assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
555

  
556
            // Añadimos al workspace2 una feature con la misma id
557
            store2 = ws2.openFeatureStore("test",false);
558
            store2.edit();
559
            insert(store2, 4, "DD2");
560

  
561
            store2.finishEditing();
562
            DisposeUtils.disposeQuietly(store2);
563

  
564
            // ------------------------------------------------------------
565
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace2 
566
            // comprobamos que funciona y que tiene lo que toca.
567
            r = ws2.updatePrepare("test");
568
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
569

  
570
            // Actualizamos (update) la tabla "test" en el workspace2 
571
            // comprobamos que falla
572
            r = ws2.update("test");
573
            assertEquals("ws2.update-1 status", ERR_UPDATE_NEED_MERGE, r);
574

  
575
            //merge y comprobar que funciona y no se insertado el cambio conflictivo
576
            r = ws2.merge("test");
577
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
578

  
579
            store2 = ws2.openFeatureStore("test",false);
580
            assertEquals("ws2-upfate sizes", 4, store2.size64());
581
            features = store2.getFeatures((Expression) null, "id", true);
582

  
583
            check(features, 0, 1, "AAA");
584
            check(features, 1, 2, "BBB");
585
            check(features, 2, 3, "CCC");
586
            check(features, 3, 4, "DD2");
587

  
588
            //Comprobamos que se ha añadido a la tabla de cambios local el delete del cambio conflictivo
589
            FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
590
            assertEquals("ws2-changes size", 2, changes.size64());
591

  
592
            // See in console debug message with 
593
            // "===: UPDATE: add delete operation to RelatedFeatureCode ..." 
594
            // and use here the value RelatedFeatureCode
595
            checkLocalChange(store2, changes, "00000000000027abcdef0123456789", OP_DELETE, 4, "DD1", -1);
596

  
597
            // See in console debug message with 
598
            // "===: UPDATE_PREPARE: conflicts.  {     "id":4,     "text":"DD2"" 
599
            // and use here the value of the field VCSGISCODE
600
            checkLocalChange(store2, changes, "00000000000035abcdef0123456789", OP_INSERT, 4, "DD2", -1);
601
            DisposeUtils.disposeQuietly(changes);
602
        } finally {
603
            server.stop();
604
        }
605

  
606
    }
607

  
608
    public void testOneCommitAndUpdateOverDeleted() throws Exception {
609
        try {
610

  
611
            VCSGisWorkspace[] wss = initTest();
612
            VCSGisWorkspace ws1 = wss[0];
613
            VCSGisWorkspace ws2 = wss[1];
614
            List<Feature> features;
615

  
616
            int r;
617
            Feature f;
618
            EditableFeature ef;
619
            FeatureStore store1;
620
            FeatureStore store2;
621

  
622
            // Modificamos en workspace1 una feature con una id concreta y la commitamos
623
            store1 = ws1.openFeatureStore("test",false);
624
            store1.edit();
625
            update(store1, 3, "CC1");
626

  
627
            store1.finishEditing();
628
            DisposeUtils.disposeQuietly(store1);
629

  
630
            r = ws1.commit();
631
            assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
632

  
633
            // Borramos en el workspace2 la feature con la misma id
634
            store2 = ws2.openFeatureStore("test",false);
635
            store2.edit();
636
            delete(store2, 3, "CCC");
637

  
638
            store2.finishEditing();
639
            DisposeUtils.disposeQuietly(store2);
640

  
641
            // ------------------------------------------------------------
642
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace2 
643
            // comprobamos que funciona y que tiene lo que toca.
644
            r = ws2.updatePrepare("test");
645
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
646

  
647
//        assertTrue(false);
648
            // Actualizamos (update) la tabla "test" en el workspace2 
649
            // comprobamos que falla
650
            r = ws2.update("test");
651
            assertEquals("ws2.update-1 status", ERR_UPDATE_NEED_MERGE, r);
652

  
653
//        // Actualizamos (merge) la tabla "test" en el workspace2 
654
//        // comprobamos que no falla
655
//        r = ws2.merge("test");
656
//        assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
657
            //Seleccionar el cambio conflictivo id=4
658
            selectRemoteChange(ws2, true, 3, "CC1", ws1);
659

  
660
            //update y comprobar que funciona y no se ha insertado la feature conflictiva
661
            r = ws2.update("test");
662
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
663

  
664
            store2 = ws2.openFeatureStore("test",false);
665
            assertEquals("ws2-update sizes", 3, store2.size64());
666
            features = store2.getFeatures((Expression) null, "id", true);
667

  
668
            check(features, 0, 1, "AAA");
669
            check(features, 1, 2, "BBB");
670
            check(features, 2, 3, "CC1");
671

  
672
            //Comprobar que en la tabla de cambios locales no hay nada
673
            FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
674
            assertEquals("ws2-changes size", 0, changes.size64());
675
            DisposeUtils.disposeQuietly(changes);
676
        } finally {
677
            server.stop();
678
        }
679

  
680
    }
681

  
682
    public void testOneCommitAndMergeOverDeleted() throws Exception {
683
        try {
684

  
685
            VCSGisWorkspace[] wss = initTest();
686
            VCSGisWorkspace ws1 = wss[0];
687
            VCSGisWorkspace ws2 = wss[1];
688
            List<Feature> features;
689

  
690
            int r;
691
            Feature f;
692
            EditableFeature ef;
693
            FeatureStore store1;
694
            FeatureStore store2;
695

  
696
            // Modificamos en workspace1 una feature con una id concreta y la commitamos
697
            store1 = ws1.openFeatureStore("test",false);
698
            store1.edit();
699
            update(store1, 3, "CC1");
700

  
701
            store1.finishEditing();
702
            DisposeUtils.disposeQuietly(store1);
703

  
704
            r = ws1.commit();
705
            assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
706

  
707
            // Borramos en el workspace2 la feature con la misma id
708
            store2 = ws2.openFeatureStore("test",false);
709
            store2.edit();
710
            delete(store2, 3, "CCC");
711

  
712
            store2.finishEditing();
713
            DisposeUtils.disposeQuietly(store2);
714

  
715
            // ------------------------------------------------------------
716
            // Preparamos la actualización de (updatePrepare) la tabla "test" en el workspace2 
717
            // comprobamos que funciona y que tiene lo que toca.
718
            r = ws2.updatePrepare("test");
719
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
720

  
721
//        assertTrue(false);
722
            // Actualizamos (update) la tabla "test" en el workspace2 
723
            // comprobamos que falla
724
//        r = ws2.update("test");
725
//        assertEquals("ws2.update-1 status", ERR_UPDATE_NEED_MERGE, r);
726
            // Actualizamos (merge) la tabla "test" en el workspace2 
727
            // comprobamos que no falla
728
            r = ws2.merge("test");
729
            assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
730

  
731
            //Comprobamos que en workspace2 hay lo que toca
732
            store2 = ws2.openFeatureStore("test",false);
733
            assertEquals("ws2-update sizes", 2, store2.size64());
734
            features = store2.getFeatures((Expression) null, "id", true);
735

  
736
            check(features, 0, 1, "AAA");
737
            check(features, 1, 2, "BBB");
738
//        check(features, 2, 3, "CC1");
739

  
740
            //Comprobar que en la tabla de cambios locales permanece el cambio
741
            FeatureStore changes = ws2.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
742
            assertEquals("ws2-changes size", 1, changes.size64());
743
            DisposeUtils.disposeQuietly(changes);
744
        } finally {
745
            server.stop();
746
        }
747

  
748
    }
749

  
750
}
org.gvsig.vcsgis/tags/org.gvsig.vcsgis-1.0.36/org.gvsig.vcsgis.app/org.gvsig.vcsgis.app.server/src/test/java/org/gvsig/vcsgis/app/server/TestUtils.java
1
package org.gvsig.vcsgis.app.server;
2

  
3
import java.io.File;
4
import java.io.OutputStreamWriter;
5
import java.net.URL;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import static junit.framework.Assert.assertEquals;
10
import org.apache.commons.io.FileUtils;
11
import org.apache.commons.lang3.StringUtils;
12
import org.eclipse.jetty.server.Server;
13
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceImpl;
14
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb;
15
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
16
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
17
import org.gvsig.fmap.dal.DALLocator;
18
import org.gvsig.fmap.dal.DataManager;
19
import org.gvsig.fmap.dal.DataServerExplorerParameters;
20
import org.gvsig.fmap.dal.DataStore;
21
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
22
import org.gvsig.fmap.dal.exception.DataException;
23
import org.gvsig.fmap.dal.feature.EditableFeature;
24
import org.gvsig.fmap.dal.feature.Feature;
25
import org.gvsig.fmap.dal.feature.FeatureStore;
26
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
27
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
28
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
29
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
30
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff