From 9bb2705c314cfc8db2a28aad1a6a1408294247ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Blu=CC=88mke?= <lbl@informatik.uni-kiel.de> Date: Thu, 12 May 2016 15:22:57 +0200 Subject: [PATCH] first try on JMSReaderTest --- .../plugin/reader/jms/JMSReaderTest.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/test/java/kieker/analysis/plugin/reader/jms/JMSReaderTest.java diff --git a/src/test/java/kieker/analysis/plugin/reader/jms/JMSReaderTest.java b/src/test/java/kieker/analysis/plugin/reader/jms/JMSReaderTest.java new file mode 100644 index 00000000..643b39bf --- /dev/null +++ b/src/test/java/kieker/analysis/plugin/reader/jms/JMSReaderTest.java @@ -0,0 +1,113 @@ +package kieker.analysis.plugin.reader.jms; + +import static teetime.framework.test.StageTester.test; + +import java.util.Hashtable; +import java.util.List; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NameNotFoundException; +import javax.naming.NamingException; + +import org.junit.Before; +import org.junit.Test; + +import kieker.common.logging.Log; +import kieker.common.logging.LogFactory; +import kieker.common.record.system.CPUUtilizationRecord; + +public class JMSReaderTest { + + private CPUUtilizationRecord record; + private JMSReader jmsReader; + + // JMSReader constructor arguments + private final String jmsProviderUrl = "tcp://localhost:3035/"; + private final String jmsDestination = "queue1"; + private final String jmsFactoryLookupName = "org.exolab.jms.jndi.InitialContextFactory"; + private final Log log = LogFactory.getLog(this.getClass().getName());; + + // Record data + private final long timestamp = 1L; + private final String hostname = "test_host"; + private final String cpuID = "cpu_1"; + private final double user = 2.0; + private final double system = 3.0; + private final double wait = 4.0; + private final double nice = 5.0; + private final double irq = 6.0; + private final double totalUtilisation = 7.0; + private final double idle = 8.0; + + @Before + public void initializeJMSReader() { + jmsReader = new JMSReader(jmsProviderUrl, jmsDestination, jmsFactoryLookupName, log); + record = new CPUUtilizationRecord(timestamp, hostname, cpuID, user, system, wait, nice, irq, totalUtilisation, idle); + + } + + @Test + public void jmsReaderShouldReadCorrectRecords() { + produceJMSMessage(); + List<Object> outputList = null; + test(jmsReader).and().receive(outputList).from(jmsReader.getOutputPort()).start(); + + if (outputList != null) { + for (Object elem : outputList) { + System.out.println("Received" + elem); + } + } + + } + + public void produceJMSMessage() { + + try { + final Hashtable<String, String> properties = new Hashtable<String, String>(); // NOPMD NOCS (InitialContext expects Hashtable) + properties.put(Context.INITIAL_CONTEXT_FACTORY, this.jmsFactoryLookupName); + + // JMS initialization + properties.put(Context.PROVIDER_URL, this.jmsProviderUrl); + final Context context = new InitialContext(properties); + final ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory"); + Connection connection; + connection = factory.createConnection(); + final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + Destination destination; + try { + // As a first step, try a JNDI lookup (this seems to fail with ActiveMQ sometimes) + destination = (Destination) context.lookup(this.jmsDestination); + } catch (final NameNotFoundException exc) { + // JNDI lookup failed, try manual creation (this seems to fail with ActiveMQ/HornetQ sometimes) + destination = session.createQueue(this.jmsDestination); + if (destination == null) { // + this.log.error("Failed to lookup queue '" + this.jmsDestination + "' via JNDI: " + exc.getMessage() + " AND failed to create queue"); + throw exc; // will be catched below to abort the read method + } + } + + // Send a monitoring record + ObjectMessage msg = session.createObjectMessage(record); + + MessageProducer producer = session.createProducer(destination); + producer.send(msg); + + connection.close(); + + } catch (NamingException ne) { + ne.printStackTrace(); + } catch (JMSException e) { + e.printStackTrace(); + } + } + +} -- GitLab