From jboss-remoting-commits at lists.jboss.org Tue Aug 31 11:50:47 2010 Content-Type: multipart/mixed; boundary="===============0704920001811684689==" MIME-Version: 1.0 From: jboss-remoting-commits at lists.jboss.org To: jboss-remoting-commits at lists.jboss.org Subject: [jboss-remoting-commits] JBoss Remoting SVN: r6080 - remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test. Date: Tue, 31 Aug 2010 11:50:46 -0400 Message-ID: <201008311550.o7VFokXS000496@svn01.web.mwc.hst.phx2.redhat.com> --===============0704920001811684689== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/Co= pyOnWriteHashMapTestCase.java Log: JBREM-1244: New unit test. Added: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/tes= t/CopyOnWriteHashMapTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/C= opyOnWriteHashMapTestCase.java (rev 0) +++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/C= opyOnWriteHashMapTestCase.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 Ron Sigal + * @version $Revision: 1.1 $ + * + * Copyright Aug 28, 2010 + */ +(a)Test(suiteName =3D "CopyOnWriteHashMap") +public class CopyOnWriteHashMapTestCase extends RemotingTestBase { + + private static final Logger log =3D Logger.getLogger(CopyOnWriteHashMap= TestCase.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 map =3D new CopyOnWriteHas= hMapWrapper(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 =3D new Object(); + Object value1 =3D new Object(); + assertNull(map.putIfAbsent(key1, value1)); // map =3D {key1=3Dvalue1} + Object value2 =3D new Object(); + assertEquals(value1, map.putIfAbsent(key1, value2)); // map =3D {key= 1=3Dvalue1} + = + // Test remove(). + assertFalse(map.remove(null, new Object())); + assertFalse(map.remove(new Object(), null)); + assertFalse(map.remove(key1, value2)); // map =3D {key1=3Dvalue1} + assertTrue(map.remove(key1, value1)); // map =3D {} + assertNull(map.putIfAbsent(key1, value1)); // map =3D {key1=3Dvalue1} + Object key2 =3D new Object(); + assertFalse(map.remove(key2, value1)); // map =3D {key1=3Dvalue1} + = + // 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 =3D {key1=3Dv= alue1} + assertFalse(map.replace(key1, value2, value2)); // map =3D {key1=3Dv= alue1} + assertTrue(map.replace(key1, value1, value2)); // map =3D {key1=3Dva= lue2} + = + // 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 =3D {key1=3Dvalue2} + assertEquals(value2, map.replace(key1, value1)); // map =3D {key1=3D= value1} + = + // Test size(). + assertEquals(1, map.size()); + = + // Test isEmpty(). + assertFalse(map.isEmpty()); + map.remove(key1); // map =3D {} + assertTrue(map.isEmpty()); + = + // Test containsKey(). + assertFalse(map.containsKey(key1)); + map.putIfAbsent(key1, value1); // map =3D {key1=3Dvalue1} + 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 =3D {key1=3Dvalue1, key2= =3Dvalue2} + assertEquals(value2, map.put(key2, value1)); // map =3D {key1=3Dvalu= e1, key2=3Dvalue1} + = + // Test remove(). + assertNull(map.remove(null)); + assertEquals(value1, map.remove(key2)); // map =3D {key1=3Dvalue1} + 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 map2 =3D new CopyOnWriteHa= shMapWrapper(identity, new Object()); + Object key3 =3D new Object(); + Object value3 =3D new Object(); + Object key4 =3D new Object(); + Object value4 =3D new Object(); + map2.put(key3, value3); + map2.put(key4, value4); + map.putAll(map2); // map =3D {key1=3Dvalue1, key3=3Dvalue3, key4=3Dv= alue4} + assertEquals(3, map.size()); + assertEquals(value1, map.get(key1)); + assertEquals(value3, map.get(key3)); + assertEquals(value4, map.get(key4)); + = + // Test clear(). + map.clear(); // map =3D {} + assertEquals(0, map.size()); + = + // Test keySet(). + map.putAll(map2); // map =3D {key3=3Dvalue3, key4=3Dvalue4} + Set keySet =3D map.keySet(); + assertEquals(2, keySet.size()); + assertTrue(keySet.contains(key3)); + assertTrue(keySet.contains(key4)); + = + // Test values(). + Collection values =3D map.values(); + assertEquals(2, values.size()); + assertTrue(values.contains(value3)); + assertTrue(values.contains(value4)); + = + // Test entrySet(). + Set> entrySet =3D map.entrySet(); + assertEquals(2, entrySet.size()); + Iterator> it =3D entrySet.iterator(); + while (it.hasNext()) { + Map.Entry entry =3D it.next(); + Object key =3D entry.getKey(); + Object value =3D entry.getValue(); + assertTrue(key =3D=3D key3 && value =3D=3D value3 || key =3D=3D k= ey4 && value =3D=3D value4); + } + } +} --===============0704920001811684689==--