1 7 8 package org.jdesktop.dataset; 9 10 import java.util.List ; 11 import junit.framework.*; 12 13 14 18 public class DataRelationTest extends TestCase { 19 20 public DataRelationTest(String testName) { 21 super(testName); 22 } 23 24 27 protected void setUp() throws Exception { 28 } 29 30 protected void tearDown() throws Exception { 31 } 32 33 public static Test suite() { 34 TestSuite suite = new TestSuite(DataRelationTest.class); 35 36 return suite; 37 } 38 39 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 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 try { 65 relation.setName(null); 66 assertTrue(false); 67 } catch (Error e) { 68 assertTrue(true); 69 } 70 71 try { 73 relation.setName(""); 74 assertTrue(false); 75 } catch (Error e) { 76 assertTrue(true); 77 } 78 79 80 try { 82 relation.setName(" "); 83 assertTrue(false); 84 } catch (Error e) { 85 assertTrue(true); 86 } 87 } 88 89 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 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 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 List <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 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 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 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 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 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 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 |