Statistics
| Revision:

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 / Test10UpdateLocalModified.java @ 2823

History | View | Annotate | Download (9.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.expressionevaluator.Expression;
9
import org.gvsig.fmap.dal.feature.EditableFeature;
10
import org.gvsig.fmap.dal.feature.Feature;
11
import org.gvsig.fmap.dal.feature.FeatureStore;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
13
import org.gvsig.tools.dispose.DisposeUtils;
14
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
15
import org.gvsig.vcsgis.lib.SequentialCodeGenerator;
16
import org.gvsig.vcsgis.lib.VCSGisLocator;
17
import org.gvsig.vcsgis.lib.VCSGisManager;
18
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_COMMIT_WORKSPACE_OUTDATED;
19
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR;
20
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
21
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

    
25
public class Test10UpdateLocalModified extends TestCase {
26

    
27
    private static final Logger LOGGER = LoggerFactory.getLogger(Test10UpdateLocalModified.class);
28

    
29
    public Test10UpdateLocalModified(String testName) {
30
        super(testName);
31
    }
32

    
33
    @Override
34
    protected void setUp() throws Exception {
35
        super.setUp();
36
        new DefaultLibrariesInitializer().fullInitialize();
37
    }
38

    
39
    @Override
40
    protected void tearDown() throws Exception {
41
        super.tearDown();
42
    }
43

    
44
    private void check(FeatureStore store2, String relatedFeatureCode, int id, String text) throws Exception {
45
        Feature f = store2.findFirst("VCSGISCODE = '"+relatedFeatureCode+"'");
46
        assertEquals(id, f.getInt("id"));
47
        assertEquals(text, f.getString("text"));
48
    }
49

    
50
    // TODO add test methods here. The name must begin with 'test'. For example:
51
    // public void testHello() {}
52

    
53
    public void testUpdateLocalModified() throws Exception {
54
        JDBCServerExplorer server = TestUtils.openServerExplorer("srv-updatelocalmodified");
55
        File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws1-updatelocalmodified")); 
56
        File ws2file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws2-updatelocalmodified")); 
57
        
58
        
59
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
60
        manager.setCodeGenerator(new SequentialCodeGenerator());
61

    
62
        int r;
63
        Feature f;
64
        EditableFeature ef;
65
        List<Feature> features;
66
        FeatureStore store1;
67
        FeatureStore store2;
68
        
69
        // ------------------------------------------------------------
70
        // Inicializamos el repositorio y lo abrimos
71
        r = manager.initRepository(server.getParameters(), null);
72
        assertEquals("init_server status", ERR_NO_ERROR, r);
73
        
74
        VCSGisRepository repo = manager.openRepository(server.getParameters());
75
        TestUtils.h2sql_repository(repo);
76

    
77
        // ------------------------------------------------------------
78
        // Creamos workspace1 y lo abrimos.
79
        r = manager.initWorkspace(ws1file, repo, "Ws1: Test update local modified", null);
80
        assertEquals("init_ws1 status", ERR_NO_ERROR, r);
81
        
82
        VCSGisWorkspace ws1 = manager.openWorkspace(ws1file);
83
        TestUtils.h2sql_workspace(ws1);
84

    
85
        // ------------------------------------------------------------
86
        // Adicionamos al workspace1 la tabla "test" y la commitamos.
87
        FeatureStore sourceTest = TestUtils.openSourceStore2();
88
        r = ws1.add("test", sourceTest, "text");
89
        assertEquals("ws1.add status", ERR_NO_ERROR, r);
90
        r = ws1.commit();
91
        assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
92
        DisposeUtils.disposeQuietly(sourceTest);
93

    
94
        
95
        // ------------------------------------------------------------
96
        // Inicializamos el workspace2 asociandolo al mismo repositorio que 
97
        // workspace1 y lo abrimos.
98
        r = manager.initWorkspace(ws2file, repo, "Ws2: Test update local modified", null);
99
        assertEquals("init_ws2 status", ERR_NO_ERROR, r);
100
        VCSGisWorkspace ws2 = manager.openWorkspace(ws2file);
101
        TestUtils.h2sql_workspace(ws2);
102
        
103
        // ------------------------------------------------------------
104
        // Hacemos un checkout en workspace2 de la tabla "test" 
105
        // y comprobamos que tiene lo que toca.
106
        r = ws2.checkout("test");
107
        assertEquals("ws2-checkout status", ERR_NO_ERROR, r);
108

    
109
        store2 = ws2.getFeatureStore("test");
110
        assertEquals("ws2-checkout sizes", 3, store2.size64());
111
        features = store2.getFeatures((Expression)null, "id", true);
112
        assertEquals(1    , features.get(0).getInt("id"));
113
        assertEquals("AAA", features.get(0).getString("text"));
114
        assertEquals(2    , features.get(1).getInt("id"));
115
        assertEquals("BBB", features.get(1).getString("text"));
116
        assertEquals(3    , features.get(2).getInt("id"));
117
        assertEquals("CCC", features.get(2).getString("text"));
118
        
119
        DisposeUtils.disposeQuietly(store2);
120

    
121
        // ------------------------------------------------------------
122
        // Modificamos la tabla "test" en el workspace1 y la commitamos.
123
        store1 = ws1.getFeatureStore("test");
124
        store1.edit();
125
        f = store1.findFirst("id=2");
126
        ef = f.getEditable();
127
        ef.set("text", "BB2");
128
        store1.update(ef);
129
        
130
        ef = store1.createNewFeature();
131
        ef.set("id",4);
132
        ef.set("text","DDD");
133
        store1.insert(ef);
134
        store1.finishEditing();
135
        DisposeUtils.disposeQuietly(store1);
136
        r = ws1.commit();
137
        assertEquals("ws1.commit-2 status", ERR_NO_ERROR, r);
138
        
139
        // ------------------------------------------------------------
140
        // Modificamos la tabla "test" en el workspace2 
141
        // estando desactualizada. 
142
        // Una actualizacion, una insercion y un borrado.
143
        // No commitamos los cambios.
144
        store2 = ws2.getFeatureStore("test");
145
        store2.edit();
146
        f = store2.findFirst("id=1");
147
        ef = f.getEditable();
148
        ef.set("text", "AA2");
149
        store2.update(ef);
150
        
151
        ef = store2.createNewFeature();
152
        ef.set("id",5);
153
        ef.set("text","EEE");
154
        store2.insert(ef);
155

    
156
        f = store2.findFirst("id=3");
157
        store2.delete(f);        
158
        
159
        store2.finishEditing();
160
        DisposeUtils.disposeQuietly(store2);
161
        
162
        // ------------------------------------------------------------
163
        // Commitamos la tabla "test" en el workspace2,
164
        // tiene que fallar por estar desactualizada.
165
        r = ws2.commit();
166
        assertEquals("ws2.commit-1 status", ERR_CANT_COMMIT_WORKSPACE_OUTDATED, r);
167
        
168
        
169
        // ------------------------------------------------------------
170
        // Actualizamos (update) la tabla "test" en el workspace2 
171
        // y comprobamos que tiene lo que toca.
172
        r = ws2.updatePrepare("test");
173
        assertEquals("ws2.update-2 status", ERR_NO_ERROR, r);
174
        r = ws2.update("test");
175
        assertEquals("ws2.update-1 status", ERR_NO_ERROR, r);
176
        store2 = ws2.getFeatureStore("test");
177
        assertEquals("ws2-upfate sizes", 4, store2.size64());
178
        features = store2.getFeatures((Expression)null, "id", true);
179
        assertEquals(1    , features.get(0).getInt("id"));
180
        assertEquals("AA2", features.get(0).getString("text"));
181
        assertEquals(2    , features.get(1).getInt("id"));
182
        assertEquals("BB2", features.get(1).getString("text"));
183
        assertEquals(4    , features.get(2).getInt("id"));
184
        assertEquals("DDD", features.get(2).getString("text"));
185
        assertEquals(5    , features.get(3).getInt("id"));
186
        assertEquals("EEE", features.get(3).getString("text"));
187
        DisposeUtils.disposeQuietly(store2);
188

    
189
        // ------------------------------------------------------------
190
        // Commitamos la tabla "test" en el workspace2.
191
        // Se supone que estamos actualizados y podemos commitar.
192
        r = ws2.commit();
193
        assertEquals("ws2.commit-1 status", ERR_NO_ERROR, r);
194

    
195
        // ------------------------------------------------------------
196
        // Actualizamos (update) la tabla "test" en el workspace1
197
        // y comprobamos que tiene lo que toca.
198
        r = ws1.updatePrepare("test");
199
        assertEquals("ws2.update-2 status", ERR_NO_ERROR, r);
200
        r = ws1.update("test");
201
        assertEquals("ws1.update-1 status", ERR_NO_ERROR, r);
202
        store1 = ws1.getFeatureStore("test");
203
        assertEquals("ws1-upfate sizes", 4, store1.size64());
204
        features = store1.getFeatures((Expression)null, "id", true);
205
        assertEquals(1    , features.get(0).getInt("id"));
206
        assertEquals("AA2", features.get(0).getString("text"));
207
        assertEquals(2    , features.get(1).getInt("id"));
208
        assertEquals("BB2", features.get(1).getString("text"));
209
        assertEquals(4    , features.get(2).getInt("id"));
210
        assertEquals("DDD", features.get(2).getString("text"));
211
        assertEquals(5    , features.get(3).getInt("id"));
212
        assertEquals("EEE", features.get(3).getString("text"));
213
        DisposeUtils.disposeQuietly(store1);
214
    }
215
}