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 @ 2728

History | View | Annotate | Download (9.14 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.vcsgis;
22
import org.gvsig.vcsgis.lib.vcsgisadmin;
23
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
public class Test10UpdateLocalModified extends TestCase {
28

    
29
    private static final Logger LOGGER = LoggerFactory.getLogger(Test10UpdateLocalModified.class);
30

    
31
    public Test10UpdateLocalModified(String testName) {
32
        super(testName);
33
    }
34

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

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

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

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

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

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

    
79
        // ------------------------------------------------------------
80
        // Creamos workspace1 y lo abrimos.
81
        r = vcsgis.init_workspace(ws1file, repo, "Ws1: Test update local modified");
82
        assertEquals("init_ws1 status", ERR_NO_ERROR, r);
83
        
84
        VCSGisWorkspace ws1 = vcsgis.open_workspace(ws1file);
85
        TestUtils.h2sql_workspace(ws1);
86

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

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

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

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

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

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

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