Author: ron.sigal(a)jboss.com
Date: 2010-08-30 22:49:18 -0400 (Mon, 30 Aug 2010)
New Revision: 6076
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/AttachmentsTestCase.java
Log:
JBREM-1228: New unit test for org.jboss.remoting3.AttachmentsImpl.
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/AttachmentsTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/AttachmentsTestCase.java
(rev 0)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/AttachmentsTestCase.java 2010-08-31
02:49:18 UTC (rev 6076)
@@ -0,0 +1,135 @@
+/*
+ * 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.assertFalse;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.jboss.remoting3.Attachments;
+import org.jboss.remoting3.Endpoint;
+import org.jboss.remoting3.Remoting;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.log.Logger;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Tests org.jboss.remoting3.AttachmentsImpl.
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright August 27, 2010
+ */
+@Test(suiteName = "Attachments")
+public class AttachmentsTestCase extends RemotingTestBase {
+
+ private static final Logger log = Logger.getLogger(AttachmentsTestCase.class);
+
+ @BeforeMethod
+ public void setUp() {
+ }
+
+ @AfterMethod
+ public void tearDown() throws IOException {
+ }
+
+ @Test
+ public void testAttachments() throws Exception {
+ enter();
+
+ try {
+ Executor executor = new ThreadPoolExecutor(8, 64, 30, TimeUnit.SECONDS, new
ArrayBlockingQueue<Runnable>(64));
+ Endpoint endpoint = Remoting.createEndpoint("attachments", executor,
OptionMap.EMPTY);
+ Attachments attachments = endpoint.getAttachments();
+ Attachments.Key<Object> key1 = new
Attachments.Key<Object>(Object.class);
+ Object value1 = new Object();
+ attachments.attach(key1, value1);
+ assertEquals(value1, attachments.getAttachment(key1));
+
+ Object value2 = new Object();
+ attachments.attachIfAbsent(key1, value2);
+ assertEquals(value1, attachments.getAttachment(key1));
+ Attachments.Key<Object> key2 = new
Attachments.Key<Object>(Object.class);
+ attachments.attachIfAbsent(key2, value2);
+ assertEquals(value2, attachments.getAttachment(key2));
+
+ assertFalse(attachments.replaceAttachment(key1, value2, value2));
+ assertEquals(value1, attachments.getAttachment(key1));
+
+ assertTrue(attachments.replaceAttachment(key1, value1, value2));
+ assertEquals(value2, attachments.getAttachment(key1));
+
+ Attachments.Key<Object> key3 = new
Attachments.Key<Object>(Object.class);
+ Object value3 = new ComparableObject(3);
+ attachments.attach(key3, value3);
+ Object value4 = new ComparableObject(7);
+ assertFalse(attachments.replaceAttachment(key3, value4, value3));
+ assertEquals(value3, attachments.getAttachment(key3));
+ assertTrue(attachments.replaceAttachment(key3, new ComparableObject(3),
value4));
+ assertEquals(value4, attachments.getAttachment(key3));
+
+ assertEquals(value2, attachments.removeAttachment(key1));
+ assertNull(attachments.getAttachment(key1));
+
+ attachments.attach(key1, value1);
+ assertFalse(attachments.removeAttachment(key1, value2));
+ assertEquals(value1, attachments.getAttachment(key1));
+ assertTrue(attachments.removeAttachment(key1, value1));
+ assertNull(attachments.getAttachment(key1));
+
+ assertFalse(attachments.removeAttachment(key3, value3));
+ assertEquals(value4, attachments.getAttachment(key3));
+ assertTrue(attachments.removeAttachment(key3, new ComparableObject(7)));
+ assertNull(attachments.getAttachment(key3));
+
+ log.info(getName() + " PASSES");
+ } finally {
+ exit();
+ }
+ }
+
+ static class ComparableObject {
+ private final int secret;
+
+ public ComparableObject(int secret) {
+ this.secret = secret;
+ }
+
+ public boolean equals(Object other) {
+ if (! (other instanceof ComparableObject)) {
+ return false;
+ }
+ return secret == ((ComparableObject) other).secret;
+ }
+ }
+}