ARTEMIS-2116 Extend tests on producer CLI command with message body

This commit is contained in:
feuillemorte 2018-10-26 17:04:35 +02:00 committed by Clebert Suconic
parent 036e24460b
commit 39e172c977
5 changed files with 285 additions and 89 deletions

View File

@ -34,7 +34,6 @@ import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.api.core.Pair;
import org.apache.activemq.artemis.api.core.SimpleString;
@ -94,14 +93,6 @@ public class ArtemisTest extends CliTestBase {
super.setup();
}
public void setupAuth() throws Exception {
setupAuth(temporaryFolder.getRoot());
}
public void setupAuth(File folder) throws Exception {
System.setProperty("java.security.auth.login.config", folder.getAbsolutePath() + "/etc/login.config");
}
@Test
public void invalidCliDoesntThrowException() {
testCli("--silent", "create");
@ -1050,12 +1041,6 @@ public class ArtemisTest extends CliTestBase {
return System.getProperty("os.name", "null").toLowerCase().indexOf("win") >= 0;
}
private void stopServer() throws Exception {
Artemis.internalExecute("stop");
assertTrue(Run.latchRunning.await(5, TimeUnit.SECONDS));
assertEquals(0, LibaioContext.getTotalMaxIO());
}
private static Document parseXml(File xmlFile) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

View File

@ -0,0 +1,156 @@
/*
* 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.cli.test;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class CliProducerTest extends CliTestBase {
private Connection connection;
private ActiveMQConnectionFactory cf;
private static final int TEST_MESSAGE_COUNT = 10;
@Before
@Override
public void setup() throws Exception {
setupAuth();
super.setup();
startServer();
cf = getConnectionFactory(61616);
connection = cf.createConnection("admin", "admin");
}
@After
@Override
public void tearDown() throws Exception {
closeConnection(cf, connection);
super.tearDown();
}
private void produceMessages(String address, String message, int msgCount) throws Exception {
Artemis.main("producer",
"--user", "admin",
"--password", "admin",
"--destination", address,
"--message", message,
"--message-count", String.valueOf(msgCount)
);
}
private void produceMessages(String address, int msgCount) throws Exception {
Artemis.main("producer",
"--user", "admin",
"--password", "admin",
"--destination", address,
"--message-count", String.valueOf(msgCount)
);
}
private void checkSentMessages(Session session, String address, String messageBody) throws Exception {
final boolean isCustomMessageBody = messageBody != null;
boolean fqqn = false;
if (address.startsWith("fqqn://")) fqqn = true;
List<Message> received = consumeMessages(session, address, TEST_MESSAGE_COUNT, fqqn);
for (int i = 0; i < TEST_MESSAGE_COUNT; i++) {
if (!isCustomMessageBody) messageBody = "test message: " + String.valueOf(i);
assertEquals(messageBody, ((TextMessage) received.get(i)).getText());
}
}
private Session createSession() throws JMSException {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection.start();
return session;
}
@Test
public void testSendMessage() throws Exception {
String address = "test";
Session session = createSession();
produceMessages(address, TEST_MESSAGE_COUNT);
checkSentMessages(session, address, null);
}
@Test
public void testSendMessageFQQN() throws Exception {
String address = "test";
String queue = "queue";
String fqqn = address + "::" + queue;
createQueue("--multicast", address, queue);
Session session = createSession();
produceMessages("topic://" + address, TEST_MESSAGE_COUNT);
checkSentMessages(session, fqqn, null);
}
@Test
public void testSendMessageCustomBodyFQQN() throws Exception {
String address = "test";
String queue = "queue";
String fqqn = address + "::" + queue;
String messageBody = new StringGenerator().generateRandomString(20);
createQueue("--multicast", address, queue);
Session session = createSession();
produceMessages("topic://" + address, messageBody, TEST_MESSAGE_COUNT);
checkSentMessages(session, fqqn, messageBody);
}
@Test
public void testSendMessageWithCustomBody() throws Exception {
String address = "test";
String messageBody = new StringGenerator().generateRandomString(20);
Session session = createSession();
produceMessages(address, messageBody, TEST_MESSAGE_COUNT);
checkSentMessages(session, address, messageBody);
}
@Test
public void testSendMessageWithCustomBodyLongString() throws Exception {
String address = "test";
String messageBody = new StringGenerator().generateRandomString(500000);
Session session = createSession();
produceMessages(address, messageBody, TEST_MESSAGE_COUNT);
checkSentMessages(session, address, messageBody);
}
}

View File

@ -17,8 +17,12 @@
package org.apache.activemq.cli.test;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.cli.commands.Run;
import org.apache.activemq.artemis.cli.commands.tools.LockAbstract;
import org.apache.activemq.artemis.jlibaio.LibaioContext;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.apache.activemq.artemis.spi.core.security.jaas.PropertiesLoader;
import org.apache.activemq.artemis.utils.ThreadLeakCheckRule;
import org.junit.After;
@ -26,7 +30,19 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class CliTestBase {
@ -66,4 +82,69 @@ public class CliTestBase {
LockAbstract.unlock();
}
protected void startServer() throws Exception {
File rootDirectory = new File(temporaryFolder.getRoot(), "broker");
setupAuth(rootDirectory);
Run.setEmbedded(true);
Artemis.main("create", rootDirectory.getAbsolutePath(), "--silent", "--no-fsync", "--no-autotune", "--no-web", "--require-login");
System.setProperty("artemis.instance", rootDirectory.getAbsolutePath());
Artemis.internalExecute("run");
}
protected void setupAuth() throws Exception {
setupAuth(temporaryFolder.getRoot());
}
protected void setupAuth(File folder) throws Exception {
System.setProperty("java.security.auth.login.config", folder.getAbsolutePath() + "/etc/login.config");
}
protected void stopServer() throws Exception {
Artemis.internalExecute("stop");
assertTrue(Run.latchRunning.await(5, TimeUnit.SECONDS));
assertEquals(0, LibaioContext.getTotalMaxIO());
}
protected ActiveMQConnectionFactory getConnectionFactory(int serverPort) throws Exception {
return new ActiveMQConnectionFactory("tcp://localhost:" + String.valueOf(serverPort));
}
protected void createQueue(String routingTypeOption, String address, String queueName) throws Exception {
Artemis.main("queue", "create",
"--user", "admin",
"--password", "admin",
"--address", address,
"--name", queueName,
routingTypeOption,
"--durable",
"--preserve-on-no-consumers",
"--auto-create-address");
}
protected void closeConnection(ActiveMQConnectionFactory cf, Connection connection) throws Exception {
try {
connection.close();
cf.close();
} finally {
stopServer();
}
}
protected List<Message> consumeMessages(Session session, String address, int noMessages, boolean fqqn) throws Exception {
Destination destination = fqqn ? session.createQueue(address) : getDestination(address);
MessageConsumer consumer = session.createConsumer(destination);
List<Message> messages = new ArrayList<>();
for (int i = 0; i < noMessages; i++) {
Message m = consumer.receive(1000);
assertNotNull(m);
messages.add(m);
}
return messages;
}
protected Destination getDestination(String queueName) {
return ActiveMQDestination.createDestination("queue://" + queueName, ActiveMQDestination.TYPE.QUEUE);
}
}

View File

@ -18,7 +18,6 @@ package org.apache.activemq.cli.test;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
@ -35,13 +34,9 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.cli.commands.Run;
import org.apache.activemq.artemis.jlibaio.LibaioContext;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.junit.After;
import org.junit.Before;
@ -50,7 +45,6 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Test to validate that the CLI doesn't throw improper exceptions when invoked.
@ -73,36 +67,8 @@ public class MessageSerializerTest extends CliTestBase {
@After
@Override
public void tearDown() throws Exception {
try {
connection.close();
cf.close();
} finally {
stopServer();
super.tearDown();
}
}
private void setupAuth() throws Exception {
setupAuth(temporaryFolder.getRoot());
}
private void setupAuth(File folder) throws Exception {
System.setProperty("java.security.auth.login.config", folder.getAbsolutePath() + "/etc/login.config");
}
private void startServer() throws Exception {
File rootDirectory = new File(temporaryFolder.getRoot(), "broker");
setupAuth(rootDirectory);
Run.setEmbedded(true);
Artemis.main("create", rootDirectory.getAbsolutePath(), "--silent", "--no-fsync", "--no-autotune", "--no-web", "--require-login");
System.setProperty("artemis.instance", rootDirectory.getAbsolutePath());
Artemis.internalExecute("run");
}
private void stopServer() throws Exception {
Artemis.internalExecute("stop");
assertTrue(Run.latchRunning.await(5, TimeUnit.SECONDS));
assertEquals(0, LibaioContext.getTotalMaxIO());
closeConnection(cf, connection);
super.tearDown();
}
private File createMessageFile() throws IOException {
@ -215,19 +181,6 @@ public class MessageSerializerTest extends CliTestBase {
}
}
private List<Message> consumeMessages(Session session, String address, int noMessages, boolean fqqn) throws Exception {
Destination destination = fqqn ? session.createQueue(address) : getDestination(address);
MessageConsumer consumer = session.createConsumer(destination);
List<Message> messages = new ArrayList<>();
for (int i = 0; i < noMessages; i++) {
Message m = consumer.receive(1000);
assertNotNull(m);
messages.add(m);
}
return messages;
}
private void exportMessages(String address, int noMessages, File output) throws Exception {
Artemis.main("consumer",
"--user", "admin",
@ -245,18 +198,6 @@ public class MessageSerializerTest extends CliTestBase {
"--data", input.getAbsolutePath());
}
private void createQueue(String routingTypeOption, String address, String queueName) throws Exception {
Artemis.main("queue", "create",
"--user", "admin",
"--password", "admin",
"--address", address,
"--name", queueName,
routingTypeOption,
"--durable",
"--preserve-on-no-consumers",
"--auto-create-address");
}
@Test
public void testSendDirectToQueue() throws Exception {
@ -344,21 +285,8 @@ public class MessageSerializerTest extends CliTestBase {
return lines;
}
private void sendMessages(Session session, String queueName, int messageCount) throws JMSException {
MessageProducer producer = session.createProducer(getDestination(queueName));
TextMessage message = session.createTextMessage(getTestMessageBody());
for (int i = 0; i < messageCount; i++) {
producer.send(message);
}
}
private String getTestMessageBody() {
return "Sample Message";
}
private Destination getDestination(String queueName) {
return ActiveMQDestination.createDestination("queue://" + queueName, ActiveMQDestination.TYPE.QUEUE);
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.cli.test;
import java.util.Random;
/**
* Generate a random string.
*/
class StringGenerator {
private String letters = "abcdefghijklmnopqrstuvwxyz";
private String digits = "0123456789";
private String symbols = "~!@#$%^&*()_+{}|?><,./";
private String nonLatinLetters = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
String generateRandomString(int length) {
String initialString = letters + letters.toUpperCase() + nonLatinLetters + nonLatinLetters.toUpperCase()
+ symbols + digits;
StringBuilder result = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
result.append(initialString.charAt(random.nextInt(initialString.length())));
}
return result.toString();
}
}