Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / fmap / dal / feature / impl / DefaultFeatureReferenceSelectionTest.java @ 40435

History | View | Annotate | Download (7.36 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {{Task}}
26
 */
27
package org.gvsig.fmap.dal.feature.impl;
28

    
29
import java.util.Iterator;
30

    
31
import junit.framework.TestCase;
32

    
33
import org.easymock.MockControl;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.FeatureReference;
36
import org.gvsig.fmap.dal.feature.FeatureReferenceSelection;
37
import org.gvsig.fmap.dal.feature.FeatureSet;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.tools.exception.BaseException;
40
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
41
import org.gvsig.tools.visitor.Visitor;
42

    
43
/**
44
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
45
 */
46
public class DefaultFeatureReferenceSelectionTest extends AbstractLibraryAutoInitTestCase {
47

    
48
    private FeatureReferenceSelection selection;
49
    private MockControl refControl1;
50
    private FeatureReference ref1;
51
    private MockControl refControl2;
52
    private FeatureReference ref2;
53
    private MockControl storeControl;
54
    private FeatureStore store;
55
    private MockControl fsetControl;
56
    private FeatureSet featureSet;
57
    private MockControl helperControl;
58
    private FeatureSelectionHelper helper;
59

    
60
    private int total = 10;
61

    
62
    protected void doSetUp() throws Exception {     
63
        refControl1 = MockControl.createNiceControl(FeatureReference.class);
64
        ref1 = (FeatureReference) refControl1.getMock();
65
        refControl2 = MockControl.createNiceControl(FeatureReference.class);
66
        ref2 = (FeatureReference) refControl2.getMock();
67

    
68
        storeControl = MockControl.createNiceControl(FeatureStore.class);
69
        store = (FeatureStore) storeControl.getMock();
70
        storeControl.expectAndReturn(store.getFeatureCount(), total);
71

    
72
        helperControl = MockControl
73
                .createNiceControl(FeatureSelectionHelper.class);
74
        helper = (FeatureSelectionHelper) helperControl.getMock();
75
        helperControl.expectAndDefaultReturn(helper.getFeatureStoreDeltaSize(),
76
                0);
77

    
78
        fsetControl = MockControl.createNiceControl(FeatureSet.class);
79
        featureSet = (FeatureSet) fsetControl.getMock();
80

    
81
        storeControl.expectAndReturn(store.getFeatureSet(), featureSet);
82
        fsetControl.expectAndReturn(featureSet.getSize(), total);
83

    
84

    
85

    
86
        storeControl.replay();
87
        fsetControl.replay();
88
        helperControl.replay();
89

    
90
        selection = new DefaultFeatureReferenceSelection(store, helper);
91
    }
92

    
93
    protected void tearDown() throws Exception {
94
        super.tearDown();
95
    }
96

    
97
    public void testSelect() throws Exception {
98
        // Add a feature
99
        selection.select(ref1);
100
        assertTrue(selection.isSelected(ref1));
101
        assertFalse(selection.isSelected(ref2));
102

    
103
        selection.select(ref2);
104
        assertTrue(selection.isSelected(ref2));
105
    }
106

    
107
    public void testSelectAll() throws Exception {
108
        selection.selectAll();
109
        assertTrue(selection.isSelected(ref1));
110
        assertTrue(selection.isSelected(ref2));
111
    }
112

    
113
    public void testDeselect() throws Exception {
114
        // Remove a feature
115
        selection.select(ref1);
116
        selection.select(ref2);
117
        assertTrue(selection.isSelected(ref2));
118
        selection.deselect(ref2);
119
        assertFalse(selection.isSelected(ref2));
120
        selection.deselect(ref1);
121
        assertFalse(selection.isSelected(ref1));
122
    }
123

    
124
    public void testDeselectAll() throws Exception {
125
        selection.select(ref1);
126
        selection.select(ref2);
127
        selection.deselectAll();
128
        assertFalse(selection.isSelected(ref1));
129
        assertFalse(selection.isSelected(ref2));
130
    }
131

    
132
    public void testReverse() throws Exception {
133
        // Reverse selection
134
        selection.select(ref1);
135
        selection.reverse();
136
        assertFalse(selection.isSelected(ref1));
137
        assertTrue(selection.isSelected(ref2));
138
    }
139

    
140
    public void testGetSelectedCount() throws Exception {
141
        // None selected
142
        assertEquals(0, selection.getSelectedCount());
143
        selection.select(ref1);
144
        selection.select(ref2);
145
        // Two selected
146
        assertEquals(2, selection.getSelectedCount());
147
        // Deselect one, so one left selected
148
        selection.deselect(ref2);
149
        assertEquals(1, selection.getSelectedCount());
150
        // Reverse the selection, so all buy one selected
151
        selection.reverse();
152
        assertEquals(total - 1, selection.getSelectedCount());
153
        // Deselect another one, so all but two selected
154
        selection.deselect(ref2);
155
        assertEquals(total - 2, selection.getSelectedCount());
156
        // Deselect an already deselected one, nothing changes
157
        selection.deselect(ref2);
158
        assertEquals(total - 2, selection.getSelectedCount());
159
    }
160

    
161
    public void testGetSelectedValues() throws DataException {
162
        selection.deselectAll();
163
        selection.select(ref1);
164
        selection.select(ref2);
165

    
166
        boolean ref1Pending = true;
167
        boolean ref2Pending = true;
168

    
169
        for (Iterator references = selection.referenceIterator(); references
170
                .hasNext();) {
171
            FeatureReference reference = (FeatureReference) references.next();
172
            if (reference.equals(ref1)) {
173
                if (ref1Pending) {
174
                    ref1Pending = false;
175
                } else {
176
                    fail("FeatureReference 1 was already obtained");
177
                }
178
            } else if (reference.equals(ref2)) {
179
                if (ref2Pending) {
180
                    ref2Pending = false;
181
                } else {
182
                    fail("FeatureReference 2 was already obtained");
183
                }
184
            } else {
185
                fail("A reference not selected was obtained: " + reference);
186
            }
187
        }
188
    }
189

    
190
    public void testDispose() {
191
        selection.select(ref1);
192
        selection.select(ref2);
193
        selection.dispose();
194
        assertFalse(selection.isSelected(ref1));
195
        assertFalse(selection.isSelected(ref2));
196
    }
197

    
198
    public void testAccept() throws BaseException {
199
        // Create a Mopck Visitor and add expected usage
200
        MockControl visitorControl = MockControl
201
                .createNiceControl(Visitor.class);
202
        Visitor visitor = (Visitor) visitorControl.getMock();
203

    
204
        visitor.visit(ref1);
205
        visitor.visit(ref2);
206
        visitorControl.replay();
207

    
208
        // Add selected references
209
        selection.select(ref1);
210
        selection.select(ref2);
211

    
212
        // Use visitor
213
        selection.accept(visitor);
214

    
215
        // check correct visitor usage
216
        visitorControl.verify();
217
    }
218
}