/* Open Source Java Caching Service
* Copyright (C) 2002 Frank Karlstr�m
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The author can be contacted by email: fjankk@users.sourceforge.net
*/
package org.fjank.jcache.collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.util.jcache.CacheAccessFactory;
import junit.framework.TestCase;
/**Test class for FKache's Set implementation.
* @author Frank Karlstr�m
*
*/
public class SetProxyTest extends TestCase {
private Map map;
/**
* Constructor for SetProxyTest.
* @param arg0
*/
public SetProxyTest(String arg0) {
super(arg0);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(SetProxyTest.class);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
this.map = CacheAccessFactory.getInstance().getMapAccess();
}
final public void testAdd() {
try {
map.keySet().add(new Object());
fail("Should throw UnsupportedOperationException");
}catch (UnsupportedOperationException e) {
//good
}
}
final public void testAddAll() {
try {
map.keySet().addAll(new ArrayList());
fail("Should throw UnsupportedOperationException");
}catch (UnsupportedOperationException e) {
//good
}
}
final public void testClear() {
map.clear();
map.put("1", "1");
map.put("2", "1");
map.put("3", "1");
Set set = map.keySet();
set.clear();
assertEquals(true, map.isEmpty());
}
final public void testContains() {
map.clear();
map.put("jhgfd", "mnbv");
assertEquals(true, map.keySet().contains("jhgfd"));
map.clear();
assertEquals(false, map.keySet().contains("jhgfd"));
}
final public void testContainsAll() {
map.clear();
map.put("1", "1");
map.put("2", "1");
map.put("3", "1");
List coll = new ArrayList();
coll.add("1");
coll.add("2");
assertEquals(true, map.keySet().containsAll(coll));
coll.add("3");
assertEquals(true, map.keySet().containsAll(coll));
coll.add("4");
assertEquals(false, map.keySet().containsAll(coll));
}
final public void testIsEmpty() {
map.clear();
assertEquals(true, map.keySet().isEmpty());
map.put("1", "2");
assertEquals(false, map.keySet().isEmpty());
}
final public void testIterator() {
map.clear();
map.put("1", "2");
map.put("2", "2");
Iterator iter = map.keySet().iterator();
while(iter.hasNext()) {
Object obj = iter.next();
if(obj.equals("1")) iter.remove();
}
assertEquals(true, map.containsKey("2"));
assertEquals(false, map.containsKey("1"));
}
final public void testRemove() {
map.clear();
map.put("1", "2");
map.put("2", "2");
map.put("3", "2");
map.keySet().remove("2");
assertEquals(false, map.containsKey("2"));
}
final public void testRemoveAll() {
map.clear();
map.put("1", "2");
map.put("2", "2");
map.put("3", "2");
List list = new ArrayList();
list.add("1");
list.add("2");
map.keySet().removeAll(list);
assertEquals(false, map.containsKey("1"));
assertEquals(false, map.containsKey("2"));
assertEquals(true, map.containsKey("3"));
}
final public void testRetainAll() {
map.clear();
map.put("1", "2");
map.put("2", "2");
map.put("3", "2");
List list = new ArrayList();
list.add("1");
list.add("2");
map.keySet().retainAll(list);
assertEquals(true, map.containsKey("1"));
assertEquals(true, map.containsKey("2"));
assertEquals(false, map.containsKey("3"));
}
final public void testSize() {
map.clear();
assertEquals(0, map.keySet().size());
map.put("1","1");
map.put("2","1");
map.put("3","1");
assertEquals(3, map.keySet().size());
map.clear();
assertEquals(0, map.keySet().size());
}
/*
* Test for Object[] toArray()
*/
final public void testToArray() {
map.clear();
String onoe = "1";
map.put(onoe,onoe);
String two = "2";
map.put(two,onoe);
String three = "3";
map.put(three,onoe);
Object[] objArr = map.keySet().toArray();
List test = new ArrayList();
test.add(onoe);
test.add(two);
test.add(three);
for (int i = 0; i < objArr.length; i++) {
Object object = objArr[i];
assertEquals(true, test.contains(object));
test.remove(object);
}
assertEquals(true, test.isEmpty());
}
/*
* Test for Object[] toArray(Object[])
*/
final public void testToArrayObjectArray() {
String[] res = new String[4];
String one = "1";
map.put(one,one);
String two = "2";
map.put(two,one);
String three = "3";
map.put(three,one);
String four = "4";
map.put(four, one);
Object[] objArr = map.keySet().toArray(res);
List test = new ArrayList();
test.add(one);
test.add(two);
test.add(three);
test.add(four);
for (int i = 0; i < objArr.length; i++) {
Object object = objArr[i];
assertEquals(true, test.contains(object));
test.remove(object);
}
assertEquals(true, test.isEmpty());
assertSame(objArr, res);
}
}