KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > ui > test > TestCommandPackage


1 package net.firstpartners.nounit.ui.test;
2
3 /**
4  * Title: NoUnit - Identify Classes that are not being unit Tested
5  *
6  * Copyright (C) 2001 Paul Browne , FirstPartners.net
7  *
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * @author Paul Browne
24  * @version 0.7
25  */

26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34 import net.firstpartners.nounit.ui.common.CommandPackage;
35
36 /**
37  * Test the Command package class in the Junit testrunner
38  */

39 public class TestCommandPackage extends TestCase {
40
41   /**
42    * Call the Junit test constructor
43    * @param name
44    */

45     public TestCommandPackage(String JavaDoc name) {
46     super(name);
47   }
48
49    /**
50    * Enable Junit to run this Class individually
51    * @param args
52    */

53   public static void main(String JavaDoc[] args) {
54       junit.textui.TestRunner.run (suite());
55   }
56
57   /**
58    * Enable Junit to run this class
59    * @return TestSuite
60    */

61   public static Test suite() {
62       return new TestSuite(TestCommandPackage.class);
63   }
64
65   /**
66    * Test the creation of the Command Package (Method1) and retrieving Values
67    * from the command line
68    * @exception NoUnitException
69    */

70   public void testCommandPackageCreation()
71   throws Exception JavaDoc {
72
73
74     String JavaDoc[] tmpSeparateArray = TestUserData.getCommandLineValues();
75
76     //Create Command Package
77
CommandPackage testCommandPackage = new CommandPackage(tmpSeparateArray);
78
79
80     //Check that Value Pairs come out ok
81
}
82
83   /**
84    * Test getting of keys from Command Package
85    */

86   public void testGetKeys() throws Exception JavaDoc{
87
88     CommandPackage newWP = new CommandPackage();
89     assertTrue(newWP.getStoreNames()!=null);
90     assertTrue(newWP.getStoreNames().hasNext()); //pre populated with base defaults
91

92     //Add values to CommandPackage
93
newWP.addValue("key1","value1");
94     newWP.addValue("key2","value2");
95     newWP.addValue("key3","value3");
96
97     Iterator JavaDoc myEnum = newWP.getStoreNames();
98     assertTrue(newWP.getStoreNames()!=null);
99   }
100
101   /**
102    * Test adding of Keys
103    */

104    public void testAddHashTable() throws Exception JavaDoc {
105
106     CommandPackage newWP = new CommandPackage();
107     assertTrue(!newWP.getValuePairs().isEmpty()); //pre populated with base defaults
108

109     //Create New Hashtable and add values
110
HashMap JavaDoc newHash = new HashMap JavaDoc();
111       newHash.put("key1","value1");
112       newHash.put("key2","value2");
113       newHash.put("key3","value3");
114       newWP.addValues(newHash);
115
116       assertTrue(newWP.getValuePairs()!=null);
117       HashMap JavaDoc outHash = newWP.getValuePairs();
118       assertTrue(!outHash.isEmpty());
119       assertTrue(outHash.containsKey("key1"));
120       assertTrue(outHash.containsKey("key2"));
121       assertTrue(outHash.containsKey("key3"));
122       assertTrue(outHash.containsKey("key1"));
123       assertTrue(outHash.containsKey("key2"));
124       assertTrue(outHash.containsKey("key3"));
125
126    }
127
128   /**
129    * Test Storage / Retrieval of Ints
130    */

131   public void testStoreGetInt() throws Exception JavaDoc {
132
133     CommandPackage newWP = new CommandPackage();
134
135     newWP.addValue("num1",new Integer JavaDoc(1024));
136     newWP.addValue("num2","876");
137     newWP.addValue("letters","xyz");
138
139     assertTrue(newWP.getInt("num1") == 1024);
140     assertTrue(newWP.getInt("num2") == 876);
141     assertTrue(newWP.getInt("letters") == 0);
142     assertTrue(newWP.getInt("some-randon-non-existant-key") == 0);
143
144   }
145
146   
147   /**
148    * Test Storage / Retrieval of Ints
149    */

150   public void testMultipleValues() throws Exception JavaDoc {
151
152     CommandPackage newWP = new CommandPackage();
153
154     String JavaDoc[] valuesAdd = {"samekey","value1",
155                           "samekey","value2",
156                           "samekey","value3"};
157
158     newWP.addValues(valuesAdd);
159
160     Object JavaDoc tmpObject = newWP.getValue("samekey");
161     assertTrue(tmpObject!=null);
162     assertTrue(tmpObject instanceof Vector JavaDoc);
163
164     //Try with normal get object method
165
Vector JavaDoc tmpVector = (Vector JavaDoc) tmpObject;
166     assertTrue(!tmpVector.isEmpty());
167     assertTrue(tmpVector.contains("value1"));
168     assertTrue(tmpVector.contains("value2"));
169     assertTrue(tmpVector.contains("value3"));
170     assertTrue(tmpVector.size()==3);
171
172     //Try with getVector method
173
tmpVector = newWP.getVector("samekey");
174     assertTrue(!tmpVector.isEmpty());
175     assertTrue(tmpVector.contains("value1"));
176     assertTrue(tmpVector.contains("value2"));
177     assertTrue(tmpVector.contains("value3"));
178     assertTrue(tmpVector.size()==3);
179
180   }
181
182   /**
183    * Check Double Add
184    * Objects added at differenct times should not be vectorized...
185    */

186   public void testDoubleAdd() throws Exception JavaDoc {
187
188     CommandPackage myWP = new CommandPackage();
189
190     myWP.addValue("key1","value1");
191
192     //Unique Values
193
String JavaDoc[] valuesAdd = {"key1","value1",
194                           "key2","value2",
195                           "key3","value3"};
196
197     //Add Twice to try and force vectoization
198
myWP.addValues(valuesAdd);
199       myWP.addValues(valuesAdd);
200
201     //Test Results
202
Object JavaDoc tmpObject = myWP.getValue("key1");
203     assertTrue(tmpObject instanceof String JavaDoc);
204
205   }
206
207
208
209   /**
210    * test add additional data
211    */

212    public void testAddAdditionalData() {
213
214       CommandPackage myWp = new CommandPackage();
215       myWp.addAdditionalValue("somerandomkeyxxx","someobject");
216
217       Object JavaDoc tmpObject = myWp.getValue("somerandomkeyxxx");
218       assertTrue(tmpObject!=null);
219       assertTrue(tmpObject instanceof String JavaDoc);
220
221       //now add another value
222
myWp.addAdditionalValue("somerandomkeyxxx","someotherobject");
223       tmpObject = myWp.getValue("somerandomkeyxxx");
224       assertTrue(tmpObject!=null);
225       assertTrue(tmpObject instanceof Vector JavaDoc);
226
227       //Test contents of Vector
228
Vector JavaDoc tmpVector = (Vector JavaDoc)tmpObject;
229       assertTrue(!tmpVector.isEmpty());
230       assertTrue(tmpVector.contains("someobject"));
231       assertTrue(tmpVector.contains("someotherobject"));
232
233    }
234
235    /**
236     * test remove method
237     */

238    public void testRemove(){
239        
240       CommandPackage myWp= new CommandPackage();
241       myWp.addValue("X2345","LLL");
242       assertTrue(myWp.getValue("X2345")!=null);
243       myWp.remove("X2345");
244       assertTrue(myWp.getValue("X2345")==null);
245
246    }
247
248   /**
249    * Test Move Method
250    */

251   public void testMove(){
252       
253     CommandPackage myWp= new CommandPackage();
254
255     myWp.addValue("SomeKey1","SomeValue");
256     assertTrue(myWp.getValue("SomeKey1")!=null);
257     assertTrue(myWp.getValue("SomeNewKey1")==null);
258
259     myWp.move("SomeKey1","SomeNewKey1");
260     assertTrue(myWp.getValue("SomeKey1")==null);
261     assertTrue(myWp.getValue("SomeNewKey1")!=null);
262
263   }
264
265 }
Popular Tags