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

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

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

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

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

    
57
    private int total = 10;
58

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

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

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

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

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

    
81

    
82

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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