From 9445e93ae4ea518ee631a0c8f3a537c478c15b6c Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Thu, 16 Apr 2015 12:53:18 -0400 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-5685 Apply patch from Kevin Richards after adding proper license headers etc --- activemq-web/pom.xml | 6 + .../activemq/web/LocalBrokerFacade.java | 79 +++++++----- .../activemq/web/LocalBrokerFacadeTest.java | 114 ++++++++++++++++++ 3 files changed, 167 insertions(+), 32 deletions(-) create mode 100644 activemq-web/src/test/java/org/apache/activemq/web/LocalBrokerFacadeTest.java diff --git a/activemq-web/pom.xml b/activemq-web/pom.xml index 81d5c4efb1..790b0a511b 100755 --- a/activemq-web/pom.xml +++ b/activemq-web/pom.xml @@ -122,6 +122,7 @@ com.thoughtworks.xstream xstream + junit junit @@ -132,5 +133,10 @@ slf4j-log4j12 test + + org.mockito + mockito-core + test + \ No newline at end of file diff --git a/activemq-web/src/main/java/org/apache/activemq/web/LocalBrokerFacade.java b/activemq-web/src/main/java/org/apache/activemq/web/LocalBrokerFacade.java index 99e0cd8a57..50ef17c83f 100644 --- a/activemq-web/src/main/java/org/apache/activemq/web/LocalBrokerFacade.java +++ b/activemq-web/src/main/java/org/apache/activemq/web/LocalBrokerFacade.java @@ -29,48 +29,57 @@ import org.apache.activemq.broker.jmx.BrokerViewMBean; import org.apache.activemq.broker.jmx.ManagedRegionBroker; import org.apache.activemq.broker.jmx.ManagementContext; import org.apache.activemq.broker.region.Destination; +import org.apache.activemq.broker.region.DestinationFilter; import org.apache.activemq.broker.region.Queue; import org.apache.activemq.command.ActiveMQDestination; /** * An implementation of {@link BrokerFacade} which uses a local in JVM broker - * - * */ public class LocalBrokerFacade extends BrokerFacadeSupport { - private BrokerService brokerService; - public LocalBrokerFacade(BrokerService brokerService) { - this.brokerService = brokerService; - } + private final BrokerService brokerService; - public BrokerService getBrokerService() { - return brokerService; - } - public String getBrokerName() throws Exception { - return brokerService.getBrokerName(); - } - public Broker getBroker() throws Exception { - return brokerService.getBroker(); - } - public ManagementContext getManagementContext() { - return brokerService.getManagementContext(); - } - public BrokerViewMBean getBrokerAdmin() throws Exception { - return brokerService.getAdminView(); - } - public ManagedRegionBroker getManagedBroker() throws Exception { - BrokerView adminView = brokerService.getAdminView(); - if (adminView == null) { - return null; - } - return adminView.getBroker(); - } + public LocalBrokerFacade(BrokerService brokerService) { + this.brokerService = brokerService; + } + public BrokerService getBrokerService() { + return brokerService; + } + + @Override + public String getBrokerName() throws Exception { + return brokerService.getBrokerName(); + } + + public Broker getBroker() throws Exception { + return brokerService.getBroker(); + } + + @Override + public ManagementContext getManagementContext() { + return brokerService.getManagementContext(); + } + + @Override + public BrokerViewMBean getBrokerAdmin() throws Exception { + return brokerService.getAdminView(); + } + + public ManagedRegionBroker getManagedBroker() throws Exception { + BrokerView adminView = brokerService.getAdminView(); + if (adminView == null) { + return null; + } + return adminView.getBroker(); + } + + @Override public void purgeQueue(ActiveMQDestination destination) throws Exception { - Set destinations = getManagedBroker().getQueueRegion().getDestinations(destination); - for (Iterator i = destinations.iterator(); i.hasNext();) { - Destination dest = (Destination) i.next(); + Set destinations = getManagedBroker().getQueueRegion().getDestinations(destination); + for (Iterator i = destinations.iterator(); i.hasNext();) { + Destination dest = unwrap(i.next()); if (dest instanceof Queue) { Queue regionQueue = (Queue) dest; regionQueue.purge(); @@ -78,6 +87,13 @@ public class LocalBrokerFacade extends BrokerFacadeSupport { } } + private Destination unwrap(Destination dest) { + if (dest instanceof DestinationFilter) { + return unwrap(((DestinationFilter) dest).getNext()); + } + return dest; + } + @Override public Set queryNames(ObjectName name, QueryExp query) throws Exception { return getManagementContext().queryNames(name, query); @@ -87,5 +103,4 @@ public class LocalBrokerFacade extends BrokerFacadeSupport { public Object newProxyInstance(ObjectName objectName, Class interfaceClass, boolean notificationBroadcaster) { return getManagementContext().newProxyInstance(objectName, interfaceClass, notificationBroadcaster); } - } diff --git a/activemq-web/src/test/java/org/apache/activemq/web/LocalBrokerFacadeTest.java b/activemq-web/src/test/java/org/apache/activemq/web/LocalBrokerFacadeTest.java new file mode 100644 index 0000000000..37961e91bf --- /dev/null +++ b/activemq-web/src/test/java/org/apache/activemq/web/LocalBrokerFacadeTest.java @@ -0,0 +1,114 @@ +/** + * 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.web; + +import static com.google.common.collect.Sets.newHashSet; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.broker.jmx.BrokerView; +import org.apache.activemq.broker.jmx.ManagedRegionBroker; +import org.apache.activemq.broker.region.Destination; +import org.apache.activemq.broker.region.DestinationFilter; +import org.apache.activemq.broker.region.Queue; +import org.apache.activemq.broker.region.Region; +import org.apache.activemq.command.ActiveMQDestination; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class LocalBrokerFacadeTest { + + @Mock + private BrokerService brokerService; + @Mock + private BrokerView brokerView; + @Mock + private Queue queue; + @Mock + private Queue otherQueue; + @Mock + private ManagedRegionBroker managedRegionBroker; + @Mock + private Region region; + @Mock + private ActiveMQDestination destination; + + @Test + public void testPurgeQueueWorksForSimpleQueue() throws Exception { + LocalBrokerFacade facade = new LocalBrokerFacade(brokerService); + when(brokerService.getAdminView()).thenReturn(brokerView); + when(brokerView.getBroker()).thenReturn(managedRegionBroker); + when(managedRegionBroker.getQueueRegion()).thenReturn(region); + when(region.getDestinations(destination)).thenReturn(newHashSet((Destination) queue)); + + facade.purgeQueue(destination); + + verify(queue).purge(); + } + + @Test + public void testPurgeQueueWorksForMultipleDestinations() throws Exception { + Queue queue1 = mock(Queue.class); + Queue queue2 = mock(Queue.class); + + LocalBrokerFacade facade = new LocalBrokerFacade(brokerService); + when(brokerService.getAdminView()).thenReturn(brokerView); + when(brokerView.getBroker()).thenReturn(managedRegionBroker); + when(managedRegionBroker.getQueueRegion()).thenReturn(region); + when(region.getDestinations(destination)).thenReturn(newHashSet((Destination) queue1, queue2)); + + facade.purgeQueue(destination); + + verify(queue1).purge(); + verify(queue2).purge(); + } + + @Test + public void testPurgeQueueWorksForFilterWrappedQueue() throws Exception { + + LocalBrokerFacade facade = new LocalBrokerFacade(brokerService); + when(brokerService.getAdminView()).thenReturn(brokerView); + when(brokerView.getBroker()).thenReturn(managedRegionBroker); + when(managedRegionBroker.getQueueRegion()).thenReturn(region); + + when(region.getDestinations(destination)).thenReturn(newHashSet((Destination) new DestinationFilter(queue))); + + facade.purgeQueue(destination); + + verify(queue).purge(); + } + + @Test + public void testPurgeQueueWorksForMultipleFiltersWrappingAQueue() throws Exception { + + LocalBrokerFacade facade = new LocalBrokerFacade(brokerService); + when(brokerService.getAdminView()).thenReturn(brokerView); + when(brokerView.getBroker()).thenReturn(managedRegionBroker); + when(managedRegionBroker.getQueueRegion()).thenReturn(region); + + when(region.getDestinations(destination)).thenReturn(newHashSet((Destination) new DestinationFilter(new DestinationFilter(queue)))); + + facade.purgeQueue(destination); + + verify(queue).purge(); + } +}