Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src-test / org / gvsig / fmap / dal / feature / paging / impl / AbstractPagingCalculatorTest.java @ 33378

History | View | Annotate | Download (4.07 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
package org.gvsig.fmap.dal.feature.paging.impl;
23

    
24
import junit.framework.TestCase;
25

    
26
/**
27
 * Unit tests for the {@link AbstractPagingCalculator} class.
28
 * 
29
 * @author 2010- C?sar Ordi?ana - gvSIG team
30
 */
31
public class AbstractPagingCalculatorTest extends TestCase {
32

    
33
        private AbstractPagingCalculator calculator;
34

    
35
        private static final long SIZE = 22;
36

    
37
        private static final int MAX_PAGE_SIZE = 4;
38

    
39
        protected void setUp() throws Exception {
40
                super.setUp();
41
                calculator = createCalculator();
42
        }
43

    
44
        protected AbstractPagingCalculator createCalculator() {
45
                return new TestPagingCalculator(MAX_PAGE_SIZE, SIZE);
46
        }
47

    
48
        /**
49
         * Test method for
50
         * {@link org.gvsig.fmap.dal.feature.paging.impl.AbstractPagingCalculator#getMaxPageSize()}
51
         * and
52
         * {@link org.gvsig.fmap.dal.feature.paging.impl.AbstractPagingCalculator#setMaxPageSize(int)}
53
         * .
54
         */
55
        public void testMaxPageSize() {
56
                assertEquals(MAX_PAGE_SIZE, calculator.getMaxPageSize());
57

    
58
                calculator.setMaxPageSize(100);
59
                assertEquals(100, calculator.getMaxPageSize());
60

    
61
                try {
62
                        calculator.setMaxPageSize(-10);
63
                        fail("Calculator accepts a negative max page size value");
64
                } catch (Exception ex) {
65
                        // works as expected
66
                }
67
        }
68

    
69
        /**
70
         * Test method for
71
         * {@link org.gvsig.fmap.dal.feature.paging.impl.AbstractPagingCalculator#getCurrentPage()}
72
         * and
73
         * {@link org.gvsig.fmap.dal.feature.paging.impl.AbstractPagingCalculator#setCurrentPage(long)}
74
         * .
75
         */
76
        public void testCurrentPage() {
77
                assertEquals(0, calculator.getCurrentPage());
78

    
79
                long page = calculator.getNumPages() - 1;
80
                calculator.setCurrentPage(page);
81

    
82
                assertEquals(page, calculator.getCurrentPage());
83

    
84
                try {
85
                        calculator.setCurrentPage(-10);
86
                        fail("Calculator accepts a negative current page value");
87
                } catch (Exception ex) {
88
                        // works as expected
89
                }
90
        }
91

    
92
        /**
93
         * Test method for
94
         * {@link org.gvsig.fmap.dal.feature.paging.impl.AbstractPagingCalculator#getInitialIndex()}
95
         * .
96
         */
97
        public void testGetInitialIndex() {
98
                calculator.setCurrentPage(0l);
99
                assertEquals(0, calculator.getInitialIndex());
100

    
101
                calculator.setCurrentPage(2);
102
                assertEquals(2 * calculator.getMaxPageSize(),
103
                                calculator.getInitialIndex());
104
        }
105

    
106
        /**
107
         * Test method for
108
         * {@link org.gvsig.fmap.dal.feature.paging.impl.AbstractPagingCalculator#getNumPages()}
109
         * .
110
         */
111
        public void testGetNumPages() {
112
                assertEquals(6, calculator.getNumPages());
113
        }
114

    
115
        /**
116
         * Test method for
117
         * {@link org.gvsig.fmap.dal.feature.paging.impl.AbstractPagingCalculator#getCurrentPageSize()}
118
         * .
119
         */
120
        public void testGetCurrentPageSize() {
121
                for (int i = 0; i < calculator.getNumPages() - 1; i++) {
122
                        calculator.setCurrentPage(i);
123
                        assertEquals(MAX_PAGE_SIZE, calculator.getCurrentPageSize());                        
124
                }
125
                calculator.setCurrentPage(calculator.getNumPages() - 1);
126
                assertEquals(SIZE % MAX_PAGE_SIZE, calculator.getCurrentPageSize());
127
        }
128

    
129
        private class TestPagingCalculator extends AbstractPagingCalculator {
130

    
131
                private final long size;
132

    
133
                public TestPagingCalculator(int maxPageSize, long size) {
134
                        super(maxPageSize);
135
                        this.size = size;
136
                }
137

    
138
                public TestPagingCalculator(int maxPageSize, long currentPage, long size) {
139
                        super(maxPageSize, currentPage);
140
                        this.size = size;
141
                }
142

    
143
                public long getTotalSize() {
144
                        return size;
145
                }
146

    
147
        }
148

    
149
}