You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2012/11/15 01:50:02 UTC
svn commit: r1409624 - in /activemq/trunk:
activemq-client/src/test/java/org/apache/activemq/thread/
activemq-client/src/test/java/org/apache/activemq/util/
activemq-core/src/test/java/org/apache/activemq/thread/
activemq-core/src/test/java/org/apache/...
Author: tabish
Date: Thu Nov 15 00:50:01 2012
New Revision: 1409624
URL: http://svn.apache.org/viewvc?rev=1409624&view=rev
Log:
move more tests specific to activemq-client to that module from activemq-core
Added:
activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/
activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java (with props)
activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/TaskRunnerTest.java (with props)
activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/IntrospectionSupportTest.java (with props)
activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java (with props)
activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/StopWatchTest.java (with props)
activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/URISupportTest.java (with props)
Removed:
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/thread/
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/util/IntrospectionSupportTest.java
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/util/StopWatchTest.java
activemq/trunk/activemq-core/src/test/java/org/apache/activemq/util/URISupportTest.java
Added: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java?rev=1409624&view=auto
==============================================================================
--- activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java (added)
+++ activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java Thu Nov 15 00:50:01 2012
@@ -0,0 +1,165 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.thread;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.TestCase;
+
+public class PooledTaskRunnerTest extends TestCase {
+ private ExecutorService executor;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ executor = Executors.newCachedThreadPool(new IgnoreUncaughtExceptionThreadFactory());
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ executor.shutdownNow();
+
+ super.tearDown();
+ }
+
+ public void testNormalBehavior() throws Exception {
+ final CountDownLatch latch = new CountDownLatch( 1 );
+
+ PooledTaskRunner runner = new PooledTaskRunner( executor, new Task() {
+ public boolean iterate() {
+ latch.countDown();
+
+ return false;
+ }
+ }, 1 );
+
+ runner.wakeup();
+
+ assertTrue( latch.await( 1, TimeUnit.SECONDS ) );
+
+ runner.shutdown();
+ }
+
+
+ public void testWakeupResultsInThreadSafeCalls() throws Exception {
+
+ ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 10, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() {
+ public Thread newThread(Runnable runnable) {
+ Thread thread = new Thread(runnable, getName());
+ thread.setDaemon(true);
+ thread.setPriority(Thread.NORM_PRIORITY);
+ return thread;
+ }
+ });
+ final CountDownLatch doneLatch = new CountDownLatch( 100 );
+ final AtomicInteger clashCount = new AtomicInteger();
+ final AtomicInteger count = new AtomicInteger();
+
+
+ final PooledTaskRunner runner = new PooledTaskRunner(executor, new Task() {
+ String threadUnSafeVal = null;
+ public boolean iterate() {
+ if (threadUnSafeVal != null) {
+ clashCount.incrementAndGet();
+ }
+ threadUnSafeVal = Thread.currentThread().getName();
+ count.incrementAndGet();
+ doneLatch.countDown();
+ if (!threadUnSafeVal.equals(Thread.currentThread().getName())) {
+ clashCount.incrementAndGet();
+ }
+ threadUnSafeVal = null;
+ return false;
+ }
+ }, 1 );
+
+ Runnable doWakeup = new Runnable() {
+ public void run() {
+ try {
+ runner.wakeup();
+ } catch (InterruptedException ignored) {
+ }
+ }
+ };
+
+ final int iterations = 1000;
+ for (int i=0; i< iterations; i++) {
+ if (i%100 == 0) {
+ Thread.sleep(10);
+ }
+ executor.execute(doWakeup);
+ }
+
+ doneLatch.await(20, TimeUnit.SECONDS);
+ assertEquals("thread safety clash", 0, clashCount.get());
+ assertTrue("called more than once", count.get() > 1);
+ runner.shutdown();
+ }
+
+ public void testShutsDownAfterRunnerFailure() throws Exception {
+ Future<Object> future = executor.submit( new Callable<Object>() {
+ public Object call() throws Exception {
+ final CountDownLatch latch = new CountDownLatch( 1 );
+
+ PooledTaskRunner runner = new PooledTaskRunner( executor, new Task() {
+ public boolean iterate() {
+ latch.countDown();
+
+ throw new RuntimeException();
+ }
+ }, 1 );
+
+ runner.wakeup();
+
+ assertTrue( latch.await( 1, TimeUnit.SECONDS ) );
+
+ runner.shutdown();
+
+ return null;
+ }
+ } );
+
+ try {
+ future.get( 5, TimeUnit.SECONDS );
+ } catch( TimeoutException e ) {
+ fail( "TaskRunner did not shut down cleanly" );
+ }
+ }
+
+ class IgnoreUncaughtExceptionThreadFactory implements ThreadFactory, Thread.UncaughtExceptionHandler {
+ ThreadFactory threadFactory = Executors.defaultThreadFactory();
+ public Thread newThread(Runnable r) {
+ Thread thread = threadFactory.newThread(r);
+ thread.setUncaughtExceptionHandler(this);
+ return thread;
+ }
+
+ public void uncaughtException(Thread t, Throwable e) {
+ // ignore ie: no printStackTrace that would sully the test console
+ }
+ }
+}
\ No newline at end of file
Propchange: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/TaskRunnerTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/TaskRunnerTest.java?rev=1409624&view=auto
==============================================================================
--- activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/TaskRunnerTest.java (added)
+++ activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/TaskRunnerTest.java Thu Nov 15 00:50:01 2012
@@ -0,0 +1,112 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.thread;
+
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.TestCase;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TaskRunnerTest extends TestCase {
+ private static final Logger LOG = LoggerFactory.getLogger(TaskRunnerTest.class);
+
+ public void testWakeupPooled() throws InterruptedException, BrokenBarrierException {
+ System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "false");
+ doTestWakeup();
+ }
+
+ public void testWakeupDedicated() throws InterruptedException, BrokenBarrierException {
+ System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "true");
+ doTestWakeup();
+ }
+
+ /**
+ * Simulate multiple threads queuing work for the TaskRunner. The Task
+ * Runner dequeues the work.
+ *
+ * @throws InterruptedException
+ * @throws BrokenBarrierException
+ */
+ public void doTestWakeup() throws InterruptedException, BrokenBarrierException {
+
+ final int enqueueCount = 100000;
+ final AtomicInteger iterations = new AtomicInteger(0);
+ final AtomicInteger counter = new AtomicInteger(0);
+ final AtomicInteger queue = new AtomicInteger(0);
+ final CountDownLatch doneCountDownLatch = new CountDownLatch(1);
+
+ TaskRunnerFactory factory = new TaskRunnerFactory();
+ final TaskRunner runner = factory.createTaskRunner(new Task() {
+ public boolean iterate() {
+ if (queue.get() == 0) {
+ return false;
+ } else {
+ while (queue.get() > 0) {
+ queue.decrementAndGet();
+ counter.incrementAndGet();
+ }
+ iterations.incrementAndGet();
+ if (counter.get() == enqueueCount) {
+ doneCountDownLatch.countDown();
+ }
+ return true;
+ }
+ }
+ }, "Thread Name");
+
+ long start = System.currentTimeMillis();
+ final int workerCount = 5;
+ final CyclicBarrier barrier = new CyclicBarrier(workerCount + 1);
+ for (int i = 0; i < workerCount; i++) {
+ new Thread() {
+ public void run() {
+ try {
+ barrier.await();
+ for (int i = 0; i < enqueueCount / workerCount; i++) {
+ queue.incrementAndGet();
+ runner.wakeup();
+ yield();
+ }
+ } catch (BrokenBarrierException e) {
+ } catch (InterruptedException e) {
+ }
+ }
+ }.start();
+ }
+ barrier.await();
+
+ boolean b = doneCountDownLatch.await(30, TimeUnit.SECONDS);
+ long end = System.currentTimeMillis();
+ LOG.info("Iterations: " + iterations.get());
+ LOG.info("counter: " + counter.get());
+ LOG.info("Dequeues/s: " + (1000.0 * enqueueCount / (end - start)));
+ LOG.info("duration: " + ((end - start) / 1000.0));
+ assertTrue(b);
+
+ runner.shutdown();
+ }
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(TaskRunnerTest.class);
+ }
+
+}
Propchange: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/TaskRunnerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/thread/TaskRunnerTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/IntrospectionSupportTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/IntrospectionSupportTest.java?rev=1409624&view=auto
==============================================================================
--- activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/IntrospectionSupportTest.java (added)
+++ activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/IntrospectionSupportTest.java Thu Nov 15 00:50:01 2012
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.util;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="http://www.christianposta.com/blog">Christian Posta</a>
+ */
+public class IntrospectionSupportTest extends TestCase {
+
+ class DummyClass {
+ private boolean trace;
+
+ DummyClass(boolean trace) {
+ this.trace = trace;
+ }
+
+ public boolean isTrace() {
+ return trace;
+ }
+
+ public void setTrace(boolean trace) {
+ this.trace = trace;
+ }
+ }
+
+ public void testSetPropertyPrimitiveWithWrapperValue() {
+ // Wrapper value
+ Boolean value = Boolean.valueOf(true);
+
+ DummyClass dummyClass = new DummyClass(false);
+
+ // dummy field expects a primitive
+ IntrospectionSupport.setProperty(dummyClass, "trace", value);
+
+ assertTrue(dummyClass.isTrace());
+ }
+}
Propchange: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/IntrospectionSupportTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java?rev=1409624&view=auto
==============================================================================
--- activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java (added)
+++ activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java Thu Nov 15 00:50:01 2012
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.util;
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+/**
+ * @author rajdavies
+ */
+public class MarshallingSupportTest extends TestCase {
+
+ /**
+ * @throws java.lang.Exception
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /**
+ * @throws java.lang.Exception
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.activemq.util.MarshallingSupport#propertiesToString(java.util.Properties)}.
+ *
+ * @throws Exception
+ */
+ public void testPropertiesToString() throws Exception {
+ Properties props = new Properties();
+ for (int i = 0; i < 10; i++) {
+ String key = "key" + i;
+ String value = "value" + i;
+ props.put(key, value);
+ }
+ String str = MarshallingSupport.propertiesToString(props);
+ Properties props2 = MarshallingSupport.stringToProperties(str);
+ assertEquals(props, props2);
+ }
+}
Propchange: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/StopWatchTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/StopWatchTest.java?rev=1409624&view=auto
==============================================================================
--- activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/StopWatchTest.java (added)
+++ activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/StopWatchTest.java Thu Nov 15 00:50:01 2012
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.util;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ */
+public class StopWatchTest extends TestCase {
+
+ public void testStopWatch() throws Exception {
+ StopWatch watch = new StopWatch();
+ Thread.sleep(200);
+ long taken = watch.stop();
+
+ assertEquals(taken, watch.taken());
+ assertTrue("Should take approx 200 millis, was: " + taken, taken > 150);
+ }
+
+ public void testStopWatchNotStarted() throws Exception {
+ StopWatch watch = new StopWatch(false);
+ long taken = watch.stop();
+ assertEquals(0, taken);
+
+ watch.restart();
+ Thread.sleep(200);
+ taken = watch.stop();
+
+ assertEquals(taken, watch.taken());
+ assertTrue("Should take approx 200 millis, was: " + taken, taken > 150);
+ }
+
+ public void testStopWatchRestart() throws Exception {
+ StopWatch watch = new StopWatch();
+ Thread.sleep(200);
+ long taken = watch.stop();
+
+ assertEquals(taken, watch.taken());
+ assertTrue("Should take approx 200 millis, was: " + taken, taken > 150);
+
+ watch.restart();
+ Thread.sleep(100);
+ taken = watch.stop();
+
+ assertEquals(taken, watch.taken());
+ assertTrue("Should take approx 100 millis, was: " + taken, taken > 50);
+ }
+
+ public void testStopWatchTaken() throws Exception {
+ StopWatch watch = new StopWatch();
+ Thread.sleep(100);
+ long taken = watch.taken();
+ Thread.sleep(100);
+ long taken2 = watch.taken();
+ assertNotSame(taken, taken2);
+ assertTrue(taken2 > taken);
+ }
+}
\ No newline at end of file
Propchange: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/StopWatchTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/URISupportTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/URISupportTest.java?rev=1409624&view=auto
==============================================================================
--- activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/URISupportTest.java (added)
+++ activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/URISupportTest.java Thu Nov 15 00:50:01 2012
@@ -0,0 +1,214 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.util;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.activemq.util.URISupport.CompositeData;
+
+public class URISupportTest extends TestCase {
+
+ public void testEmptyCompositePath() throws Exception {
+ CompositeData data = URISupport.parseComposite(new URI("broker:()/localhost?persistent=false"));
+ assertEquals(0, data.getComponents().length);
+ }
+
+ public void testCompositePath() throws Exception {
+ CompositeData data = URISupport.parseComposite(new URI("test:(path)/path"));
+ assertEquals("path", data.getPath());
+ data = URISupport.parseComposite(new URI("test:path"));
+ assertNull(data.getPath());
+ }
+
+ public void testSimpleComposite() throws Exception {
+ CompositeData data = URISupport.parseComposite(new URI("test:part1"));
+ assertEquals(1, data.getComponents().length);
+ }
+
+ public void testComposite() throws Exception {
+ URI uri = new URI("test:(part1://host,part2://(sub1://part,sube2:part))");
+ CompositeData data = URISupport.parseComposite(uri);
+ assertEquals(2, data.getComponents().length);
+ }
+
+ public void testEmptyCompositeWithParenthesisInParam() throws Exception {
+ URI uri = new URI("failover://()?updateURIsURL=file:/C:/Dir(1)/a.csv");
+ CompositeData data = URISupport.parseComposite(uri);
+ assertEquals(0, data.getComponents().length);
+ assertEquals(1, data.getParameters().size());
+ assertTrue(data.getParameters().containsKey("updateURIsURL"));
+ assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL"));
+ }
+
+ public void testCompositeWithParenthesisInParam() throws Exception {
+ URI uri = new URI("failover://(test)?updateURIsURL=file:/C:/Dir(1)/a.csv");
+ CompositeData data = URISupport.parseComposite(uri);
+ assertEquals(1, data.getComponents().length);
+ assertEquals(1, data.getParameters().size());
+ assertTrue(data.getParameters().containsKey("updateURIsURL"));
+ assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL"));
+ }
+
+ public void testCompositeWithComponentParam() throws Exception {
+ CompositeData data = URISupport.parseComposite(new URI("test:(part1://host?part1=true)?outside=true"));
+ assertEquals(1, data.getComponents().length);
+ assertEquals(1, data.getParameters().size());
+ Map<String, String> part1Params = URISupport.parseParameters(data.getComponents()[0]);
+ assertEquals(1, part1Params.size());
+ assertTrue(part1Params.containsKey("part1"));
+ }
+
+ public void testParsingURI() throws Exception {
+ URI source = new URI("tcp://localhost:61626/foo/bar?cheese=Edam&x=123");
+
+ Map<String, String> map = URISupport.parseParameters(source);
+
+ assertEquals("Size: " + map, 2, map.size());
+ assertMapKey(map, "cheese", "Edam");
+ assertMapKey(map, "x", "123");
+
+ URI result = URISupport.removeQuery(source);
+
+ assertEquals("result", new URI("tcp://localhost:61626/foo/bar"), result);
+ }
+
+ protected void assertMapKey(Map<String, String> map, String key, Object expected) {
+ assertEquals("Map key: " + key, map.get(key), expected);
+ }
+
+ public void testParsingCompositeURI() throws URISyntaxException {
+ CompositeData data = URISupport.parseComposite(new URI("broker://(tcp://localhost:61616)?name=foo"));
+ assertEquals("one component", 1, data.getComponents().length);
+ assertEquals("Size: " + data.getParameters(), 1, data.getParameters().size());
+ }
+
+ public void testCheckParenthesis() throws Exception {
+ String str = "fred:(((ddd))";
+ assertFalse(URISupport.checkParenthesis(str));
+ str += ")";
+ assertTrue(URISupport.checkParenthesis(str));
+ }
+
+ public void testCreateWithQuery() throws Exception {
+ URI source = new URI("vm://localhost");
+ URI dest = URISupport.createURIWithQuery(source, "network=true&one=two");
+
+ assertEquals("correct param count", 2, URISupport.parseParameters(dest).size());
+ assertEquals("same uri, host", source.getHost(), dest.getHost());
+ assertEquals("same uri, scheme", source.getScheme(), dest.getScheme());
+ assertFalse("same uri, ssp", dest.getQuery().equals(source.getQuery()));
+ }
+
+ public void testParsingParams() throws Exception {
+ URI uri = new URI("static:(http://localhost:61617?proxyHost=jo&proxyPort=90)?proxyHost=localhost&proxyPort=80");
+ Map<String,String>parameters = URISupport.parseParameters(uri);
+ verifyParams(parameters);
+ uri = new URI("static://http://localhost:61617?proxyHost=localhost&proxyPort=80");
+ parameters = URISupport.parseParameters(uri);
+ verifyParams(parameters);
+ uri = new URI("http://0.0.0.0:61616");
+ parameters = URISupport.parseParameters(uri);
+ }
+
+ public void testCompositeCreateURIWithQuery() throws Exception {
+ String queryString = "query=value";
+ URI originalURI = new URI("outerscheme:(innerscheme:innerssp)");
+ URI querylessURI = originalURI;
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
+ assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
+ originalURI = new URI("outerscheme:(innerscheme:innerssp)?outerquery=0");
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
+ assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
+ originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)");
+ querylessURI = originalURI;
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
+ assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
+ originalURI = new URI("outerscheme:(innerscheme:innerssp?innerquery=0)?outerquery=0");
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, null));
+ assertEquals(querylessURI, URISupport.createURIWithQuery(originalURI, ""));
+ assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
+ }
+
+ public void testApplyParameters() throws Exception {
+
+ URI uri = new URI("http://0.0.0.0:61616");
+ Map<String,String> parameters = new HashMap<String, String>();
+ parameters.put("t.proxyHost", "localhost");
+ parameters.put("t.proxyPort", "80");
+
+ uri = URISupport.applyParameters(uri, parameters);
+ Map<String,String> appliedParameters = URISupport.parseParameters(uri);
+ assertEquals("all params applied with no prefix", 2, appliedParameters.size());
+
+ // strip off params again
+ uri = URISupport.createURIWithQuery(uri, null);
+
+ uri = URISupport.applyParameters(uri, parameters, "joe");
+ appliedParameters = URISupport.parseParameters(uri);
+ assertTrue("no params applied as none match joe", appliedParameters.isEmpty());
+
+ uri = URISupport.applyParameters(uri, parameters, "t.");
+ verifyParams(URISupport.parseParameters(uri));
+ }
+
+ private void verifyParams(Map<String,String> parameters) {
+ assertEquals(parameters.get("proxyHost"), "localhost");
+ assertEquals(parameters.get("proxyPort"), "80");
+ }
+
+ public void testIsCompositeURIWithQueryNoSlashes() throws URISyntaxException {
+ URI[] compositeURIs = new URI[] { new URI("test:(part1://host?part1=true)?outside=true"), new URI("broker:(tcp://localhost:61616)?name=foo") };
+ for (URI uri : compositeURIs) {
+ assertTrue(uri + " must be detected as composite URI", URISupport.isCompositeURI(uri));
+ }
+ }
+
+ public void testIsCompositeURIWithQueryAndSlashes() throws URISyntaxException {
+ URI[] compositeURIs = new URI[] { new URI("test://(part1://host?part1=true)?outside=true"), new URI("broker://(tcp://localhost:61616)?name=foo") };
+ for (URI uri : compositeURIs) {
+ assertTrue(uri + " must be detected as composite URI", URISupport.isCompositeURI(uri));
+ }
+ }
+
+ public void testIsCompositeURINoQueryNoSlashes() throws URISyntaxException {
+ URI[] compositeURIs = new URI[] { new URI("test:(part1://host,part2://(sub1://part,sube2:part))"), new URI("test:(path)/path") };
+ for (URI uri : compositeURIs) {
+ assertTrue(uri + " must be detected as composite URI", URISupport.isCompositeURI(uri));
+ }
+ }
+
+ public void testIsCompositeURINoQueryNoSlashesNoParentheses() throws URISyntaxException {
+ assertFalse("test:part1" + " must be detected as non-composite URI", URISupport.isCompositeURI(new URI("test:part1")));
+ }
+
+ public void testIsCompositeURINoQueryWithSlashes() throws URISyntaxException {
+ URI[] compositeURIs = new URI[] { new URI("failover://(tcp://bla:61616,tcp://bla:61617)"),
+ new URI("failover://(tcp://localhost:61616,ssl://anotherhost:61617)") };
+ for (URI uri : compositeURIs) {
+ assertTrue(uri + " must be detected as composite URI", URISupport.isCompositeURI(uri));
+ }
+ }
+
+}
Propchange: activemq/trunk/activemq-client/src/test/java/org/apache/activemq/util/URISupportTest.java
------------------------------------------------------------------------------
svn:eol-style = native