Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_data / src-test / org / gvsig / fmap / data / feature / paging / FeatureCollectionPagingHelperTest.java @ 23675

History | View | Annotate | Download (8.3 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.data.feature.paging;
28

    
29
import java.util.Iterator;
30

    
31
import junit.framework.TestCase;
32

    
33
import org.easymock.MockControl;
34
import org.gvsig.fmap.data.feature.*;
35

    
36
/**
37
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
38
 */
39
public class FeatureCollectionPagingHelperTest extends TestCase {
40

    
41
    private FeatureCollectionPagingHelper helper;
42
    private MockControl collectionControl;
43
    private FeatureCollection collectionMock;
44
    private MockControl storeControl;
45
    private FeatureStore storeMock;
46

    
47
    protected void setUp() throws Exception {
48
        super.setUp();
49
        collectionControl = MockControl
50
                .createNiceControl(FeatureCollection.class);
51
        collectionMock = (FeatureCollection) collectionControl.getMock();
52
        storeControl = MockControl.createNiceControl(FeatureStore.class);
53
        storeMock = (FeatureStore) storeControl.getMock();
54
    }
55

    
56
    protected void tearDown() throws Exception {
57
        super.tearDown();
58
    }
59

    
60
    /**
61
     * Test method for
62
     * {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getMaxPageSize()}
63
     */
64
    public void testGetMaxPageSize() throws Exception {
65
        storeControl.expectAndReturn(storeMock.getFeatureCollection(),
66
                collectionMock, 2);
67
        storeControl.replay();
68
        collectionControl.expectAndReturn(collectionMock.size(), 10, 2);
69
        collectionControl.replay();
70

    
71
        helper = new FeatureCollectionPagingHelperImpl(storeMock);
72
        assertEquals(
73
                "El tama?o m?ximo de p?gina no es el establecido en el helper",
74
                FeatureCollectionPagingHelper.DEFAULT_PAGE_SIZE, helper
75
                        .getMaxPageSize());
76

    
77
        helper = new FeatureCollectionPagingHelperImpl(storeMock, 5);
78
        assertEquals(
79
                "El tama?o m?ximo de p?gina no es el establecido en el helper",
80
                5, helper.getMaxPageSize());
81

    
82
        storeControl.verify();
83
        collectionControl.verify();
84
    }
85

    
86
    /**
87
     * Test method for
88
     * {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getNumPages()}
89
     * .
90
     */
91
    public void testGetNumPages() throws Exception {
92
        storeControl.expectAndReturn(storeMock.getFeatureCollection(),
93
                collectionMock);
94
        storeControl.replay();
95
        collectionControl.expectAndReturn(collectionMock.size(), 10);
96
        collectionControl.replay();
97

    
98
        helper = new FeatureCollectionPagingHelperImpl(storeMock);
99

    
100
        assertEquals("El n? de p?ginas calculadas no es correcto", 1, helper
101
                .getNumPages());
102

    
103
        storeControl.verify();
104
        collectionControl.verify();
105
    }
106

    
107
    /**
108
     * Test method for
109
     * {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getTotalSize()}
110
     * .
111
     */
112
    public void testGetTotalSize() throws Exception {
113
        storeControl.expectAndReturn(storeMock.getFeatureCollection(),
114
                collectionMock);
115
        storeControl.replay();
116
        collectionControl.expectAndReturn(collectionMock.size(), 10, 2);
117
        collectionControl.replay();
118

    
119
        helper = new FeatureCollectionPagingHelperImpl(storeMock);
120

    
121
        assertEquals("El n? total de features no es el de la Colecci?n", 10,
122
                helper.getTotalSize());
123

    
124
        storeControl.verify();
125
        collectionControl.verify();
126
    }
127

    
128
    /**
129
     * Test method for
130
     * {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#getFeatureAt(int)}
131
     * .
132
     */
133
    public void testGetFeatureAt() throws Exception {
134
        // Create two mock features for the test.
135
        MockControl featureControl = MockControl.createControl(Feature.class);
136
        Feature mockFeature1 = (Feature) featureControl.getMock();
137
        Feature mockFeature2 = (Feature) featureControl.getMock();
138

    
139
        // Create a mock Iterator to return the Features.
140
        MockControl iteratorControl = MockControl
141
                .createControl(FeatureIterator.class);
142
        Iterator mockIterator = (Iterator) iteratorControl.getMock();
143

    
144
        // Values 0 and 1
145
        mockIterator.hasNext();
146
        iteratorControl.setReturnValue(true);
147
        mockIterator.next();
148
        iteratorControl.setReturnValue(mockFeature1);
149
        mockIterator.hasNext();
150
        iteratorControl.setReturnValue(true);
151
        mockIterator.next();
152
        iteratorControl.setReturnValue(mockFeature2);
153
        mockIterator.hasNext();
154
        iteratorControl.setReturnValue(false);
155

    
156
        // Values 4 and 5
157
        mockIterator.hasNext();
158
        iteratorControl.setReturnValue(true);
159
        mockIterator.next();
160
        iteratorControl.setReturnValue(mockFeature2);
161
        mockIterator.hasNext();
162
        iteratorControl.setReturnValue(true);
163
        mockIterator.next();
164
        iteratorControl.setReturnValue(mockFeature1);
165
        mockIterator.hasNext();
166
        iteratorControl.setReturnValue(false);
167

    
168
        iteratorControl.replay();
169

    
170
        // Define the mock Collection operations.
171
        storeControl.expectAndReturn(storeMock.getFeatureCollection(),
172
                collectionMock);
173
        storeControl.replay();
174

    
175
        collectionControl.expectAndReturn(collectionMock.size(), 11);
176
        collectionControl.expectAndReturn(collectionMock.iterator(0),
177
                mockIterator);
178
        collectionControl.expectAndReturn(collectionMock.iterator(4),
179
                mockIterator);
180
        collectionControl.replay();
181

    
182
        helper = new FeatureCollectionPagingHelperImpl(storeMock, 2);
183

    
184
        // Check the returned Features are the correct ones
185
        Feature feature = helper.getFeatureAt(0);
186
        assertEquals("La Feature devuelta no es la que corresponde "
187
                + "al ?ndice solicitado: 0", mockFeature1, feature);
188

    
189
        feature = helper.getFeatureAt(1);
190
        assertEquals("La Feature devuelta no es la que corresponde "
191
                + "al ?ndice solicitado: 1", mockFeature2, feature);
192

    
193
        feature = helper.getFeatureAt(5);
194
        assertEquals("La Feature devuelta no es la que corresponde "
195
                + "al ?ndice solicitado: 5", mockFeature1, feature);
196

    
197
        try {
198
            feature = helper.getFeatureAt(9999);
199
            fail("Returned a value for a non existent Feature position");
200
        } catch (Exception ex) {
201
            // Good
202
        }
203

    
204
        storeControl.verify();
205
        collectionControl.verify();
206
        iteratorControl.verify();
207
    }
208

    
209
    /**
210
     * Test method for
211
     * {@link org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper#setCurrentPage(int)}
212
     * .
213
     */
214
    public void testSetCurrentPage() throws Exception {
215
        storeControl.expectAndReturn(storeMock.getFeatureCollection(),
216
                collectionMock);
217
        storeControl.replay();
218
        collectionControl.expectAndReturn(collectionMock.size(), 10);
219
        collectionControl.replay();
220

    
221
        helper = new FeatureCollectionPagingHelperImpl(storeMock);
222

    
223
        try {
224
            helper.setCurrentPage(-4);
225
            fail("Allowed to set a negative number as the current page");
226
        } catch (Exception ex) {
227
            // Good
228
        }
229

    
230
        try {
231
            helper.setCurrentPage(99999);
232
            fail("Allowed to set a current page greater than the number "
233
                    + "of available pages");
234
        } catch (Exception ex) {
235
            // Good
236
        }
237

    
238
        storeControl.verify();
239
        collectionControl.verify();
240
    }
241

    
242
}