Statistics
| Revision:

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

History | View | Annotate | Download (6.95 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (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.exceptions.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.impl.DefaultFeatureReferenceSelection;
39
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
40
import org.gvsig.tools.exception.BaseException;
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 TestCase {
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 DefaultFeatureStore store;
55
    private MockControl fsetControl;
56
    private FeatureSet featureSet;
57

    
58
    private int total = 10;
59

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

    
67
        storeControl = MockControl.createNiceControl(DefaultFeatureStore.class);
68
        store = (DefaultFeatureStore) storeControl.getMock();
69

    
70
        fsetControl = MockControl.createNiceControl(FeatureSet.class);
71
        featureSet = (FeatureSet) fsetControl.getMock();
72

    
73
        storeControl.expectAndReturn(store.getFeatureSet(), featureSet);
74
        fsetControl.expectAndReturn(featureSet.getSize(), total);
75

    
76
        storeControl.replay();
77
        fsetControl.replay();
78

    
79
        selection = new DefaultFeatureReferenceSelection(store);
80
    }
81

    
82
    protected void tearDown() throws Exception {
83
        super.tearDown();
84
    }
85

    
86
    public void testSelect() throws Exception {
87
        // Add a feature
88
        selection.select(ref1);
89
        assertTrue(selection.isSelected(ref1));
90
        assertFalse(selection.isSelected(ref2));
91

    
92
        selection.select(ref2);
93
        assertTrue(selection.isSelected(ref2));
94
    }
95

    
96
    public void testSelectAll() throws Exception {
97
        selection.selectAll();
98
        assertTrue(selection.isSelected(ref1));
99
        assertTrue(selection.isSelected(ref2));
100
    }
101

    
102
    public void testDeselect() throws Exception {
103
        // Remove a feature
104
        selection.select(ref1);
105
        selection.select(ref2);
106
        assertTrue(selection.isSelected(ref2));
107
        selection.deselect(ref2);
108
        assertFalse(selection.isSelected(ref2));
109
        selection.deselect(ref1);
110
        assertFalse(selection.isSelected(ref1));
111
    }
112

    
113
    public void testDeselectAll() throws Exception {
114
        selection.select(ref1);
115
        selection.select(ref2);
116
        selection.deselectAll();
117
        assertFalse(selection.isSelected(ref1));
118
        assertFalse(selection.isSelected(ref2));
119
    }
120

    
121
    public void testReverse() throws Exception {
122
        // Reverse selection
123
        selection.select(ref1);
124
        selection.reverse();
125
        assertFalse(selection.isSelected(ref1));
126
        assertTrue(selection.isSelected(ref2));
127
    }
128

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

    
150
    public void testGetSelectedValues() throws DataException {
151
        selection.deselectAll();
152
        selection.select(ref1);
153
        selection.select(ref2);
154

    
155
        boolean ref1Pending = true;
156
        boolean ref2Pending = true;
157

    
158
        for (Iterator references = selection.referenceIterator(); references
159
                .hasNext();) {
160
            FeatureReference reference = (FeatureReference) references.next();
161
            if (reference.equals(ref1)) {
162
                if (ref1Pending) {
163
                    ref1Pending = false;
164
                } else {
165
                    fail("FeatureReference 1 was already obtained");
166
                }
167
            } else if (reference.equals(ref2)) {
168
                if (ref2Pending) {
169
                    ref2Pending = false;
170
                } else {
171
                    fail("FeatureReference 2 was already obtained");
172
                }
173
            } else {
174
                fail("A reference not selected was obtained: " + reference);
175
            }
176
        }
177
    }
178

    
179
    public void testDispose() {
180
        selection.select(ref1);
181
        selection.select(ref2);
182
        selection.dispose();
183
        assertFalse(selection.isSelected(ref1));
184
        assertFalse(selection.isSelected(ref2));
185
    }
186

    
187
    public void testAccept() throws BaseException {
188
        // Create a Mopck Visitor and add expected usage
189
        MockControl visitorControl = MockControl
190
                .createNiceControl(Visitor.class);
191
        Visitor visitor = (Visitor) visitorControl.getMock();
192

    
193
        visitor.visit(ref1);
194
        visitor.visit(ref2);
195
        visitorControl.replay();
196

    
197
        // Add selected references
198
        selection.select(ref1);
199
        selection.select(ref2);
200

    
201
        // Use visitor
202
        selection.accept(visitor);
203

    
204
        // check correct visitor usage
205
        visitorControl.verify();
206
    }
207
}