diff options
| author | Martin Ritchie <ritchiem@apache.org> | 2010-06-22 13:41:24 +0000 |
|---|---|---|
| committer | Martin Ritchie <ritchiem@apache.org> | 2010-06-22 13:41:24 +0000 |
| commit | 5951dfafe74a4115ef22dbb5d5d9b8c455bde0f5 (patch) | |
| tree | 41f103484174afd124be65a0355626ebb65074f6 /java/broker-plugins | |
| parent | 571c08285a95a1fb810309f19ceca2107054f12d (diff) | |
| download | qpid-python-5951dfafe74a4115ef22dbb5d5d9b8c455bde0f5.tar.gz | |
QPID-2555 : Modified the MANIFEST to include org.apache.qpid.common and org.apache.qpid.server.registry
Patch provided by Sorin Suciu
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@956888 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/broker-plugins')
12 files changed, 398 insertions, 434 deletions
diff --git a/java/broker-plugins/experimental/info/MANIFEST.MF b/java/broker-plugins/experimental/info/MANIFEST.MF index dabafe8305..0ffa869ea9 100644 --- a/java/broker-plugins/experimental/info/MANIFEST.MF +++ b/java/broker-plugins/experimental/info/MANIFEST.MF @@ -5,7 +5,9 @@ Bundle-SymbolicName: qpid_info_plugin;singleton:=true Bundle-Version: 1.0.0 Bundle-Activator: org.apache.qpid.info.Activator Import-Package: org.apache.qpid.server.configuration, - org.osgi.framework + org.osgi.framework, + org.apache.qpid.common, + org.apache.qpid.server.registry Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ClassPath: . Bundle-ActivationPolicy: lazy diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java index 167a53fdc5..a442cf1f28 100644 --- a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java +++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Activator.java @@ -19,60 +19,204 @@ * */ -/** - * - * @author sorin - * - * Activator class for the tracking services - */ - package org.apache.qpid.info; import java.io.File; -import java.io.FileInputStream; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Properties; import org.apache.qpid.info.util.HttpPoster; +import org.apache.qpid.info.util.IniFileReader; +import org.apache.qpid.info.util.SoapClient; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +/** + * The Activator class for the OSGI info service + * + */ public class Activator implements BundleActivator { + private final List<String> soapPropList = Arrays.asList("soap.hostname", + "soap.port", "soap.path", "soap.action", "soap.envelope"); + + private final List<String> httpPropList = Arrays.asList("http.url", + "http.envelope"); + + InfoServiceImpl service = null; + BundleContext _ctx = null; + Map<String, Properties> infoprops = new HashMap<String, Properties>(); + + /** + * Start bundle method + * + * @param ctx + * the bundle context + */ public void start(BundleContext ctx) throws Exception { if (null != ctx) { - BrokerInfoServiceImpl service = new BrokerInfoServiceImpl(ctx); - ctx.registerService(BrokerInfoService.class.getName(), service, - null); _ctx = ctx; - HttpPoster hp; - try - { - Properties props = new Properties(); - String QPID_WORK = System.getenv("QPID_WORK"); - props.load(new FileInputStream(QPID_WORK + File.separator - + "etc" + File.separator + "qpidinfo.properties")); - hp = new HttpPoster(props, service.invoke().toXML()); - hp.run(); - } catch (Exception ex) - { - // Silently drop any exception - } + service = new InfoServiceImpl(); + ctx.registerService(InfoService.class.getName(), service, null); + sendInfo("STARTUP"); } } + /** + * Getter for the bundle context + * + * @return BundleContext the bundle context + */ public BundleContext getBundleContext() { return _ctx; } + /** + * Stop the bundle method + * + * @param ctx + * the bundle context + */ public void stop(BundleContext ctx) throws Exception { - // no need to do anything here, osgi will unregister the service for us + sendInfo("SHUTDOWN"); + } + + /** + * Sends the information message + * + * @param action + * label that identifies if we are starting up or shutting down + */ + private void sendInfo(String action) + { + if ((null == _ctx) && (null == service)) + { + // invalid state + return; + } + + IniFileReader ifr = new IniFileReader(); + try + { + String QPID_HOME = System.getProperty("QPID_HOME"); + String cfgFilePath = QPID_HOME + File.separator + "etc" + + File.separator + "qpidinfo.properties"; + ifr.load(cfgFilePath); + } catch (Exception ex) + { + // drop the exception + return; + } + + // If we have no sections, something has gone really wrong, abort + if (ifr.getSections().size() == 0) + return; + + Info<? extends Map<String, ?>> info = service.invoke(action); + String protocol = ifr.getSections().get("").getProperty("protocol"); + sendMessages(protocol, ifr, info); + } + + /** + * Sends all the messages configured in the properties file + * + * @param protocol + * indicates what protocol to be used: http and soap implemented + * for now + * @param ifr + * an instance of IniFileReader class + * @param info + * an instance of an Info object, encapsulating the information + * we want to send + */ + private void sendMessages(String protocol, IniFileReader ifr, + Info<? extends Map<String, ?>> info) + { + if (null != protocol) + { + // Set the global properties first (as they are the defaults) + Properties defaultProps = ifr.getSections().get(""); + if (protocol.toLowerCase().startsWith("http")) + { + for (String section : ifr.getSections().keySet()) + { + // Skip the defaults + if (section.equals("")) + continue; + Properties props = new Properties(); + props.putAll(defaultProps); + props.putAll(ifr.getSections().get(section)); + if (isValid(protocol, props)) + { + new HttpPoster(props, info.toXML()).run(); + } + } + + } else if (protocol.toLowerCase().startsWith("soap")) + { + for (String section : ifr.getSections().keySet()) + { + Properties props = new Properties(); + props.putAll(defaultProps); + props.putAll(ifr.getSections().get(section)); + if (isValid(protocol, props)) + { + new SoapClient(info.toMap(), props).sendSOAPMessage(); + } + } + } + } else + { + return; + } + } + + /** + * Checks if the properties for a specified protocol are valid + * + * @param protocol + * String representing the protocol + * @param props + * The properties associate with the specified protocol + */ + private boolean isValid(String protocol, Properties props) + { + if (null == protocol) + return false; + String value = ""; + if (protocol.toLowerCase().startsWith("http")) + { + for (String prop : httpPropList) + { + if (null == props.get(prop)) + return false; + } + return true; + } + + if (protocol.toLowerCase().startsWith("soap")) + { + for (String prop : soapPropList) + { + value = props.getProperty(prop); + if (null == value) + { + return false; + } + } + return true; + } + return false; } +} // end class -} diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java deleted file mode 100644 index 4c3c95d385..0000000000 --- a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoService.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * - * 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.qpid.info; - -public interface BrokerInfoService -{ - public Info<?> invoke(); -} diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java deleted file mode 100644 index fe10d55dea..0000000000 --- a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/BrokerInfoServiceImpl.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * - * 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. - * - */ - -/** - * - * @author sorin - * - * Implementation for Info service - */ - -package org.apache.qpid.info; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.Map.Entry; - -import org.apache.qpid.server.configuration.ServerConfiguration; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; - -public class BrokerInfoServiceImpl implements BrokerInfoService -{ - - SortedMap<String, String> brokerInfoMap = new TreeMap<String, String>(); - - private final List<String> qpidProps = Arrays.asList("QPID_HOME", - "QPID_WORK", "java.class.path", "java.vm.name", - "java.class.version", "os.arch", "os.name", "os.version", - "sun.arch.data.model", "user.dir", "user.name", "user.timezone"); - - private final BundleContext _ctx; - - public BrokerInfoServiceImpl(BundleContext ctx) - { - _ctx = ctx; - } - - public Info<? extends Map<String, ?>> invoke() - { - // Get current time - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ"); - brokerInfoMap.put("time", sdf.format(Calendar.getInstance().getTime())); - // Get the hostname - try - { - InetAddress addr = InetAddress.getLocalHost(); - String hostname = addr.getHostName(); - brokerInfoMap.put("hostname", hostname); - brokerInfoMap.put("ip", addr.getHostAddress()); - } catch (UnknownHostException e) - { - // - } - // Dump system props - Properties sysprops = System.getProperties(); - String propName; - for (Iterator<Entry<Object, Object>> it = sysprops.entrySet() - .iterator(); it.hasNext();) - { - Entry<Object, Object> en = it.next(); - propName = en.getKey().toString(); - if (qpidProps.indexOf(propName) >= 0) - { - brokerInfoMap.put(propName, en.getValue().toString()); - } - } - - if (null == _ctx) - { - return new Info<SortedMap<String, String>>(brokerInfoMap); - } - - ServiceReference sref; - ServerConfiguration sc; - try - { - sref = _ctx - .getServiceReference(ServerConfiguration.class.getName()); - sc = (ServerConfiguration) _ctx.getService(sref); - if (null != sc) - { - brokerInfoMap.put("port", sc.getPorts().toString()); - } - } - catch (Exception e) - { - return new Info<SortedMap<String, String>>(brokerInfoMap); - } - - return new Info<SortedMap<String, String>>(brokerInfoMap); - } - -} diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java index e4d33817a1..2c6db8df12 100644 --- a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java +++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/Info.java @@ -35,15 +35,24 @@ import java.util.Properties; import org.apache.qpid.info.util.XMLWriter; +/** + * The Info class encapsulates all the informations we are collecting + * and it is able to render it in different data representations + */ public class Info<T extends Map<String, ?>> { private T _info; + /** + * Constructor. + * @param info instantiates the object with a Map<String,?> + */ public Info(T info) { _info = info; } + @Override public String toString() { String result = ""; @@ -55,21 +64,25 @@ public class Info<T extends Map<String, ?>> return result; } - public Properties toProps() - { + /** + * Renders Info map to a property object + * @return A Properties object representing the Info map + */ + public Properties toProps() { Properties props = new Properties(); - if (null == _info) - return null; - for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();) - { - String key = it.next(); - props.put(key, _info.get(key)); + if (null==_info) return null; + for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();) { + String key = it.next(); + props.put(key, _info.get(key)); } return props; } - - public StringBuffer toStringBuffer() - { + + /** + * Renders Info map to a StringBuffer + * @return A StringBuffer object representing the Info map + */ + public StringBuffer toStringBuffer() { StringBuffer sb = new StringBuffer(); for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();) { @@ -78,22 +91,39 @@ public class Info<T extends Map<String, ?>> } return sb; } - - public StringBuffer toXML() + + /** + * Renders Info map to a StringBuffer containing an XML string + * @return A StringBuffer object containing an XML representation of the Info map + */ + public StringBuffer toXML() { XMLWriter xw = new XMLWriter(new StringBuffer()); xw.writeXMLHeader(); Map<String, String> attr = new HashMap<String, String>(); xw.writeOpenTag("qpidinfo", attr); String key; - for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();) - { + for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();) { attr.clear(); key = it.next(); - xw.writeTag(key, attr, _info.get(key).toString()); - } + xw.writeTag(key, attr, _info.get(key).toString()); + } xw.writeCloseTag("qpidinfo"); return xw.getXML(); } + + /** + * Renders Info map to a HashMap + * @return A HashMap object representing the Info map + */ + public HashMap<String,String> toMap() { + String key; + HashMap<String,String> infoMap = new HashMap<String,String>(); + for (Iterator<String> it = _info.keySet().iterator(); it.hasNext();) { + key = it.next(); + infoMap.put(key, _info.get(key).toString()); + } + return infoMap; + } } diff --git a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java index c67a7682f3..d07f34dec0 100644 --- a/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java +++ b/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/HttpPoster.java @@ -36,62 +36,92 @@ import java.util.Properties; import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
+import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
+import java.net.UnknownHostException;
+
+public class HttpPoster implements Runnable
+{
+ private final String url;
+
+ private final Hashtable<String, String> header;
-public class HttpPoster implements Runnable {
- private final String url;
- private final Hashtable<String,String> header;
private final List<String> response = new ArrayList<String>();
- private final StringBuffer _buf;
- //
- public HttpPoster(Properties props, StringBuffer buf)
+
+ private final StringBuffer _buf;
+
+ /**
+ * Constructor
+ * @param props Properties containing the URL
+ * @param buf Buffer containing the message to be posted
+ */
+ public HttpPoster(Properties props, StringBuffer buf)
+ {
+ _buf = buf;
+ if (null != props)
+ {
+ url = props.getProperty("http.url");
+ header = new Hashtable<String, String>();
+ try
+ {
+ String hostname = InetAddress.getLocalHost().getHostName();
+ header.put("hostname", hostname);
+ } catch (UnknownHostException e)
+ {
+ // Silently ignoring the error ;)
+ }
+ } else
+ {
+ url = null;
+ header = null;
+ }
+ }
+ /**
+ * Posts the message from the _buf StringBuffer to the http server
+ */
+ public void run()
{
- _buf = buf;
- if (null!= props) {
- url = props.getProperty("URL");
- header = new Hashtable<String, String>();
- String hostname = props.getProperty("hostname");
- if (null!= hostname) header.put("hostname", hostname);
- } else {
- url = null;
- header = null;
- }
- }
- //
- @Override
- public void run()
+ if (null == url)
+ return;
+ String line;
+ URL urlDest;
+ URLConnection urlConn;
+ try
{
- if (null==url) return;
- String line;
- URL urlDest;
- URLConnection urlConn;
- try {
- urlDest = new URL(url);
- urlConn = urlDest.openConnection();
- urlConn.setDoOutput(true);
- urlConn.setUseCaches(false);
- for (Iterator<String> it=header.keySet().iterator(); it.hasNext();) {
- String prop = (String)it.next();
- urlConn.setRequestProperty(prop, header.get(prop));
- }
- OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
+ urlDest = new URL(url);
+ urlConn = urlDest.openConnection();
+ urlConn.setDoOutput(true);
+ urlConn.setUseCaches(false);
+ for (Iterator<String> it = header.keySet().iterator(); it.hasNext();)
+ {
+ String prop = (String) it.next();
+ urlConn.setRequestProperty(prop, header.get(prop));
+ }
+ OutputStreamWriter wr = new OutputStreamWriter(urlConn
+ .getOutputStream());
wr.write(_buf.toString());
wr.flush();
// Get the response
- BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
- while ((line = rd.readLine()) != null) {
+ BufferedReader rd = new BufferedReader(new InputStreamReader(
+ urlConn.getInputStream()));
+ while ((line = rd.readLine()) != null)
+ {
response.add(line);
- }
- } catch (Exception ex) {
- return;
- }
- }
-
- public List<String> getResponse()
+ }
+ } catch (Exception ex)
{
- return response;
- }
-
-}
+ return;
+ }
+ }
+
+ /**
+ * Retrieves the response from the http server
+ * @return List<String> response received from the http server
+ */
+ public List<String> getResponse()
+ {
+ return response;
+ }
+}
diff --git a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/ActivatorTest.java b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/ActivatorTest.java deleted file mode 100644 index 69d569797e..0000000000 --- a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/ActivatorTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * - * 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.qpid.info.test; - -import junit.framework.TestCase; -import org.apache.qpid.info.Activator; - -/* - * This test verifies whether the activator for the info service is starting Ok. - */ -public class ActivatorTest extends TestCase -{ - private Activator activator; - - protected void setUp() throws Exception - { - super.setUp(); - activator = new Activator(); - activator.start(null); - } - - protected void tearDown() throws Exception - { - super.tearDown(); - activator = null; - } - - public void testStart() - { - assertNotNull(activator); - } - - public void testGetBundleContext() - { - assertEquals(activator.getBundleContext(), null); - } - -} diff --git a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/BrokerInfoServiceImplTest.java b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/BrokerInfoServiceImplTest.java deleted file mode 100644 index 1eaa491603..0000000000 --- a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/BrokerInfoServiceImplTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * - * 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.qpid.info.test; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import org.apache.qpid.info.BrokerInfoServiceImpl; -import org.apache.qpid.info.Info; -import junit.framework.TestCase; - -/* - * This test verifies the invoke() method for the info service making sure that the parameters are returned - */ -public class BrokerInfoServiceImplTest extends TestCase -{ - - BrokerInfoServiceImpl bisi = null; - - public void testBrokerInfoServiceImpl() - { - bisi = new BrokerInfoServiceImpl(null); - assertNotNull(bisi); - } - - @SuppressWarnings("unchecked") - public void testInvoke() - { - bisi = new BrokerInfoServiceImpl(null); - assertNotNull(bisi); - Info<? extends Map<String, String>> info = (Info<? extends Map<String, String>>) bisi - .invoke(); - assertNotNull(info); - Properties props = info.toProps(); - assertNotNull(props); - List<String> qpidProps = Arrays.asList("java.class.path", - "java.vm.name", "java.class.version", "os.arch", "os.name", - "os.version", "sun.arch.data.model", "user.dir", "user.name", - "user.timezone"); - for (String tag : qpidProps) - { - assertNotNull(props.getProperty(tag)); - } - } - -} diff --git a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/HttpPosterTest.java b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/HttpPosterTest.java index bb0ee9d259..6be421dd7d 100644 --- a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/HttpPosterTest.java +++ b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/HttpPosterTest.java @@ -35,52 +35,73 @@ import junit.framework.TestCase; public class HttpPosterTest extends TestCase { - private HttpPoster hp; - - private Properties props; - - private StringBuffer sb; - private ServletTester tester; private String baseURL; private final String contextPath = "/info"; - protected void setUp() throws Exception + /* + * This method generates a dummy HttpPoster with a dummy body containing a + * single line. The url we are posting to can be controlled by the parameter + * url + * + * @param url + */ + private HttpPoster getHttpPoster(String url) { - super.setUp(); + StringBuffer sb = new StringBuffer("test=TEST"); + Properties props = new Properties(); + props.put("http.url", url); + return new HttpPoster(props, sb); + } + /* + * (non-Javadoc) + * + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception + { tester = new ServletTester(); tester.setContextPath("/"); tester.addServlet(InfoServlet.class, contextPath); baseURL = tester.createSocketConnector(true); tester.start(); - // - props = new Properties(); - props.put("URL", baseURL + contextPath); - props.put("hostname", "localhost"); - sb = new StringBuffer("test=TEST"); - hp = new HttpPoster(props, sb); - } + /* + * (non-Javadoc) + * + * @see junit.framework.TestCase#tearDown() + */ protected void tearDown() throws Exception { super.tearDown(); - hp = null; - props = null; - sb = null; tester.stop(); } - public void testHttpPoster() + /* + * This test is posting a string to an embedded Jetty Servlet and captures + * the response message. If the servlet receives the message ok, it will + * print Ok. A failure test is following where we post to a non-existent URL + */ + public void testHttpPoster() throws Exception { + // Test HttpPoster posts correctly to the servlet + HttpPoster hp = getHttpPoster(baseURL + contextPath); assertNotNull(hp); hp.run(); List<String> response = hp.getResponse(); assertTrue(response.size() > 0); assertEquals("OK <br>", response.get(0).toString()); + + // Failure Test + hp = getHttpPoster("http://localhost/nonexistent"); + hp.run(); + response = hp.getResponse(); + assertTrue(response.size() == 0); + } } diff --git a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoServlet.java b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoServlet.java index 075013f912..6b12a2d80c 100644 --- a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoServlet.java +++ b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoServlet.java @@ -1,5 +1,5 @@ /* - * + * * 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 @@ -7,17 +7,18 @@ * 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.qpid.info.test; import java.io.BufferedReader; @@ -53,4 +54,4 @@ public class InfoServlet extends GenericServlet System.out.println("ServletResponse: OK"); } -} +}
\ No newline at end of file diff --git a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoTest.java b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoTest.java index 2e9ac9a779..da3d844491 100644 --- a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoTest.java +++ b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/InfoTest.java @@ -1,5 +1,5 @@ /* - * + * * 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 @@ -7,17 +7,18 @@ * 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.qpid.info.test; import java.util.HashMap; @@ -39,6 +40,8 @@ public class InfoTest extends TestCase { super.setUp(); infoPayLoad = new HashMap<String, String>(); + infoPayLoad.put("test", "Test"); + info = new Info<HashMap<String, String>>(infoPayLoad); } protected void tearDown() throws Exception @@ -48,48 +51,44 @@ public class InfoTest extends TestCase infoPayLoad = null; } - public void testInfo() - { - info = new Info<HashMap<String, String>>(infoPayLoad); - assertNotNull(info); - } - + /* + * Test the conversion toString() of the Info object + */ public void testToString() { - infoPayLoad.clear(); - infoPayLoad.put("test", "Test"); - info = new Info<HashMap<String, String>>(infoPayLoad); - assertNotNull(info.toString()); - assertEquals("test=Test\n", info.toString()); + assertNotNull("toString() returned null", info.toString()); + assertEquals("toString() did not return the proper string", + "test=Test\n", info.toString()); } + /* + * Test the conversion toProps() of the Info object + */ public void testToProps() { Properties props = new Properties(); props.put("test", "Test"); - infoPayLoad.clear(); - infoPayLoad.put("test", "Test"); - info = new Info<HashMap<String, String>>(infoPayLoad); - assertNotNull(info.toProps()); - assertEquals(props, info.toProps()); + assertNotNull("toProperties() returned null", info.toProps()); + assertEquals("toProperties not returned the proper object", props, info + .toProps()); } + /* + * Test the conversion toStringBuffer() of the Info object + */ public void testToStringBuffer() { StringBuffer sb = new StringBuffer("test=Test\n"); - infoPayLoad.clear(); - infoPayLoad.put("test", "Test"); - info = new Info<HashMap<String, String>>(infoPayLoad); assertNotNull(info.toStringBuffer()); assertEquals(sb.toString(), info.toStringBuffer().toString()); } + /* + * Test conversion toXML() of the info object + */ public void testToXML() { String INDEND = " "; - infoPayLoad.clear(); - infoPayLoad.put("test", "Test"); - info = new Info<HashMap<String, String>>(infoPayLoad); StringBuffer sb = new StringBuffer(); sb.append("<?xml version=\"1.0\"?>\n"); sb.append("<qpidinfo>\n"); @@ -97,7 +96,24 @@ public class InfoTest extends TestCase sb.append(INDEND + "Test\n"); sb.append("</test>\n"); sb.append("</qpidinfo>\n"); - assertEquals(info.toXML().toString(), sb.toString()); + assertEquals("toString() does not return the proper string", info + .toXML().toString(), sb.toString()); + } + + /* + * Test the conversion toMap() of the Info object + */ + public void testToMap() + { + HashMap<String, String> thm = info.toMap(); + assertFalse("toMap() returned empty map", thm.isEmpty()); + assertEquals("testToMap did not returned 1", 1, thm.size()); + assertTrue("toMap() returned a map not containing expected key: test", + thm.containsKey("test")); + assertTrue( + "toMap() returned a map not containing the value for key test: Test", + thm.containsValue("Test")); + } } diff --git a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/XMLWriterTest.java b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/XMLWriterTest.java index 980e6dfa56..f3e2e1d55d 100644 --- a/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/XMLWriterTest.java +++ b/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/XMLWriterTest.java @@ -1,5 +1,5 @@ /* - * + * * 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 @@ -7,17 +7,18 @@ * 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.qpid.info.test; import java.util.HashMap; @@ -34,23 +35,11 @@ public class XMLWriterTest extends TestCase private XMLWriter xw = null; - protected void setUp() throws Exception - { - super.setUp(); - - } - - protected void tearDown() throws Exception - { - super.tearDown(); - - } - public void testXMLWriter() { xw = new XMLWriter(new StringBuffer("Test")); - assertNotNull(xw); - assertEquals("Test", xw.getXML().toString()); + assertNotNull("XMLWriter could not instantiate", xw); + assertEquals("XMLWriter.toString() failed","Test", xw.getXML().toString()); } public void testWriteXMLHeader() @@ -58,16 +47,16 @@ public class XMLWriterTest extends TestCase xw = new XMLWriter(new StringBuffer()); assertNotNull(xw); xw.writeXMLHeader(); - assertEquals("<?xml version=\"1.0\"?>\n", xw.getXML().toString()); + assertEquals("XMLWriter.writeXMLHeader(...) failed","<?xml version=\"1.0\"?>\n", xw.getXML().toString()); } public void testWriteTag() { String INDEND = " "; xw = new XMLWriter(new StringBuffer()); - assertNotNull(xw); + assertNotNull("XMLWriter could not instantiate", xw); xw.writeTag("test", new HashMap<String, String>(), "TEST"); - assertEquals("<test>\n" + INDEND + "TEST\n" + "</test>\n", xw.getXML() + assertEquals("XMLWriter.writeTag(...) failed","<test>\n" + INDEND + "TEST\n" + "</test>\n", xw.getXML() .toString()); } @@ -77,10 +66,10 @@ public class XMLWriterTest extends TestCase assertNotNull(xw); HashMap<String, String> attr = new HashMap<String, String>(); xw.writeOpenTag("test", attr); - assertEquals("<test>\n", xw.getXML().toString()); + assertEquals("XMLWriter.writeOpenTag(...) failed","<test>\n", xw.getXML().toString()); attr.put("id", "1"); xw.writeOpenTag("test1", attr); - assertEquals("<test>\n" + "<test1 id=\"1\">\n", xw.getXML().toString()); + assertEquals("XMLWriter.writeOpenTag(...) failed","<test>\n" + "<test1 id=\"1\">\n", xw.getXML().toString()); } public void testWriteCloseTag() |
