KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > viewer > tableviewer > AttributeTableModelTest


1 /**
2  * Created by IntelliJ IDEA.
3  * User: erstr01
4  * Date: Mar 19, 2003
5  * Time: 11:45:12 AM
6  * To change this template use Options | File Templates.
7  */

8 package com.ca.directory.jxplorer.viewer.tableviewer;
9
10 import junit.framework.*;
11 import javax.naming.NamingException JavaDoc;
12
13 import com.ca.commons.naming.*;
14
15
16
17 public class AttributeTableModelTest extends TestCase
18 {
19     /**
20      * Set up a test AttributeTableModel with some standard attributes and a multi-valued RDN.
21      * @return a test AttributeTableModel
22      */

23
24     private AttributeTableModel getStandardTestModel()
25     {
26         DXEntry entry = new DXEntry(new DN("cn=john+sn=smith,ou=pig botherers,o=pig corp"));
27
28         DXAttribute cn = new DXAttribute("cn", "john");
29         cn.add("fred");
30         cn.add("nigel");
31
32         DXAttribute sn = new DXAttribute("sn", "smith");
33         sn.add("john");
34         sn.add("fred");
35
36         DXAttribute objectClass = new DXAttribute("objectClass", "top");
37         objectClass.add("person");
38         objectClass.add("inetOrgPerson");
39
40         DXAttribute favouriteDrink = new DXAttribute("favouriteDrink", "Midori");
41         objectClass.add("Cointreau");
42         objectClass.add("Lemon Juice");
43         objectClass.add("Mmm... Japanese Slipper...");
44
45         entry.put(cn);
46         entry.put(sn);
47         entry.put(objectClass);
48         entry.put("eyeColour", "purple");
49         entry.put("mySocks", "moldy");
50
51
52         AttributeTableModel model = new AttributeTableModel();
53         model.insertAttributes(entry);
54         return model;
55     }
56
57     public AttributeTableModelTest(String JavaDoc name)
58     {
59         super(name);
60     }
61
62     public static Test suite()
63     {
64         return new TestSuite(AttributeTableModelTest.class);
65     }
66
67     public static void main (String JavaDoc[] args)
68     {
69         junit.textui.TestRunner.run(suite());
70     }
71
72     public void testGetRDN()
73     {
74         AttributeTableModel model = getStandardTestModel();
75         assertEquals("test getRDN using cn=john+sn=smith", "cn=john+sn=smith", model.getRDN().toString());
76     }
77
78     public void testRemoveNamingComponent()
79     {
80         AttributeTableModel model = getStandardTestModel();
81         model.removeNamingComponent(new AttributeType("sn", true),new AttributeValue("sn", "smith"));
82         assertEquals("test getRDN after removing 'sn=fred' using cn=john", "cn=john", model.getRDN().toString());
83         assertEquals(model.namingTypes.length, 1);
84         assertEquals(model.namingRawValues.length, 1);
85         assertEquals(model.numberNamingValues, 1);
86     }
87
88     public void testAddNamingComponent()
89     {
90         AttributeTableModel model = getStandardTestModel();
91
92         int row = getSpecificAttributeValueRow(model, "eyeColour", "purple");
93
94         assertTrue("Couldn't find eyeColour attribute ", (row != -1));
95         AttributeValue val = (AttributeValue)model.attributeValues.get(row);
96         AttributeType type = (AttributeType)model.attributeTypes.get(row);
97
98         model.addNamingComponent(type, val);
99         assertEquals("test getRDN after adding eyeColour=purple ", "cn=john+eyecolour=purple+sn=smith", model.getRDN().toString());
100
101         assertEquals(model.namingTypes.length, 3);
102         assertEquals(model.namingRawValues.length, 3);
103         assertEquals(model.numberNamingValues, 3);
104
105     }
106
107
108
109     private int getSpecificAttributeValueRow(AttributeTableModel model, String JavaDoc id, String JavaDoc value)
110     {
111         AttributeValue val = null;
112         for (int i=0; i<model.attributeValues.size(); i++)
113         {
114             val = (AttributeValue)model.attributeValues.get(i);
115             //System.out.println("searching : " + val.getID().toString() + ":" + val.getStringValue() + " for " + id + ":" + value);
116
if (val.getID().equalsIgnoreCase(id) && val.getStringValue().equals(value))
117             {
118                 return i;
119             }
120         }
121         return -1;
122     }
123
124
125
126     public void testChangeMultiValuesNamingComponentValue()
127     {
128         AttributeTableModel model = getStandardTestModel();
129
130         // find which row is 'cn=john', and change it...
131
int row = getSpecificAttributeValueRow(model, "cn", "john");
132
133         assertTrue("couldn't find test attribute cn=john to rename", (row!=-1));
134         model.setValueAt("erick", row, 1);
135         assertEquals("erick", model.getValueAt(row, 1).toString());
136         assertEquals("renamed rdn from cn=john+sn=smith to cn=erick+sn=smith", "cn=erick+sn=smith", model.getRDN().toString());
137     }
138
139     public void testChangeSingleValuedNamingComponentValue()
140     {
141         AttributeTableModel model = getStandardTestModel();
142
143         model.removeNamingComponent(new AttributeType("sn", true),new AttributeValue("sn", "smith"));
144         // find which row is 'cn=john', and change it...
145
int row = -1;
146         for (int i=0; i<model.attributeValues.size(); i++)
147         {
148             AttributeValue val = (AttributeValue)model.attributeValues.get(i);
149             if (val.getID().equals("cn") && val.getStringValue().equals("john"))
150             {
151                 row = i;
152                 break;
153             }
154         }
155         assertTrue("couldn't find test attribute cn=john to rename", (row!=-1));
156         model.setValueAt("erick", row, 1);
157         assertEquals("erick", model.getValueAt(row, 1).toString());
158
159         assertEquals("renamed rdn from cn=john to cn=erick", "cn=erick", model.getRDN().toString());
160     }
161
162
163
164     public void testRemoveRowFromArray() throws NamingException JavaDoc
165     {
166         int testElement1 = 0;
167         String JavaDoc[] test1 = new String JavaDoc[] {"0","1","2","3","4"};
168         String JavaDoc[] test1b = new String JavaDoc[] {"1","2","3","4"};
169         int testElement2 = 3;
170         String JavaDoc[] test2 = new String JavaDoc[] {"0","1","2","3","4"};
171         String JavaDoc[] test2b = new String JavaDoc[] {"0","1","2","4"};
172         int testElement3 = 4;
173         String JavaDoc[] test3 = new String JavaDoc[] {"0","1","2","3","4"};
174         String JavaDoc[] test3b = new String JavaDoc[] {"0","1","2","3"};
175
176
177         // test first element removal
178
assertEqualArrays(test1b, AttributeTableModel.removeRowFromArray(test1, testElement1));
179
180         // test mid element removal
181
assertEqualArrays(test2b, AttributeTableModel.removeRowFromArray(test2, testElement2));
182
183         // test end element removal
184
assertEqualArrays(test3b, AttributeTableModel.removeRowFromArray(test3, testElement3));
185
186         // test lower out-of-bounds error
187
assertEqualArrays(test1, AttributeTableModel.removeRowFromArray(test1, -1));
188
189         // test upper out-of-bounds error
190
assertEqualArrays(test1, AttributeTableModel.removeRowFromArray(test1, test1.length));
191
192     }
193
194     private void assertEqualArrays(String JavaDoc[] array1, String JavaDoc[] array2)
195     {
196         String JavaDoc expectedArray = "expected array is {";
197         for (int i=0; i<array1.length;i++)
198             expectedArray += "\"" + array1[i] + "\",";
199         expectedArray += "]";
200
201         assertEquals(expectedArray + " but lengths aren't the same!", array1.length, array2.length);
202
203         if (array1.length == array2.length)
204         {
205             for (int i=0; i<array1.length; i++)
206                 assertEquals(expectedArray + " but element " + i + " is wrong.", array1[i], array2[i]);
207         }
208     }
209 }
210
Popular Tags