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

History | View | Annotate | Download (7.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl;
25

    
26
import java.util.Iterator;
27

    
28
import org.easymock.MockControl;
29

    
30
import org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.feature.FeatureReference;
32
import org.gvsig.fmap.dal.feature.FeatureReferenceSelection;
33
import org.gvsig.fmap.dal.feature.FeatureSet;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.tools.exception.BaseException;
36
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
37
import org.gvsig.tools.visitor.Visitor;
38

    
39
/**
40
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
41
 */
42
public class DefaultFeatureReferenceSelectionTest extends AbstractLibraryAutoInitTestCase {
43

    
44
    private FeatureReferenceSelection selection;
45
    private MockControl refControl1;
46
    private FeatureReference ref1;
47
    private MockControl refControl2;
48
    private FeatureReference ref2;
49
    private MockControl storeControl;
50
    private FeatureStore store;
51
    private MockControl fsetControl;
52
    private FeatureSet featureSet;
53
    private MockControl helperControl;
54
    private FeatureSelectionHelper helper;
55

    
56
    private int total = 10;
57

    
58
    protected void doSetUp() throws Exception {
59
        refControl1 = MockControl.createNiceControl(FeatureReference.class);
60
        ref1 = (FeatureReference) refControl1.getMock();
61
        refControl2 = MockControl.createNiceControl(FeatureReference.class);
62
        ref2 = (FeatureReference) refControl2.getMock();
63

    
64
        storeControl = MockControl.createNiceControl(FeatureStore.class);
65
        store = (FeatureStore) storeControl.getMock();
66
        storeControl.expectAndReturn(store.getFeatureCount(), total);
67

    
68
        helperControl = MockControl
69
                .createNiceControl(FeatureSelectionHelper.class);
70
        helper = (FeatureSelectionHelper) helperControl.getMock();
71
        helperControl.expectAndDefaultReturn(helper.getFeatureStoreDeltaSize(),
72
                0);
73

    
74
        fsetControl = MockControl.createNiceControl(FeatureSet.class);
75
        featureSet = (FeatureSet) fsetControl.getMock();
76

    
77
        storeControl.expectAndReturn(store.getFeatureSet(), featureSet);
78
        fsetControl.expectAndReturn(featureSet.getSize(), total);
79

    
80

    
81

    
82
        storeControl.replay();
83
        fsetControl.replay();
84
        helperControl.replay();
85

    
86
        selection = new DefaultFeatureReferenceSelection(store, helper);
87
    }
88

    
89
    protected void tearDown() throws Exception {
90
        super.tearDown();
91
    }
92

    
93
    public void testSelect() throws Exception {
94
        // Add a feature
95
        selection.select(ref1);
96
        assertTrue(selection.isSelected(ref1));
97
        assertFalse(selection.isSelected(ref2));
98

    
99
        selection.select(ref2);
100
        assertTrue(selection.isSelected(ref2));
101
    }
102

    
103
    public void testSelectAll() throws Exception {
104
        selection.selectAll();
105
        assertTrue(selection.isSelected(ref1));
106
        assertTrue(selection.isSelected(ref2));
107
    }
108

    
109
    public void testDeselect() throws Exception {
110
        // Remove a feature
111
        selection.select(ref1);
112
        selection.select(ref2);
113
        assertTrue(selection.isSelected(ref2));
114
        selection.deselect(ref2);
115
        assertFalse(selection.isSelected(ref2));
116
        selection.deselect(ref1);
117
        assertFalse(selection.isSelected(ref1));
118
    }
119

    
120
    public void testDeselectAll() throws Exception {
121
        selection.select(ref1);
122
        selection.select(ref2);
123
        selection.deselectAll();
124
        assertFalse(selection.isSelected(ref1));
125
        assertFalse(selection.isSelected(ref2));
126
    }
127

    
128
    public void testReverse() throws Exception {
129
        // Reverse selection
130
        selection.select(ref1);
131
        selection.reverse();
132
        assertFalse(selection.isSelected(ref1));
133
        assertTrue(selection.isSelected(ref2));
134
    }
135

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

    
157
    public void testGetSelectedValues() throws DataException {
158
        selection.deselectAll();
159
        selection.select(ref1);
160
        selection.select(ref2);
161

    
162
        boolean ref1Pending = true;
163
        boolean ref2Pending = true;
164

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

    
186
    public void testDispose() {
187
        selection.select(ref1);
188
        selection.select(ref2);
189
        selection.dispose();
190
        assertFalse(selection.isSelected(ref1));
191
        assertFalse(selection.isSelected(ref2));
192
    }
193

    
194
    public void testAccept() throws BaseException {
195
        // Create a Mopck Visitor and add expected usage
196
        MockControl visitorControl = MockControl
197
                .createNiceControl(Visitor.class);
198
        Visitor visitor = (Visitor) visitorControl.getMock();
199

    
200
        visitor.visit(ref1);
201
        visitor.visit(ref2);
202
        visitorControl.replay();
203

    
204
        // Add selected references
205
        selection.select(ref1);
206
        selection.select(ref2);
207

    
208
        // Use visitor
209
        selection.accept(visitor);
210

    
211
        // check correct visitor usage
212
        visitorControl.verify();
213
    }
214
}