1   /*   Open Source Java Caching Service
2   *    Copyright (C) 2002 Frank Karlstrøm
3   *    This library is free software; you can redistribute it and/or
4   *    modify it under the terms of the GNU Lesser General Public
5   *    License as published by the Free Software Foundation; either
6   *    version 2.1 of the License, or (at your option) any later version.
7   *
8   *    This library is distributed in the hope that it will be useful,
9   *    but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  *    Lesser General Public License for more details.
12  *
13  *    You should have received a copy of the GNU Lesser General Public
14  *    License along with this library; if not, write to the Free Software
15  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  *    The author can be contacted by email: fjankk@users.sourceforge.net
18  */
19  package org.fjank.jcache.collection;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import javax.util.jcache.CacheAccessFactory;
27  import junit.framework.TestCase;
28  
29  /**Test class for FKache's Set implementation.
30   * @author Frank Karlstrøm
31   *
32   */
33  public class SetProxyTest extends TestCase {
34  
35      private Map map;
36  
37      /**
38       * Constructor for SetProxyTest.
39       * @param arg0
40       */
41      public SetProxyTest(String arg0) {
42          super(arg0);
43      }
44  
45      public static void main(String[] args) {
46          junit.textui.TestRunner.run(SetProxyTest.class);
47      }
48  
49      /*
50       * @see TestCase#setUp()
51       */
52      protected void setUp() throws Exception {
53  		this.map = CacheAccessFactory.getInstance().getMapAccess();
54      }
55  
56      final public void testAdd() {
57      	try {
58      		map.keySet().add(new Object());
59      		fail("Should throw UnsupportedOperationException");
60      	}catch (UnsupportedOperationException e) {
61      		//good
62      	}
63      }
64      
65  
66      final public void testAddAll() {
67  		try {
68  			map.keySet().addAll(new ArrayList());
69  			fail("Should throw UnsupportedOperationException");
70  		}catch (UnsupportedOperationException e) {
71  			//good
72  		}
73      }
74  
75      final public void testClear() {
76      	map.clear();
77  		map.put("1", "1");
78  		map.put("2", "1");
79  		map.put("3", "1");
80  		Set set = map.keySet();
81  		set.clear();
82  		assertEquals(true, map.isEmpty());
83      }
84  
85      final public void testContains() {
86      	map.clear();
87      	map.put("jhgfd", "mnbv");
88      	assertEquals(true, map.keySet().contains("jhgfd"));
89      	map.clear();
90  		assertEquals(false, map.keySet().contains("jhgfd"));
91      	
92      }
93  
94      final public void testContainsAll() {
95  		map.clear();
96  		map.put("1", "1");
97  		map.put("2", "1");
98  		map.put("3", "1");
99  		List coll = new ArrayList();
100 		coll.add("1");
101 		coll.add("2");
102 		assertEquals(true, map.keySet().containsAll(coll));
103 		coll.add("3");
104 		assertEquals(true, map.keySet().containsAll(coll));
105 		coll.add("4");
106 		assertEquals(false, map.keySet().containsAll(coll));
107 		
108     }
109 
110     final public void testIsEmpty() {
111     	map.clear();
112     	assertEquals(true, map.keySet().isEmpty());
113     	map.put("1", "2");
114 		assertEquals(false, map.keySet().isEmpty());
115     	
116     }
117 
118     final public void testIterator() {
119     	map.clear();
120 		map.put("1", "2");
121 		map.put("2", "2");
122     	Iterator iter = map.keySet().iterator();
123     	while(iter.hasNext()) {
124     		Object obj = iter.next();
125     		if(obj.equals("1")) iter.remove();
126     	}
127 		assertEquals(true, map.containsKey("2"));
128 		assertEquals(false, map.containsKey("1"));
129     }
130 
131     final public void testRemove() {
132     	map.clear();
133 		map.put("1", "2");
134 		map.put("2", "2");
135 		map.put("3", "2");
136 		map.keySet().remove("2");
137 		assertEquals(false, map.containsKey("2"));
138     }
139 
140     final public void testRemoveAll() {
141 		map.clear();
142 		map.put("1", "2");
143 		map.put("2", "2");
144 		map.put("3", "2");
145 		List list = new ArrayList();
146 		list.add("1");
147 		list.add("2");
148 		map.keySet().removeAll(list);
149 		assertEquals(false, map.containsKey("1"));
150 		assertEquals(false, map.containsKey("2"));
151 		assertEquals(true, map.containsKey("3"));
152     }
153 
154     final public void testRetainAll() {
155 		map.clear();
156 		map.put("1", "2");
157 		map.put("2", "2");
158 		map.put("3", "2");
159 		List list = new ArrayList();
160 		list.add("1");
161 		list.add("2");
162 		map.keySet().retainAll(list);
163 		assertEquals(true, map.containsKey("1"));
164 		assertEquals(true, map.containsKey("2"));
165 		assertEquals(false, map.containsKey("3"));
166     }
167 
168     final public void testSize() {
169     	map.clear();
170     	assertEquals(0, map.keySet().size());
171 		map.put("1","1");
172 		map.put("2","1");
173 		map.put("3","1");
174 		assertEquals(3, map.keySet().size());
175 		map.clear();
176 		assertEquals(0, map.keySet().size());
177     }
178 
179     /*
180      * Test for Object[] toArray()
181      */
182     final public void testToArray() {
183     	map.clear();
184 		String onoe = "1";
185         map.put(onoe,onoe);
186 		String two = "2";
187         map.put(two,onoe);
188 		String three = "3";
189         map.put(three,onoe);
190 		Object[] objArr = map.keySet().toArray();
191 		List test = new ArrayList();
192 		test.add(onoe);
193 		test.add(two);
194 		test.add(three);
195 		for (int i = 0; i < objArr.length; i++) {
196             Object object = objArr[i];
197             assertEquals(true, test.contains(object));
198             test.remove(object);
199         }
200         assertEquals(true,  test.isEmpty());
201     }
202 
203     /*
204      * Test for Object[] toArray(Object[])
205      */
206     final public void testToArrayObjectArray() {
207     	String[] res = new String[4];
208 		String one = "1";
209 		map.put(one,one);
210 		String two = "2";
211 		map.put(two,one);
212 		String three = "3";
213 		map.put(three,one);
214 		String four = "4";
215 		map.put(four, one);
216 		Object[] objArr = map.keySet().toArray(res);
217 		List test = new ArrayList();
218 		test.add(one);
219 		test.add(two);
220 		test.add(three);
221 		test.add(four);
222 		for (int i = 0; i < objArr.length; i++) {
223 			Object object = objArr[i];
224 			assertEquals(true, test.contains(object));
225 			test.remove(object);
226 		}
227 		assertEquals(true,  test.isEmpty());
228 		assertSame(objArr, res);
229     	
230     }
231 
232 }