KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DataRelationTest.java,v 1.2 2005/02/25 19:46:08 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 java.util.List JavaDoc;
11 import junit.framework.*;
12
13
14 /**
15  *
16  * @author rbair
17  */

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

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

42     public void testGetDataSet() {
43         System.out.println("testGetDataSet");
44         
45         DataSet ds = new DataSet();
46         DataRelation relation = ds.createRelation();
47         assertEquals(relation.getDataSet(), ds);
48     }
49
50     /**
51      * Test of getName method, of class org.jdesktop.dataset.DataRelation.
52      */

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

92     public void testGetParentColumn() {
93         System.out.println("testGetParentColumn");
94         
95         DataSet ds = new DataSet();
96         DataRelation relation = ds.createRelation();
97         assertNull(relation.getParentColumn());
98         
99         DataTable parentTable = ds.createTable();
100         DataColumn parentColumn = parentTable.createColumn();
101         relation.setParentColumn(parentColumn);
102         assertEquals(relation.getParentColumn(), parentColumn);
103         
104         relation.setParentColumn(null);
105         assertNull(relation.getParentColumn());
106     }
107
108     /**
109      * Test of getChildColumn method, of class org.jdesktop.dataset.DataRelation.
110      */

111     public void testGetChildColumn() {
112         System.out.println("testGetChildColumn");
113         
114         DataSet ds = new DataSet();
115         DataRelation relation = ds.createRelation();
116         assertNull(relation.getParentColumn());
117         
118         DataTable childTable = ds.createTable();
119         DataColumn childColumn = childTable.createColumn();
120         relation.setChildColumn(childColumn);
121         assertEquals(relation.getChildColumn(), childColumn);
122         
123         relation.setChildColumn(null);
124         assertNull(relation.getChildColumn());
125     }
126
127     /**
128      * Test of getRows method, of class org.jdesktop.dataset.DataRelation.
129      */

130     public void testGetRows() {
131         System.out.println("testGetRows");
132         
133         DataSet ds = new DataSet();
134         DataRelation relation = ds.createRelation();
135         
136         DataTable parentTable = ds.createTable();
137         DataColumn parentColumn = parentTable.createColumn();
138         parentColumn.setName("pid");
139         relation.setParentColumn(parentColumn);
140         parentTable.appendRow().setValue("pid", 100);
141         parentTable.appendRow().setValue("pid", 101);
142         parentTable.appendRow().setValue("pid", 102);
143         
144         DataTable childTable = ds.createTable();
145         DataColumn childColumn = childTable.createColumn();
146         childColumn.setName("cid");
147         childColumn = childTable.createColumn();
148         childColumn.setName("pid");
149         relation.setChildColumn(childColumn);
150         DataRow row = childTable.appendRow();
151         row.setValue("cid", 200);
152         row.setValue("pid", 100);
153         row = childTable.appendRow();
154         row.setValue("cid", 201);
155         row.setValue("pid", 101);
156         row = childTable.appendRow();
157         row.setValue("cid", 202);
158         row.setValue("pid", 101);
159         row = childTable.appendRow();
160         row.setValue("cid", 203);
161         row.setValue("pid", 100);
162         row = childTable.appendRow();
163         row.setValue("cid", 204);
164         row.setValue("pid", 100);
165         row = childTable.appendRow();
166         row.setValue("cid", 205);
167         row.setValue("pid", 102);
168         
169         //get the child rows for parent 100
170
List JavaDoc<DataRow> rows = relation.getRows(0);
171         assertEquals(rows.size(), 3);
172         assertEquals(rows.get(0).getValue("cid"), 200);
173         assertEquals(rows.get(0).getValue("pid"), 100);
174         assertEquals(rows.get(1).getValue("cid"), 203);
175         assertEquals(rows.get(1).getValue("pid"), 100);
176         assertEquals(rows.get(2).getValue("cid"), 204);
177         assertEquals(rows.get(2).getValue("pid"), 100);
178         
179         //get the child rows for parent 101
180
rows = relation.getRows(1);
181         assertEquals(rows.size(), 2);
182         assertEquals(rows.get(0).getValue("cid"), 201);
183         assertEquals(rows.get(0).getValue("pid"), 101);
184         assertEquals(rows.get(1).getValue("cid"), 202);
185         assertEquals(rows.get(1).getValue("pid"), 101);
186         
187         //get the child rows for parent 102
188
rows = relation.getRows(2);
189         assertEquals(rows.size(), 1);
190         assertEquals(rows.get(0).getValue("cid"), 205);
191         assertEquals(rows.get(0).getValue("pid"), 102);
192
193         //get the child rows for both 100 and 102
194
rows = relation.getRows(new int[]{0, 2});
195         assertEquals(rows.size(), 4);
196         assertEquals(rows.get(0).getValue("cid"), 200);
197         assertEquals(rows.get(0).getValue("pid"), 100);
198         assertEquals(rows.get(1).getValue("cid"), 203);
199         assertEquals(rows.get(1).getValue("pid"), 100);
200         assertEquals(rows.get(2).getValue("cid"), 204);
201         assertEquals(rows.get(2).getValue("pid"), 100);
202         assertEquals(rows.get(3).getValue("cid"), 205);
203         assertEquals(rows.get(3).getValue("pid"), 102);
204
205         //get the child rows for both 100 and 101
206
rows = relation.getRows(new int[]{0, 1});
207         assertEquals(rows.size(), 5);
208         assertEquals(rows.get(0).getValue("cid"), 200);
209         assertEquals(rows.get(0).getValue("pid"), 100);
210         assertEquals(rows.get(1).getValue("cid"), 203);
211         assertEquals(rows.get(1).getValue("pid"), 100);
212         assertEquals(rows.get(2).getValue("cid"), 204);
213         assertEquals(rows.get(2).getValue("pid"), 100);
214         assertEquals(rows.get(3).getValue("cid"), 201);
215         assertEquals(rows.get(3).getValue("pid"), 101);
216         assertEquals(rows.get(4).getValue("cid"), 202);
217         assertEquals(rows.get(4).getValue("pid"), 101);
218
219         //get the child rows for both 101 and 102
220
rows = relation.getRows(new int[]{1, 2});
221         assertEquals(rows.size(), 3);
222         assertEquals(rows.get(0).getValue("cid"), 201);
223         assertEquals(rows.get(0).getValue("pid"), 101);
224         assertEquals(rows.get(1).getValue("cid"), 202);
225         assertEquals(rows.get(1).getValue("pid"), 101);
226         assertEquals(rows.get(2).getValue("cid"), 205);
227         assertEquals(rows.get(2).getValue("pid"), 102);
228         
229         //get the child rows for all of them (100, 101, 102)
230
rows = relation.getRows(new int[]{0, 1, 2});
231         assertEquals(rows.size(), 6);
232         assertEquals(rows.get(0).getValue("cid"), 200);
233         assertEquals(rows.get(0).getValue("pid"), 100);
234         assertEquals(rows.get(1).getValue("cid"), 203);
235         assertEquals(rows.get(1).getValue("pid"), 100);
236         assertEquals(rows.get(2).getValue("cid"), 204);
237         assertEquals(rows.get(2).getValue("pid"), 100);
238         assertEquals(rows.get(3).getValue("cid"), 201);
239         assertEquals(rows.get(3).getValue("pid"), 101);
240         assertEquals(rows.get(4).getValue("cid"), 202);
241         assertEquals(rows.get(4).getValue("pid"), 101);
242         assertEquals(rows.get(5).getValue("cid"), 205);
243         assertEquals(rows.get(5).getValue("pid"), 102);
244     }
245 }
Popular Tags