From c0eae1bfff646075a52067a7a060a68dbb0d9508 Mon Sep 17 00:00:00 2001 From: artnaseef Date: Thu, 1 Jan 2015 20:10:59 -0700 Subject: [PATCH] [AMQ-5499] Register the "publishable" URI of every connector on the broker as a maven property after broker startup. Added unit tests. Reworked singletons to allow clean unit testing of StartBrokerMojo. --- .../activemq-maven-plugin/pom.xml | 11 +- .../org/apache/activemq/maven/Broker.java | 21 +- .../activemq/maven/MavenBrokerManager.java | 54 +++ .../maven/MavenBrokerSingletonManager.java | 59 ++++ .../activemq/maven/StartBrokerMojo.java | 237 ++++++++++++- .../apache/activemq/maven/StopBrokerMojo.java | 19 +- .../activemq/maven/XBeanFileResolver.java | 20 +- .../activemq/maven/StartBrokerMojoTest.java | 332 ++++++++++++++++++ .../activemq/maven/XBeanFileResolverTest.java | 4 +- pom.xml | 1 + 10 files changed, 736 insertions(+), 22 deletions(-) create mode 100644 activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerManager.java create mode 100644 activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerSingletonManager.java create mode 100644 activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/StartBrokerMojoTest.java diff --git a/activemq-tooling/activemq-maven-plugin/pom.xml b/activemq-tooling/activemq-maven-plugin/pom.xml index a5479ad006..95a70e6a95 100644 --- a/activemq-tooling/activemq-maven-plugin/pom.xml +++ b/activemq-tooling/activemq-maven-plugin/pom.xml @@ -67,6 +67,13 @@ org.apache.geronimo.specs geronimo-j2ee-management_1.1_spec + + + org.mockito + mockito-core + ${mockito-version} + test + @@ -74,7 +81,9 @@ maven-surefire-plugin - true + + ${project.build.testOutputDirectory} + diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/Broker.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/Broker.java index 14a0275a9b..f427bcd969 100644 --- a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/Broker.java +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/Broker.java @@ -16,12 +16,13 @@ */ package org.apache.activemq.maven; -import java.util.Properties; - import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerService; import org.apache.maven.plugin.MojoExecutionException; +/** + * Singleton facade between Maven and one ActiveMQ broker. + */ public class Broker { private static BrokerService broker; @@ -117,4 +118,20 @@ public class Broker { broker.stop(); } } + + /** + * Return the broker service created. + */ + public static BrokerService getBroker() { + return broker; + } + + /** + * Override the default creation of the broker service. Primarily added for testing purposes. + * + * @param broker + */ + public static void setBroker(BrokerService broker) { + Broker.broker = broker; + } } diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerManager.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerManager.java new file mode 100644 index 0000000000..9f715fb777 --- /dev/null +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerManager.java @@ -0,0 +1,54 @@ +/** + * 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.maven; + +import org.apache.activemq.broker.BrokerService; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Manager of the broker used by the maven plugin. + */ +public interface MavenBrokerManager { + /** + * Start the broker using the fork setting and configuration at the given URI. + * + * @param fork true => run the broker asynchronously; false => run the broker synchronously (this method does not + * return until the broker shuts down) + * @param configUri URI of the broker configuration; prefix with "xbean:file" to read XML configuration from a file. + * @throws MojoExecutionException + */ + void start(boolean fork, String configUri) throws MojoExecutionException; + + /** + * Stop the broker. + * + * @throws MojoExecutionException + */ + void stop() throws MojoExecutionException; + + /** + * Return the broker service created. + */ + BrokerService getBroker(); + + /** + * Set the broker service managed to the one given. + * + * @param broker activemq instance to manage. + */ + void setBroker(BrokerService broker); +} diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerSingletonManager.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerSingletonManager.java new file mode 100644 index 0000000000..54cb02c8a3 --- /dev/null +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/MavenBrokerSingletonManager.java @@ -0,0 +1,59 @@ +/** + * 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.maven; + +import org.apache.activemq.broker.BrokerService; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Broker manager for use with Maven; uses the original singleton to track the broker instance. + */ +public class MavenBrokerSingletonManager implements MavenBrokerManager { + + public void start(boolean fork, String configUri) throws MojoExecutionException { + Broker.start(fork, configUri); + } + + public void stop() throws MojoExecutionException { + Broker.stop(); + } + + /** + * Wait for a shutdown invocation elsewhere + * + * @throws Exception + */ + protected void waitForShutdown() throws Exception { + Broker.waitForShutdown(); + } + + /** + * Return the broker service created. + */ + public BrokerService getBroker() { + return Broker.getBroker(); + } + + /** + * Override the default creation of the broker service. Primarily added for testing purposes. + * + * @param broker + */ + public void setBroker(BrokerService broker) { + Broker.setBroker(broker); + } +} diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StartBrokerMojo.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StartBrokerMojo.java index eef68aaa93..9e38949ae0 100644 --- a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StartBrokerMojo.java +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StartBrokerMojo.java @@ -18,6 +18,7 @@ package org.apache.activemq.maven; import java.util.Properties; +import org.apache.activemq.broker.TransportConnector; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; @@ -29,6 +30,10 @@ import org.apache.maven.project.MavenProject; * @phase process-sources */ public class StartBrokerMojo extends AbstractMojo { + /** + * Default connector property name format. + */ + public static final String DEFAULT_CONNECTOR_PROPERTY_NAME_FORMAT = "org.apache.activemq.connector.%s.uri"; /** * The maven project. @@ -71,6 +76,175 @@ public class StartBrokerMojo extends AbstractMojo { */ private boolean skip; + /** + * Format of the connector URI property names. The first argument for the format is the connector name. + * + * @parameter property="connectorPropertyNameFormat" + */ + private String connectorPropertyNameFormat = DEFAULT_CONNECTOR_PROPERTY_NAME_FORMAT; + + /** + * Broker manager used to start and stop the broker. + */ + private MavenBrokerManager brokerManager; + + /** + * XBean File Resolver used to detect and transform xbean file URIs. + */ + private XBeanFileResolver xBeanFileResolver = new XBeanFileResolver(); + + /** + * Retrieve the Maven project for this mojo. + * + * @return the Maven project. + */ + public MavenProject getProject() { + return project; + } + + /** + * Set the Maven project for this mojo. + * + * @param project the Maven project. + */ + public void setProject(MavenProject project) { + this.project = project; + } + + /** + * Retrieve the URI used to configure the broker on startup. + * + * @return the configuration URI. + */ + public String getConfigUri() { + return configUri; + } + + /** + * Set the URI used to configure the broker on startup. + * + * @param configUri the URI used to configure the broker. + */ + public void setConfigUri(String configUri) { + this.configUri = configUri; + } + + /** + * Determine if the mojo is configured to fork a broker. + * + * @return true => the mojo will fork a broker (i.e. start it in the background); false => start the broker and + * wait synchronously for it to terminate. + */ + public boolean isFork() { + return fork; + } + + /** + * Configure the mojo to run the broker asynchronously (i.e. fork) or synchronously. + * + * @param fork true => start the broker asynchronously; true => start the broker synchronously. + */ + public void setFork(boolean fork) { + this.fork = fork; + } + + /** + * Determine if the mojo is configured to skip the broker startup. + * + * @return true => the mojo will skip the broker startup; false => the mojo will start the broker normally. + */ + public boolean isSkip() { + return skip; + } + + /** + * Configure the mojo to skip or normally execute the broker startup. + * + * @param skip true => the mojo will skip the broker startup; false => the mojo will start the broker normally. + */ + public void setSkip(boolean skip) { + this.skip = skip; + } + + /** + * Retrieve properties to add to the System properties on broker startup. + * + * @return properties to add to the System properties. + */ + public Properties getSystemProperties() { + return systemProperties; + } + + /** + * Set properties to add to the System properties on broker startup. + * + * @param systemProperties properties to add to the System properties. + */ + public void setSystemProperties(Properties systemProperties) { + this.systemProperties = systemProperties; + } + + /** + * Retrieve the format used to generate property names when registering connector URIs. + * + * @return the format used to generate property names. + */ + public String getConnectorPropertyNameFormat() { + return connectorPropertyNameFormat; + } + + /** + * Set the format used to generate property names when registering connector URIs. + * + * @param connectorPropertyNameFormat the new format to use when generating property names. + */ + public void setConnectorPropertyNameFormat(String connectorPropertyNameFormat) { + this.connectorPropertyNameFormat = connectorPropertyNameFormat; + } + + /** + * Retrieve the manager used to create and retain the started broker. + * + * @return the manager. + */ + public MavenBrokerManager getBrokerManager() { + return brokerManager; + } + + /** + * Set the manager used to create and retain the started broker. + * + * @param brokerManager the new manager to use. + */ + public void setBrokerManager(MavenBrokerManager brokerManager) { + this.brokerManager = brokerManager; + } + + /** + * Retrieve the XBeanFileResolver used to detect and transform XBean URIs. + * + * @return the XBeanFileResolver used. + */ + public XBeanFileResolver getxBeanFileResolver() { + return xBeanFileResolver; + } + + /** + * Set the XBeanFileResolver to use when detecting and transforming XBean URIs. + * + * @param xBeanFileResolver the XBeanFileResolver to use. + */ + public void setxBeanFileResolver(XBeanFileResolver xBeanFileResolver) { + this.xBeanFileResolver = xBeanFileResolver; + } + + /** + * Perform the mojo operation, which starts the ActiveMQ broker unless configured to skip it. Also registers the + * connector URIs in the maven project properties on startup, which enables the use of variable substitution in + * the pom.xml file to determine the address of the connector using the standard ${...} syntax. + * + * @throws MojoExecutionException + */ @Override public void execute() throws MojoExecutionException { if (skip) { @@ -78,16 +252,21 @@ public class StartBrokerMojo extends AbstractMojo { return; } - setSystemProperties(); + addActiveMQSystemProperties(); getLog().info("Loading broker configUri: " + configUri); - if (XBeanFileResolver.isXBeanFile(configUri)) { + if (this.xBeanFileResolver.isXBeanFile(configUri)) { getLog().debug("configUri before transformation: " + configUri); - configUri = XBeanFileResolver.toUrlCompliantAbsolutePath(configUri); + configUri = this.xBeanFileResolver.toUrlCompliantAbsolutePath(configUri); getLog().debug("configUri after transformation: " + configUri); } - Broker.start(fork, configUri); + this.useBrokerManager().start(fork, configUri); + + // + // Register the transport connector URIs in the Maven project. + // + this.registerTransportConnectorUris(); getLog().info("Started the ActiveMQ Broker"); } @@ -95,7 +274,7 @@ public class StartBrokerMojo extends AbstractMojo { /** * Set system properties */ - protected void setSystemProperties() { + protected void addActiveMQSystemProperties() { // Set the default properties System.setProperty("activemq.base", project.getBuild().getDirectory() + "/"); System.setProperty("activemq.home", project.getBuild().getDirectory() + "/"); @@ -107,4 +286,52 @@ public class StartBrokerMojo extends AbstractMojo { // Overwrite any custom properties System.getProperties().putAll(systemProperties); } + + /** + * Register all of the broker's transport connector URIs in the Maven project as properties. Property names are + * formed from the connectorPropertyNameFormat using String.format(), with the connector name as the one and only + * argument. The value of each property is that returned by getPublishableConnectString(). + */ + protected void registerTransportConnectorUris () { + Properties props = project.getProperties(); + + // + // Loop through all of the connectors, adding each. + // + for ( TransportConnector oneConnector : this.useBrokerManager().getBroker().getTransportConnectors() ) { + try { + // + // Format the name of the property and grab the value. + // + String propName = String.format(this.connectorPropertyNameFormat, oneConnector.getName()); + String value = oneConnector.getPublishableConnectString(); + + getLog().debug("setting transport connector URI property: propertyName=\"" + propName + + "\"; value=\"" + value + "\""); + + // + // Set the property. + // + props.setProperty(propName, value); + } catch (Exception exc) { + // + // Warn of the issue and continue. + // + getLog().warn("error on obtaining broker connector uri; connector=" + oneConnector, exc); + } + } + } + + /** + * Use the configured broker manager, if defined; otherwise, use the default broker manager. + * + * @return the broker manager to use. + */ + protected MavenBrokerManager useBrokerManager () { + if ( this.brokerManager == null ) { + this.brokerManager = new MavenBrokerSingletonManager(); + } + + return this.brokerManager; + } } diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StopBrokerMojo.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StopBrokerMojo.java index a0b15eb1dd..2daebbbf9e 100644 --- a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StopBrokerMojo.java +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StopBrokerMojo.java @@ -26,6 +26,15 @@ import org.apache.maven.plugin.MojoExecutionException; * @phase process-sources */ public class StopBrokerMojo extends AbstractMojo { + private MavenBrokerManager brokerManager; + + public MavenBrokerManager getBrokerManager() { + return brokerManager; + } + + public void setBrokerManager(MavenBrokerManager brokerManager) { + this.brokerManager = brokerManager; + } /** * Skip execution of the ActiveMQ Broker plugin if set to true @@ -40,8 +49,16 @@ public class StopBrokerMojo extends AbstractMojo { return; } - Broker.stop(); + this.brokerManager.stop(); getLog().info("Stopped the ActiveMQ Broker"); } + + protected MavenBrokerManager useBrokerManager () { + if ( this.brokerManager == null ) { + this.brokerManager = new MavenBrokerSingletonManager(); + } + + return this.brokerManager; + } } diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/XBeanFileResolver.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/XBeanFileResolver.java index ad6914b612..aa0ddce444 100644 --- a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/XBeanFileResolver.java +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/XBeanFileResolver.java @@ -28,14 +28,10 @@ import java.net.URL; public class XBeanFileResolver { private static final String XBEAN_FILE = "xbean:file:"; - private XBeanFileResolver() { - // Utility class, should not be instantiated. - } - /** * Check if the provided path is an URL to a XBean file (xbean:file:) */ - public static boolean isXBeanFile(final String configUri) { + public boolean isXBeanFile(final String configUri) { return configUri.startsWith(XBEAN_FILE); } @@ -43,7 +39,7 @@ public class XBeanFileResolver { * Convert provided path into a URL-style absolute path. See also: * http://maven.apache.org/plugin-developers/common-bugs.html# Converting_between_URLs_and_Filesystem_Paths */ - public static String toUrlCompliantAbsolutePath(final String configUri) { + public String toUrlCompliantAbsolutePath(final String configUri) { if (!isXBeanFile(configUri)) return configUri; @@ -51,15 +47,15 @@ public class XBeanFileResolver { return XBEAN_FILE + toAbsolutePath(filePath); } - private static String extractFilePath(final String configUri) { + private String extractFilePath(final String configUri) { return configUri.substring(getIndexFilePath(configUri), configUri.length()); } - private static int getIndexFilePath(final String configUri) { + private int getIndexFilePath(final String configUri) { return configUri.indexOf(XBEAN_FILE) + XBEAN_FILE.length(); } - private static String toAbsolutePath(final String path) { + private String toAbsolutePath(final String path) { try { final URL url = new File(path).toURI().toURL(); return toFilePath(url); @@ -68,17 +64,17 @@ public class XBeanFileResolver { } } - private static String toFilePath(final URL url) { + private String toFilePath(final URL url) { String filePath = url.getFile(); return underWindows() ? removePrependingSlash(filePath) : filePath; } - private static String removePrependingSlash(String filePath) { + private String removePrependingSlash(String filePath) { // Remove prepending '/' because path would be /C:/temp/file.txt, as URL would be file:/C:/temp/file.txt return filePath.substring(1, filePath.length()); } - private static boolean underWindows() { + private boolean underWindows() { String os = System.getProperty("os.name").toLowerCase(); return (os.indexOf("win") >= 0); } diff --git a/activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/StartBrokerMojoTest.java b/activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/StartBrokerMojoTest.java new file mode 100644 index 0000000000..df2f0a1de9 --- /dev/null +++ b/activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/StartBrokerMojoTest.java @@ -0,0 +1,332 @@ +/** + * 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.maven; + +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.broker.TransportConnector; +import org.apache.maven.model.Build; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; + +import static org.junit.Assert.*; + +public class StartBrokerMojoTest { + /** + * Main object-under-test; configured with mocks and defaults. + */ + private StartBrokerMojo startBrokerMojo; + + /** + * Secondary object-under-test primarily for setter/getter testing; available for all tests without any + * configuration (i.e. raw). + */ + private StartBrokerMojo startBrokerMojoRaw; + + //// + // MOCKS + //// + private MavenProject mockMavenProject; + private Build mockBuild; + private Properties mockMavenProperties; + private MavenBrokerManager mockBrokerManager; + private XBeanFileResolver mockXbeanFileResolver; + private BrokerService mockBrokerService; + private Log mockMavenLog; + + //// + // Test Objects + //// + private Properties systemProperties; + private List transportConnectorList; + private Exception testException; + + @Before + public void setupTest () { + // + // Create the objects-under-test + // + this.startBrokerMojo = new StartBrokerMojo(); + this.startBrokerMojoRaw = new StartBrokerMojo(); + + // + // Create mocks + // + this.mockMavenProject = Mockito.mock(MavenProject.class); + this.mockBuild = Mockito.mock(Build.class); + this.mockMavenProperties = Mockito.mock(Properties.class); + this.mockBrokerManager = Mockito.mock(MavenBrokerManager.class); + this.mockXbeanFileResolver = Mockito.mock(XBeanFileResolver.class); + this.mockBrokerService = Mockito.mock(BrokerService.class); + this.mockMavenLog = Mockito.mock(Log.class); + + // + // Prepare other test objects and configure the object-under-test. + // + this.transportConnectorList = new LinkedList(); + this.systemProperties = new Properties(); + this.testException = new Exception("x-test-exc-x"); + + this.startBrokerMojo.setBrokerManager(this.mockBrokerManager); + this.startBrokerMojo.setxBeanFileResolver(this.mockXbeanFileResolver); + this.startBrokerMojo.setProject(this.mockMavenProject); + this.startBrokerMojo.setConnectorPropertyNameFormat("x-name-%s"); + this.startBrokerMojo.setConfigUri("x-config-uri-x"); + this.startBrokerMojo.setFork(false); + this.startBrokerMojo.setSystemProperties(this.systemProperties); + this.startBrokerMojo.setLog(this.mockMavenLog); + + // + // Define standard mock interactions. + // + Mockito.when(this.mockBrokerManager.getBroker()).thenReturn(this.mockBrokerService); + Mockito.when(this.mockMavenProject.getProperties()).thenReturn(this.mockMavenProperties); + Mockito.when(this.mockMavenProject.getBuild()).thenReturn(this.mockBuild); + Mockito.when(this.mockBuild.getDirectory()).thenReturn("x-proj-dir-x"); + Mockito.when(this.mockBrokerService.getTransportConnectors()).thenReturn(transportConnectorList); + } + + /** + * Test the setter and getter for propertyNameFormat. + */ + @Test + public void testSetGetConnectorPropertyNameFormat () { + assertEquals(StartBrokerMojo.DEFAULT_CONNECTOR_PROPERTY_NAME_FORMAT, + this.startBrokerMojoRaw.getConnectorPropertyNameFormat()); + + this.startBrokerMojoRaw.setConnectorPropertyNameFormat("x-name-format-x"); + assertEquals("x-name-format-x", this.startBrokerMojoRaw.getConnectorPropertyNameFormat()); + } + + /** + * Test the setter and getter for configUri. + */ + @Test + public void testSetGetConfigUri () { + assertNull(this.startBrokerMojoRaw.getConfigUri()); + + this.startBrokerMojoRaw.setConfigUri("x-config-uri-x"); + assertEquals("x-config-uri-x", this.startBrokerMojoRaw.getConfigUri()); + } + + /** + * Test the setter and getter for mavenProject. + */ + @Test + public void testSetGetMavenProject () { + assertNull(this.startBrokerMojoRaw.getProject()); + + this.startBrokerMojoRaw.setProject(this.mockMavenProject); + assertSame(this.mockMavenProject, this.startBrokerMojoRaw.getProject()); + } + + /** + * Test the setter and getter for fork. + */ + @Test + public void testSetGetFork () { + assertFalse(this.startBrokerMojoRaw.isFork()); + + this.startBrokerMojoRaw.setFork(true); + assertTrue(this.startBrokerMojoRaw.isFork()); + } + + /** + * Test the setter and getter for skip. + */ + @Test + public void testSetGetSkip () { + assertFalse(this.startBrokerMojoRaw.isSkip()); + + this.startBrokerMojoRaw.setSkip(true); + assertTrue(this.startBrokerMojoRaw.isSkip()); + } + + /** + * Test the setter and getter for systemProperties. + */ + @Test + public void testSetGetSystemProperties () { + assertNull(this.startBrokerMojoRaw.getSystemProperties()); + + this.startBrokerMojoRaw.setSystemProperties(this.systemProperties); + assertSame(this.systemProperties, this.startBrokerMojoRaw.getSystemProperties()); + } + + /** + * Test the setter and getter for brokerManager. + */ + @Test + public void testSetGetBrokerManager () { + assertNull(this.startBrokerMojoRaw.getBrokerManager()); + + this.startBrokerMojoRaw.setBrokerManager(this.mockBrokerManager); + assertSame(this.mockBrokerManager, this.startBrokerMojoRaw.getBrokerManager()); + } + + /** + * Test the setter and getter for xbeanFileResolver. + */ + @Test + public void testSetGetXbeanFileResolver () { + assertTrue(this.startBrokerMojoRaw.getxBeanFileResolver() instanceof XBeanFileResolver); + assertNotSame(this.mockXbeanFileResolver, this.startBrokerMojoRaw.getxBeanFileResolver()); + + this.startBrokerMojoRaw.setxBeanFileResolver(this.mockXbeanFileResolver); + assertSame(this.mockXbeanFileResolver, this.startBrokerMojoRaw.getxBeanFileResolver()); + } + + /** + * Test normal execution of the mojo leads to startup of the broker. + * + * @throws Exception + */ + @Test + public void testExecute () throws Exception { + this.startBrokerMojo.execute(); + + Mockito.verify(this.mockBrokerManager).start(false, "x-config-uri-x"); + } + + /** + * Test the registration of a single transport connector URI when a broker with only one connector is started. + */ + @Test + public void testExecuteRegistersTransportConnectorOneUri () throws Exception { + this.startBrokerMojo.setProject(this.mockMavenProject); + + this.createTestTransportConnectors("openwire-client"); + this.startBrokerMojo.execute(); + + Mockito.verify(this.mockMavenProperties).setProperty("x-name-openwire-client", "x-pub-addr-for-openwire-client"); + } + + /** + * Test the registration of multiple transport connector URIs when a broker with multiple connectors is started. + * + * @throws Exception + */ + @Test + public void testExecuteRegistersTransportConnectorMultiUri () throws Exception { + this.createTestTransportConnectors("connector1", "connector2", "connector3"); + + this.startBrokerMojo.execute(); + + Mockito.verify(this.mockMavenProperties).setProperty("x-name-connector1", "x-pub-addr-for-connector1"); + Mockito.verify(this.mockMavenProperties).setProperty("x-name-connector2", "x-pub-addr-for-connector2"); + Mockito.verify(this.mockMavenProperties).setProperty("x-name-connector3", "x-pub-addr-for-connector3"); + } + + /** + * Test handling when TransportConnector.getPublishableConnectString() throws an exception. + * + * @throws Exception + */ + @Test + public void testExceptionOnGetPublishableConnectString () throws Exception { + TransportConnector mockTransportConnector = Mockito.mock(TransportConnector.class); + + Mockito.when(mockTransportConnector.toString()).thenReturn("x-conn-x"); + Mockito.when(mockTransportConnector.getPublishableConnectString()).thenThrow(testException); + + this.transportConnectorList.add(mockTransportConnector); + + this.startBrokerMojo.execute(); + + Mockito.verify(this.mockMavenLog).warn("error on obtaining broker connector uri; connector=x-conn-x", + this.testException); + } + + /** + * Test that an xbean configuration file URI is transformed on use. + * + * @throws Exception + */ + @Test + public void testUseXbeanConfigFile () throws Exception { + Mockito.when(this.mockXbeanFileResolver.isXBeanFile("x-config-uri-x")).thenReturn(true); + Mockito.when(this.mockXbeanFileResolver.toUrlCompliantAbsolutePath("x-config-uri-x")) + .thenReturn("x-transformed-uri-x"); + + this.startBrokerMojo.execute(); + + Mockito.verify(this.mockMavenLog).debug("configUri before transformation: x-config-uri-x"); + Mockito.verify(this.mockMavenLog).debug("configUri after transformation: x-transformed-uri-x"); + } + + /** + * Test that a URI that does not represent an xbean configuration file is not translated. + * + * @throws Exception + */ + @Test + public void testDoNotUseXbeanConfigFile () throws Exception { + Mockito.when(this.mockXbeanFileResolver.isXBeanFile("x-config-uri-x")).thenReturn(false); + + this.startBrokerMojo.execute(); + + Mockito.verify(this.mockMavenLog, Mockito.times(0)).debug("configUri before transformation: x-config-uri-x"); + Mockito.verify(this.mockMavenLog, Mockito.times(0)) + .debug("configUri after transformation: x-transformed-uri-x"); + } + + /** + * Test that execution of the mojo is skipped if so configured. + * + * @throws Exception + */ + @Test + public void testSkip () throws Exception { + this.startBrokerMojo.setSkip(true); + this.startBrokerMojo.execute(); + + Mockito.verify(this.mockMavenLog).info("Skipped execution of ActiveMQ Broker"); + Mockito.verifyNoMoreInteractions(this.mockBrokerManager); + } + + /** + * Add mock, test transport connectors with the given names. + * + * @param connectorNames names of the mock connectors to add for the test. + * @throws Exception + */ + protected void createTestTransportConnectors(String... connectorNames) throws Exception { + for ( String oneConnectorName : connectorNames ) { + // + // Mock the connector + // + TransportConnector mockConnector = Mockito.mock(TransportConnector.class); + + // + // Return the connector name on getName() and a unique string on getPublishableConnectString(). + // + Mockito.when(mockConnector.getName()).thenReturn(oneConnectorName); + Mockito.when(mockConnector.getPublishableConnectString()).thenReturn("x-pub-addr-for-" + oneConnectorName); + + // + // Add to the test connector list. + // + this.transportConnectorList.add(mockConnector); + } + } +} diff --git a/activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/XBeanFileResolverTest.java b/activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/XBeanFileResolverTest.java index 67a36e6b48..7d3e123096 100644 --- a/activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/XBeanFileResolverTest.java +++ b/activemq-tooling/activemq-maven-plugin/src/test/java/org/apache/activemq/maven/XBeanFileResolverTest.java @@ -31,13 +31,15 @@ public class XBeanFileResolverTest { @Test public void urlToXBeanFileShouldBeResolvedToAbsolutePath() throws IOException { + XBeanFileResolver xBeanFileResolver = new XBeanFileResolver(); + String currentDirectory = getCurrentDirectoryLinuxStyle(); String relativeXBeanFilePath = "src/main/resources/activemq.xml"; // e.g. xbean:file:C:/dev/src/active-mq/activemq-tooling/activemq-maven-plugin/src/main/resources/activemq.xml String expectedUrl = XBEAN_FILE + currentDirectory + "/" + relativeXBeanFilePath; - String actualUrl = XBeanFileResolver.toUrlCompliantAbsolutePath(XBEAN_FILE + relativeXBeanFilePath); + String actualUrl = xBeanFileResolver.toUrlCompliantAbsolutePath(XBEAN_FILE + relativeXBeanFilePath); assertEquals(expectedUrl, actualUrl); } diff --git a/pom.xml b/pom.xml index c8cb6a276e..7185cd0300 100755 --- a/pom.xml +++ b/pom.xml @@ -92,6 +92,7 @@ 0.6 1.8 1.2.17 + 1.10.17 1.10 1.2.0 10.10.1.1