Source code: com/tripi/asp/test/AspCollectionTest.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp.test;
26
27 import junit.framework.*;
28 import java.util.Enumeration;
29 import com.tripi.asp.*;
30
31 /**
32 * This class tests the AspCollection object.
33 *
34 * @author Terence Haddock
35 */
36 public class AspCollectionTest extends TestCase
37 {
38 /**
39 * Constructor.
40 * @param name Test name
41 */
42 public AspCollectionTest(String name)
43 {
44 super(name);
45 }
46
47 /** Caches versions of integers */
48 public static final Integer zero = new Integer(0);
49 public static final Integer one = new Integer(1);
50 public static final Integer two = new Integer(2);
51 public static final Integer three = new Integer(3);
52
53 /** Test put by index */
54 public void testPutByIndex() throws AspException
55 {
56 AspCollection col = new AspCollection();
57 col.put(three, "What's up?");
58 col.put(one, "Hello there");
59 col.put(two, "How are you?");
60 Assert.assertEquals("Hello there", col.get(one));
61 Assert.assertEquals("How are you?", col.get(two));
62 Assert.assertEquals("What's up?", col.get(three));
63 }
64
65 /** Test put by key */
66 public void testPutByKey() throws AspException
67 {
68 AspCollection col = new AspCollection();
69 col.put("Hello", "What's up?");
70 Assert.assertEquals("What's up?", col.get("Hello"));
71 Assert.assertEquals("What's up?", col.get("HELLO"));
72 Assert.assertEquals("What's up?", col.get("hello"));
73 Assert.assertEquals("What's up?", col.get("hElLo"));
74 }
75
76 /** Test put by key/index mix */
77 public void testPutByKeyIndex() throws AspException
78 {
79 AspCollection col = new AspCollection();
80 col.put("Hello", "First Row");
81 col.put("There", "Second Row");
82 col.put("George", "Third Row");
83 col.put(two, "Second Row Revisited");
84 Assert.assertEquals("First Row", col.get(one));
85 Assert.assertEquals("Second Row Revisited", col.get(two));
86 Assert.assertEquals("Third Row", col.get(three));
87 Assert.assertEquals("First Row", col.get("Hello"));
88 Assert.assertEquals("Second Row Revisited", col.get("There"));
89 Assert.assertEquals("Third Row", col.get("George"));
90 }
91
92 /** Test getting non-existant element */
93 public void testGetNonExistant() throws AspException
94 {
95 AspCollection col = new AspCollection();
96 Assert.assertEquals(Constants.undefinedValueNode, col.get("chacha"));
97 }
98
99 /** Test ContainsKey */
100 public void testContainsKey() throws AspException
101 {
102 AspCollection col = new AspCollection();
103 col.put("Hello", "First Row");
104 col.put("There", "Second Row");
105 col.put("George", "Third Row");
106 Assert.assertTrue(col.containsKey("Hello"));
107 Assert.assertTrue(col.containsKey("HELLO"));
108 Assert.assertTrue(col.containsKey("hElLo"));
109 Assert.assertTrue(col.containsKey("There"));
110 Assert.assertTrue(col.containsKey("George"));
111 Assert.assertTrue(!col.containsKey("NotGeorge"));
112 }
113
114 /** Test keys enumeration */
115 public void testKeysEnumeration() throws AspException
116 {
117 AspCollection col = new AspCollection();
118 col.put("One", "First");
119 col.put("Two", "Second");
120 col.put("Three", "Third");
121 Enumeration enKeys = col.keys();
122 Assert.assertEquals("One", (String)enKeys.nextElement());
123 Assert.assertEquals("Two", (String)enKeys.nextElement());
124 Assert.assertEquals("Three", (String)enKeys.nextElement());
125 }
126
127 /** Test elements enumeration */
128 public void testElementsEnumeration() throws AspException
129 {
130 AspCollection col = new AspCollection();
131 col.put("One", "First");
132 col.put("Two", "Second");
133 col.put("Three", "Third");
134 Enumeration enKeys = col.elements();
135 boolean hasFirst = false, hasSecond = false, hasThird = false;
136 while (enKeys.hasMoreElements())
137 {
138 String key = (String)enKeys.nextElement();
139 if (key.equals("First")) {
140 Assert.assertTrue(!hasFirst);
141 hasFirst = true;
142 } else if (key.equals("Second")) {
143 Assert.assertTrue(!hasSecond);
144 hasSecond = true;
145 } else if (key.equals("Third")) {
146 Assert.assertTrue(!hasThird);
147 hasThird = true;
148 }
149 }
150 Assert.assertTrue(hasFirst);
151 Assert.assertTrue(hasSecond);
152 Assert.assertTrue(hasThird);
153 }
154 }
155