Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / test / java / org / gvsig / tools / dynobject / DynObjectPagingHelperTest.java @ 105

History | View | Annotate | Download (3.69 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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {}  {{Task}}
26
 */
27
package org.gvsig.tools.dynobject;
28

    
29
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
30

    
31
/**
32
 * Tests for the {@link DynObjectPagingHelper} interface.
33
 * 
34
 * @author 2010- C?sar Ordi?ana - gvSIG team
35
 */
36
public abstract class DynObjectPagingHelperTest extends
37
                AbstractLibraryAutoInitTestCase {
38

    
39
        private DynObjectPagingHelper helper;
40

    
41
        protected void doSetUp() throws Exception {
42
                helper = createDynObjectPagingHelper();
43
        }
44

    
45
        /**
46
         * Test method for the max page size setting.
47
         */
48
        public void testMaxPageSize() throws Exception {
49

    
50
                helper.setMaxPageSize(2);
51
                helper.setCurrentPage(0);
52

    
53
                long numPages = helper.getNumPages();
54
                if (numPages == 0) {
55
                        fail("The provided helper can't have 0 pages, "
56
                                        + "please provide one with some data for the test");
57
                }
58
                for (long i = numPages; i < numPages - 1; i++) {
59
                        assertEquals("Number of elements of page " + i
60
                                        + " is not the same as the maximum page size", numPages,
61
                                        helper.getCurrentPageDynObjects().length);
62
                        helper.setCurrentPage(i + 1);
63
                }
64
                assertTrue(
65
                                "Last page is not less than or equal to the maximum page size",
66
                                numPages >= helper.getCurrentPageDynObjects().length);
67
        }
68

    
69
        /**
70
         * Test method for
71
         * {@link org.gvsig.tools.dynobject.DynObjectPagingHelper#getNumPages()}.
72
         */
73
        public void testGetNumPages() throws Exception {
74
                long numDOs = helper.getTotalSize();
75
                long pageSize = helper.getMaxPageSize();
76

    
77
                long numPages = (long) Math.ceil(numDOs / pageSize);
78
                assertEquals(numPages, helper.getNumPages());
79

    
80
                pageSize = 2;
81
                helper.setMaxPageSize((int) pageSize);
82

    
83
                numPages = (long) Math.ceil(numDOs / pageSize);
84
                assertEquals(numPages, helper.getNumPages());
85
        }
86

    
87
        /**
88
         * Test method for
89
         * {@link org.gvsig.tools.dynobject.DynObjectPagingHelper#setCurrentPage(long)}
90
         * .
91
         */
92
        public void testSetCurrentPage() throws Exception {
93
                try {
94
                        helper.setCurrentPage(helper.getNumPages() + 1);
95
                        fail("Helper allows to set a page which does not exist");
96
                } catch (Exception e) {
97
                        // Works as excepted
98
                }
99

    
100
                try {
101
                        helper.setCurrentPage(-1);
102
                        fail("Helper allows to set a negative page");
103
                } catch (Exception e) {
104
                        // Works as excepted
105
                }
106
        }
107

    
108
        /**
109
         * Test method for
110
         * {@link org.gvsig.tools.dynobject.DynObjectPagingHelper#getCurrentPageDynObjects()}
111
         * .
112
         */
113
        public void testGetCurrentPageDynObjects() throws Exception {
114
                helper.setMaxPageSize(2);
115
                long numPages = helper.getNumPages();
116

    
117
                if (numPages < 2) {
118
                        fail("The provided helper can't have 0 or 1 page, "
119
                                        + "please provide one with some more data for the test");
120
                }
121

    
122
                helper.setCurrentPage(0);
123
                DynObject[] values = helper.getCurrentPageDynObjects();
124
                assertTrue(values != null && values.length == 2);
125
        }
126

    
127
        protected abstract DynObjectPagingHelper createDynObjectPagingHelper()
128
                        throws Exception;
129
}