Author: ron.sigal(a)jboss.com
Date: 2010-08-31 11:50:46 -0400 (Tue, 31 Aug 2010)
New Revision: 6080
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CopyOnWriteHashMapTestCase.java
Log:
JBREM-1244: New unit test.
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CopyOnWriteHashMapTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CopyOnWriteHashMapTestCase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/CopyOnWriteHashMapTestCase.java 2010-08-31
15:50:46 UTC (rev 6080)
@@ -0,0 +1,246 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.jboss.remoting3.test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import org.jboss.remoting3.CopyOnWriteHashMapWrapper;
+import org.jboss.xnio.log.Logger;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ *
+ * Copyright Aug 28, 2010
+ */
+@Test(suiteName = "CopyOnWriteHashMap")
+public class CopyOnWriteHashMapTestCase extends RemotingTestBase {
+
+ private static final Logger log = Logger.getLogger(CopyOnWriteHashMapTestCase.class);
+
+ @BeforeMethod
+ public void setUp() {
+ }
+
+ @AfterMethod
+ public void tearDown() throws IOException {
+ }
+
+ @Test
+ public void testIdentityFalse() throws Exception {
+ enter();
+
+ try {
+ doTest(false);
+ log.info(getName() + " PASSES");
+ } finally {
+ exit();
+ }
+ }
+
+ @Test
+ public void testIdentityTrue() throws Exception {
+ enter();
+
+ try {
+ doTest(true);
+ log.info(getName() + " PASSES");
+ } finally {
+ exit();
+ }
+ }
+
+ protected void doTest(boolean identity) {
+ CopyOnWriteHashMapWrapper<Object, Object> map = new
CopyOnWriteHashMapWrapper<Object, Object>(identity, new Object());
+
+ // Test putIfAbsent().
+ try {
+ map.putIfAbsent(null, new Object());
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ log.info("got expected NullPointerException");
+ } catch (Exception e) {
+ fail("expected NullPointerException, got " + e.getClass() +
"(" + e.getMessage() + ")");
+ }
+ try {
+ map.putIfAbsent(new Object(), null);
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ log.info("got expected NullPointerException");
+ } catch (Exception e) {
+ fail("expected NullPointerException, got " + e.getClass() +
"(" + e.getMessage() + ")");
+ }
+ Object key1 = new Object();
+ Object value1 = new Object();
+ assertNull(map.putIfAbsent(key1, value1)); // map = {key1=value1}
+ Object value2 = new Object();
+ assertEquals(value1, map.putIfAbsent(key1, value2)); // map = {key1=value1}
+
+ // Test remove().
+ assertFalse(map.remove(null, new Object()));
+ assertFalse(map.remove(new Object(), null));
+ assertFalse(map.remove(key1, value2)); // map = {key1=value1}
+ assertTrue(map.remove(key1, value1)); // map = {}
+ assertNull(map.putIfAbsent(key1, value1)); // map = {key1=value1}
+ Object key2 = new Object();
+ assertFalse(map.remove(key2, value1)); // map = {key1=value1}
+
+ // Test replace(final K key, final V oldValue, final V newValue).
+ assertFalse(map.replace(null, new Object(), new Object()));
+ assertFalse(map.replace(new Object(), null, new Object()));
+ try {
+ map.replace(new Object(), new Object(), null);
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ log.info("got expected NullPointerException");
+ } catch (Exception e) {
+ fail("expected NullPointerException, got " + e.getClass() +
"(" + e.getMessage() + ")");
+ }
+ assertFalse(map.replace(key2, value1, value2)); // map = {key1=value1}
+ assertFalse(map.replace(key1, value2, value2)); // map = {key1=value1}
+ assertTrue(map.replace(key1, value1, value2)); // map = {key1=value2}
+
+ // Test replace(final K key, final V value).
+ assertNull(map.replace(null, new Object()));
+ try {
+ map.replace(new Object(), null);
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ log.info("got expected NullPointerException");
+ } catch (Exception e) {
+ fail("expected NullPointerException, got " + e.getClass() +
"(" + e.getMessage() + ")");
+ }
+ assertNull(map.replace(key2, value1)); // map = {key1=value2}
+ assertEquals(value2, map.replace(key1, value1)); // map = {key1=value1}
+
+ // Test size().
+ assertEquals(1, map.size());
+
+ // Test isEmpty().
+ assertFalse(map.isEmpty());
+ map.remove(key1); // map = {}
+ assertTrue(map.isEmpty());
+
+ // Test containsKey().
+ assertFalse(map.containsKey(key1));
+ map.putIfAbsent(key1, value1); // map = {key1=value1}
+ assertTrue(map.containsKey(key1));
+
+ // Test containsValue().
+ assertFalse(map.containsValue(value2));
+ assertTrue(map.containsValue(value1));
+
+ // Test get().
+ assertNull(map.get(key2));
+ assertEquals(value1, map.get(key1));
+
+ // Test put().
+ try {
+ map.put(null, new Object());
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ log.info("got expected NullPointerException");
+ } catch (Exception e) {
+ fail("expected NullPointerException, got " + e.getClass() +
"(" + e.getMessage() + ")");
+ }
+ try {
+ map.put(new Object(), null);
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ log.info("got expected NullPointerException");
+ } catch (Exception e) {
+ fail("expected NullPointerException, got " + e.getClass() +
"(" + e.getMessage() + ")");
+ }
+ assertNull(map.put(key2, value2)); // map = {key1=value1, key2=value2}
+ assertEquals(value2, map.put(key2, value1)); // map = {key1=value1, key2=value1}
+
+ // Test remove().
+ assertNull(map.remove(null));
+ assertEquals(value1, map.remove(key2)); // map = {key1=value1}
+ assertNull(map.remove(key2));
+
+ // Test putAll().
+ try {
+ map.putAll(null);
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ log.info("got expected NullPointerException");
+ } catch (Exception e) {
+ fail("expected NullPointerException, got " + e.getClass() +
"(" + e.getMessage() + ")");
+ }
+ CopyOnWriteHashMapWrapper<Object, Object> map2 = new
CopyOnWriteHashMapWrapper<Object, Object>(identity, new Object());
+ Object key3 = new Object();
+ Object value3 = new Object();
+ Object key4 = new Object();
+ Object value4 = new Object();
+ map2.put(key3, value3);
+ map2.put(key4, value4);
+ map.putAll(map2); // map = {key1=value1, key3=value3, key4=value4}
+ assertEquals(3, map.size());
+ assertEquals(value1, map.get(key1));
+ assertEquals(value3, map.get(key3));
+ assertEquals(value4, map.get(key4));
+
+ // Test clear().
+ map.clear(); // map = {}
+ assertEquals(0, map.size());
+
+ // Test keySet().
+ map.putAll(map2); // map = {key3=value3, key4=value4}
+ Set<Object> keySet = map.keySet();
+ assertEquals(2, keySet.size());
+ assertTrue(keySet.contains(key3));
+ assertTrue(keySet.contains(key4));
+
+ // Test values().
+ Collection<Object> values = map.values();
+ assertEquals(2, values.size());
+ assertTrue(values.contains(value3));
+ assertTrue(values.contains(value4));
+
+ // Test entrySet().
+ Set<Entry<Object, Object>> entrySet = map.entrySet();
+ assertEquals(2, entrySet.size());
+ Iterator<Map.Entry<Object, Object>> it = entrySet.iterator();
+ while (it.hasNext()) {
+ Map.Entry<Object, Object> entry = it.next();
+ Object key = entry.getKey();
+ Object value = entry.getValue();
+ assertTrue(key == key3 && value == value3 || key == key4 &&
value == value4);
+ }
+ }
+}