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 / Test11GetLocalChanges.java @ 2697

History | View | Annotate | Download (6.05 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.fmap.dal.feature.EditableFeature;
9
import org.gvsig.fmap.dal.feature.Feature;
10
import org.gvsig.fmap.dal.feature.FeatureStore;
11
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
12
import org.gvsig.tools.dispose.DisposeUtils;
13
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
14
import org.gvsig.tools.util.GetItemWithSize64;
15
import org.gvsig.vcsgis.lib.client.VCSGisClientManager;
16
import static org.gvsig.vcsgis.lib.client.VCSGisClientManager.OP_DELETE;
17
import static org.gvsig.vcsgis.lib.client.VCSGisClientManager.OP_INSERT;
18
import static org.gvsig.vcsgis.lib.client.VCSGisClientManager.OP_UPDATE;
19
import org.gvsig.vcsgis.lib.client.VCSGisWorkspace;
20
import org.gvsig.vcsgis.lib.client.VCSGisWorkspaceEntity;
21
import static org.gvsig.vcsgis.lib.client.VCSGisWorkspace.ERR_NO_ERROR;
22
import org.gvsig.vcsgis.lib.vcsgis;
23
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
24
import org.gvsig.vcsgis.lib.vcsgisadmin;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

    
28
public class Test11GetLocalChanges extends TestCase {
29

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

    
32
    public Test11GetLocalChanges(String testName) {
33
        super(testName);
34
    }
35

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

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

    
47
    // TODO add test methods here. The name must begin with 'test'. For example:
48
    // public void testHello() {}
49

    
50
    public void testUpdate() throws Exception {
51
        JDBCServerExplorer server = TestUtils.openServerExplorer("srv-update");
52
        File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws1-update")); 
53
        
54
        int r;
55
        Feature f;
56
        EditableFeature ef;
57
        List<Feature> features;
58
        FeatureStore store1;
59
        
60
        // ------------------------------------------------------------
61
        // Inicializamos el repositorio y lo abrimos
62
        r = vcsgisadmin.init_repository(server);
63
        assertEquals("init_server status", ERR_NO_ERROR, r);
64
        
65
        VCSGisRepository repo = vcsgisadmin.open_repository(server);
66
        TestUtils.h2sql_repository(repo);
67

    
68
        // ------------------------------------------------------------
69
        // Creamos workspace1 y lo abrimos.
70
        r = vcsgis.init_workspace(ws1file, repo, "Test update1");
71
        assertEquals("init_ws1 status", ERR_NO_ERROR, r);
72
        
73
        VCSGisWorkspace ws1 = vcsgis.open_workspace(ws1file);
74
        TestUtils.h2sql_workspace(ws1);
75

    
76
        // ------------------------------------------------------------
77
        // A?adimos al workspace1 la tabla "test" y la commitamos.
78
        FeatureStore sourceTest = TestUtils.openSourceStore2();
79
        r = ws1.add("test", sourceTest, "text");
80
        assertEquals("ws1.add status", ERR_NO_ERROR, r);
81
        r = ws1.commit();
82
        assertEquals("ws1.commit-1 status", ERR_NO_ERROR, r);
83
        DisposeUtils.disposeQuietly(sourceTest);
84

    
85
        
86
        // ------------------------------------------------------------
87
        // Modificamos la tabla "test" en el workspace1 y no la commitamos.
88
        store1 = ws1.getFeatureStore("test");
89
        store1.edit();
90
        f = store1.findFirst("id=2");
91
        ef = f.getEditable();
92
        ef.set("text", "BB2");
93
        store1.update(ef);
94
        
95
        String code1 = ef.getString(VCSGisClientManager.FEATURECODE_FIELD_NAME);
96
        assertNotNull("featureCode 1", code1);
97
        
98
        ef = store1.createNewFeature();
99
        ef.set("id",4);
100
        ef.set("text","DDD");
101
        store1.insert(ef);
102
        
103
        String code2 = ef.getString(VCSGisClientManager.FEATURECODE_FIELD_NAME);
104
        assertNotNull("featureCode 2", code2);
105
        
106
        f = store1.findFirst("id=3");
107
        store1.delete(f);        
108

    
109
        String code3 = f.getString(VCSGisClientManager.FEATURECODE_FIELD_NAME);
110
        assertNotNull("featureCode 3", code3);
111
        
112

    
113
        store1.finishEditing();
114
        DisposeUtils.disposeQuietly(store1);
115
        
116
        GetItemWithSize64<VCSGisWorkspace.LocalChange> localChanges = ws1.getLocalChangesByEntityName("test");
117
        
118
        assertEquals("localChangesSize", 3L, localChanges.size64());
119
        VCSGisWorkspaceEntity entity = ws1.getWorkspaceEntityByName("test");
120
        
121
        for(long i = 0; i<localChanges.size64(); i++){
122
            VCSGisWorkspace.LocalChange change = localChanges.get64(i);
123
            assertEquals(entity.getEntityCode(), change.getEntityCode());
124
            
125
            if(change.getRelatedFeatureCode().equalsIgnoreCase(code1)){
126
                assertEquals(OP_UPDATE, change.getOperation());
127
                assertEquals("BB2", change.getLabel());
128
                f = change.getRelatedFeature();
129
                assertEquals(2, f.getInt("id"));
130
                assertEquals("BB2", f.getString("text"));
131
                
132
                
133
            } else if(change.getRelatedFeatureCode().equalsIgnoreCase(code2)){
134
                assertEquals(OP_INSERT, change.getOperation());
135
                assertEquals("DDD", change.getLabel());
136
                f = change.getRelatedFeature();
137
                assertEquals(4, f.getInt("id"));
138
                assertEquals("DDD", f.getString("text"));
139
                
140
            } else if(change.getRelatedFeatureCode().equalsIgnoreCase(code3)){
141
                assertEquals(OP_DELETE, change.getOperation());
142
                assertEquals("CCC", change.getLabel());
143
                boolean ok = true;
144
                try {
145
                    change.getRelatedFeature();
146
                    ok = false;
147
                } catch (Exception e) {
148
                }
149
                assertTrue("code3 deleted", ok);
150
                
151
            } else {
152
                assertTrue("Incorrect code.", false);
153
            }
154
        }
155
        
156
    }
157
}