KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > DataSelectorTest


1 /*
2  * $Id: DataSelectorTest.java,v 1.2 2005/02/25 19:46:05 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.dataset;
9
10 import junit.framework.*;
11
12
13 /**
14  *
15  * @author rbair
16  */

17 public class DataSelectorTest extends TestCase {
18     
19     public DataSelectorTest(String JavaDoc testName) {
20         super(testName);
21     }
22     
23     // TODO add test methods here. The name must begin with 'test'. For example:
24
// public void testHello() {}
25

26     protected void setUp() throws Exception JavaDoc {
27     }
28
29     protected void tearDown() throws Exception JavaDoc {
30     }
31
32     public static Test suite() {
33         TestSuite suite = new TestSuite(DataSelectorTest.class);
34         
35         return suite;
36     }
37
38     /**
39      * Test of getDataTable method, of class org.jdesktop.dataset.DataSelector.
40      */

41     public void testGetDataTable() {
42         System.out.println("testGetDataTable");
43
44         DataSet ds = new DataSet();
45         DataTable t = ds.createTable();
46         DataSelector s = t.createSelector();
47         assertNotNull(s);
48         assertEquals(s.getDataTable(), t);
49     }
50
51     /**
52      * Test of getName method, of class org.jdesktop.dataset.DataSelector.
53      */

54     public void testGetName() {
55         System.out.println("testGetName");
56         
57         //test that the name can be set
58
DataSet ds = new DataSet();
59         DataTable table = ds.createTable();
60         DataSelector s = table.createSelector();
61         s.setName("sel_1");
62         assertEquals(s.getName(), "sel_1");
63         
64         //test that the name cannot be set to null
65
try {
66             s.setName(null);
67             assertTrue(false);
68         } catch (Error JavaDoc e) {
69             assertTrue(true);
70         }
71         
72         //test that the name cannot be set to empty string
73
try {
74             s.setName("");
75             assertTrue(false);
76         } catch (Error JavaDoc e) {
77             assertTrue(true);
78         }
79         
80         
81         //test that the name cannot be set to empty string with spaces
82
try {
83             s.setName(" ");
84             assertTrue(false);
85         } catch (Error JavaDoc e) {
86             assertTrue(true);
87         }
88     }
89
90     /**
91      * Test of getRowIndices method, of class org.jdesktop.dataset.DataSelector.
92      */

93     public void testGetRowIndices() {
94         System.out.println("testGetRowIndices");
95         
96         DataSet ds = new DataSet();
97         DataTable table = ds.createTable();
98         DataColumn idCol = table.createColumn();
99         idCol.setName("id");
100         DataColumn fnCol = table.createColumn();
101         fnCol.setName("firstName");
102         DataColumn lnCol = table.createColumn();
103         lnCol.setName("lastName");
104         DataRow row0 = table.appendRow();
105         DataRow row1 = table.appendRow();
106         DataRow row2 = table.appendRow();
107         DataRow row3 = table.appendRow();
108         DataRow row4 = table.appendRow();
109         
110         //start setting values
111
row0.setValue("id", 1);
112         row0.setValue("firstName", "Richard");
113         row0.setValue("lastName", "Bair");
114         row1.setValue("id", 2);
115         row1.setValue("firstName", "Bino");
116         row1.setValue("lastName", "George");
117         row2.setValue("id", 3);
118         row2.setValue("firstName", "Hans");
119         row2.setValue("lastName", "Muller");
120         row3.setValue("id", 4);
121         row3.setValue("firstName", "Scott");
122         row3.setValue("lastName", "Violet");
123         row4.setValue("id", 5);
124         row4.setValue("firstName", "Kleopatra");
125         
126         //create the data selector
127
DataSelector s = table.createSelector();
128         
129         //assert that the list is empty by default
130
assertEquals(0, s.getRowIndices().size());
131         
132         //set the selection to be the first record
133
s.setRowIndices(new int[]{0});
134         assertEquals(1, s.getRowIndices().size());
135         assertEquals(1, table.getRow(s.getRowIndices().get(0)).getValue("id"));
136         
137         //set the selection to be the third record
138
s.setRowIndices(new int[]{2});
139         assertEquals(1, s.getRowIndices().size());
140         assertEquals(3, table.getRow(s.getRowIndices().get(0)).getValue("id"));
141         
142         //set the selection to be the last record
143
s.setRowIndices(new int[]{4});
144         assertEquals(1, s.getRowIndices().size());
145         assertEquals(5, table.getRow(s.getRowIndices().get(0)).getValue("id"));
146         
147         //set the first and last to be selected
148
s.setRowIndices(new int[]{0,4});
149         assertEquals(2, s.getRowIndices().size());
150         assertEquals(1, table.getRow(s.getRowIndices().get(0)).getValue("id"));
151         assertEquals(5, table.getRow(s.getRowIndices().get(1)).getValue("id"));
152     }
153     
154 }
155
Popular Tags