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

History | View | Annotate | Download (6.1 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.VCSGisLocator;
16
import org.gvsig.vcsgis.lib.VCSGisManager;
17
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_NO_ERROR;
18
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE;
19
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_INSERT;
20
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_UPDATE;
21
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
22
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
23
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange;
24
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity;
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
        VCSGisManager manager = VCSGisLocator.getVCSGisManager();
52

    
53
        JDBCServerExplorer server = TestUtils.openServerExplorer("srv-update");
54
        File ws1file = TestUtils.getFile(FileUtils.getFile("test-dbs","ws1-update")); 
55
        
56
        int r;
57
        Feature f;
58
        EditableFeature ef;
59
        List<Feature> features;
60
        FeatureStore store1;
61
        
62
        // ------------------------------------------------------------
63
        // Inicializamos el repositorio y lo abrimos
64
        r = manager.initRepository(server.getParameters(), null);
65
        assertEquals("init_server status", ERR_NO_ERROR, r);
66
        
67
        VCSGisRepository repo = manager.openRepository(server.getParameters());
68
        TestUtils.h2sql_repository(repo);
69

    
70
        // ------------------------------------------------------------
71
        // Creamos workspace1 y lo abrimos.
72
        r = manager.initWorkspace(ws1file, repo, "Test update1",null);
73
        assertEquals("init_ws1 status", ERR_NO_ERROR, r);
74
        
75
        VCSGisWorkspace ws1 = manager.openWorkspace(ws1file);
76
        TestUtils.h2sql_workspace(ws1);
77

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

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

    
111
        String code3 = f.getString(VCSGisManager.FEATURECODE_FIELD_NAME);
112
        assertNotNull("featureCode 3", code3);
113
        
114

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