Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src-test / org / gvsig / fmap / dal / feature / impl / DefaultFeatureSelectionTest.java @ 31544

History | View | Annotate | Download (7.26 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 junit.framework.TestCase;
30

    
31
import org.easymock.MockControl;
32
import org.gvsig.fmap.dal.feature.Feature;
33
import org.gvsig.fmap.dal.feature.FeatureReference;
34
import org.gvsig.fmap.dal.feature.FeatureSelection;
35
import org.gvsig.fmap.dal.feature.FeatureSet;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38

    
39
/**
40
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
41
 */
42
public class DefaultFeatureSelectionTest extends TestCase {
43
    private FeatureSelection selection;
44
    private MockControl refControl1;
45
    private FeatureReference ref1;
46
    private MockControl refControl2;
47
    private FeatureReference ref2;
48
    private MockControl fControl1;
49
    private Feature feature1;
50
    private MockControl fControl2;
51
    private Feature feature2;
52
    private MockControl storeControl;
53
    private FeatureType featureType;
54
    private MockControl fTypeControl;
55
    private FeatureStore store;
56
    private MockControl fsetControl;
57
    private FeatureSet featureSet;
58
    private MockControl helperControl;
59
    private FeatureSelectionHelper helper;
60

    
61
    private int total = 10;
62

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

    
70
        fControl1 = MockControl.createControl(Feature.class);
71
        feature1 = (Feature) fControl1.getMock();
72
        fControl2 = MockControl.createControl(Feature.class);
73
        feature2 = (Feature) fControl2.getMock();
74

    
75
        fTypeControl = MockControl.createNiceControl(FeatureType.class);
76
        featureType = (FeatureType) fTypeControl.getMock();
77

    
78
        fControl1.expectAndDefaultReturn(feature1.getType(), featureType);
79
        fControl2.expectAndDefaultReturn(feature2.getType(), featureType);
80

    
81
        storeControl = MockControl.createNiceControl(FeatureStore.class);
82
        store = (FeatureStore) storeControl.getMock();
83
        storeControl.expectAndReturn(store.getFeatureCount(), total);
84

    
85
        helperControl = MockControl
86
                .createNiceControl(FeatureSelectionHelper.class);
87
        helper = (FeatureSelectionHelper) helperControl.getMock();
88
        helperControl.expectAndDefaultReturn(helper.getFeatureStoreDeltaSize(),
89
                0);
90

    
91
        fsetControl = MockControl.createNiceControl(FeatureSet.class);
92
        featureSet = (FeatureSet) fsetControl.getMock();
93

    
94
        storeControl.expectAndReturn(store.getFeatureSet(), featureSet);
95
        fsetControl.expectAndReturn(featureSet.getSize(), total);
96

    
97
        storeControl.replay();
98
        fsetControl.replay();
99
        helperControl.replay();
100

    
101
        selection = new DefaultFeatureSelection(store, helper);
102
    }
103

    
104
    protected void tearDown() throws Exception {
105
        super.tearDown();
106
    }
107

    
108
    /**
109
     * Test method for
110
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#select(org.gvsig.fmap.dal.feature.Feature)}
111
     * .
112
     */
113
    public void testSelectFeature() {
114
        fControl1.expectAndReturn(feature1.getReference(), ref1, 2);
115
        fControl2.expectAndReturn(feature2.getReference(), ref2, 3);
116
        fControl1.replay();
117
        fControl2.replay();
118

    
119
        // Add a feature
120
        selection.select(feature1);
121
        assertTrue("Selected feature1 is not recognized as selected", selection
122
                .isSelected(feature1));
123
        assertFalse("Selected feature2 is recognized as selected", selection
124
                .isSelected(feature2));
125

    
126
        selection.select(feature2);
127
        assertTrue(selection.isSelected(feature2));
128
    }
129

    
130
    /**
131
     * Test method for
132
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#deselect(org.gvsig.fmap.dal.feature.Feature)}
133
     * .
134
     */
135
    public void testDeselectFeature() {
136
        fControl1.expectAndReturn(feature1.getReference(), ref1, 3);
137
        fControl2.expectAndReturn(feature2.getReference(), ref2, 4);
138
        fControl1.replay();
139
        fControl2.replay();
140

    
141
        // Remove a feature
142
        selection.select(feature1);
143
        selection.select(feature2);
144
        assertTrue(selection.isSelected(feature2));
145
        selection.deselect(feature2);
146
        assertFalse(selection.isSelected(feature2));
147
        selection.deselect(feature1);
148
        assertFalse(selection.isSelected(feature1));
149
    }
150

    
151
    /**
152
     * Test method for
153
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#getSize()}
154
     * .
155
     */
156
    public void testGetSize() throws Exception {
157
        fControl1.expectAndReturn(feature1.getReference(), ref1, 1);
158
        fControl2.expectAndReturn(feature2.getReference(), ref2, 4);
159
        fControl1.replay();
160
        fControl2.replay();
161

    
162
        // None selected
163
        assertEquals(0, selection.getSize());
164
        selection.select(feature1);
165
        selection.select(feature2);
166
        // Two selected
167
        assertEquals(2, selection.getSize());
168
        // Deselect one, so one left selected
169
        selection.deselect(feature2);
170
        assertEquals(1, selection.getSize());
171
        // Reverse the selection, so all buy one selected
172
        selection.reverse();
173
        assertEquals(total - 1, selection.getSize());
174
        // Deselect another one, so all but two selected
175
        selection.deselect(feature2);
176
        assertEquals(total - 2, selection.getSize());
177
        // Deselect an already deselected one, nothing changes
178
        selection.deselect(feature2);
179
        assertEquals(total - 2, selection.getSize());
180
    }
181

    
182
    /**
183
     * Test method for
184
     * {@link org.gvsig.fmap.dal.feature.impl.DefaultFeatureSelection#isEmpty()}
185
     * .
186
     */
187
    public void testIsEmpty() throws Exception {
188
        fControl1.expectAndReturn(feature1.getReference(), ref1, 1);
189
        fControl2.expectAndReturn(feature2.getReference(), ref2, 4);
190
        fControl1.replay();
191
        fControl2.replay();
192

    
193
        // None selected
194
        assertTrue(selection.isEmpty());
195

    
196
        // One selected
197
        selection.select(feature1);
198
        assertFalse(selection.isEmpty());
199

    
200
        // Deselect all
201
        selection.deselectAll();
202
        assertTrue(selection.isEmpty());
203

    
204
        // Reverse
205
        selection.reverse();
206
        assertFalse(selection.isEmpty());
207
    }
208

    
209
}