This closes #6 update on hacking guide plus examples

This commit is contained in:
Clebert Suconic 2015-05-28 15:05:34 -04:00
commit 873751fd45
3 changed files with 219 additions and 2 deletions

View File

@ -22,7 +22,7 @@ The broker is comprised of POJOs so it's simple to configure and run a broker in
Even complex test-cases involving multiple clustered brokers are relatively easy to write. Almost every test in the
test-suite follows this pattern - configure broker, start broker, test functionality, stop broker.
The test-suite uses JUnit to manage test execution and life-cycle. Most tests extend [org.apache.activemq.artemis.tests.util.ActiveMQTestBase](../../../artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java)
The test-suite uses JUnit to manage test execution and life-cycle. Most tests extend [`org.apache.activemq.artemis.tests.util.ActiveMQTestBase`](../../../artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java)
which contains JUnit setup and tear-down methods as well as a wealth of utility functions to configure, start, manage,
and stop brokers as well as perform other common tasks.
@ -64,6 +64,15 @@ to add the factory to the test-suite's internal resource ledger.
There are numerous methods in `org.apache.activemq.artemis.tests.util.ActiveMQTestBase` to create a configuration. These
methods are named like create*Config(..). Each one creates a slightly different configuration but there is a lot of
overlap between them.
overlap between them.
In any case, `org.apache.activemq.artemis.core.config.Configuration` is a [_fluent_](http://en.wikipedia.org/wiki/Fluent_interface)
interface so it's easy to customize however you need.
### Look at other test-cases
If you need ideas on how to configure something or test something try looking through the test-suite at other test-cases
which may be similar. This is one of the best ways to learn how the test-suite works and how you can leverage the
testing infrastructure to test your particular case.

View File

@ -0,0 +1,126 @@
/**
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.artemis.tests.integration;
import org.apache.activemq.artemis.api.core.client.ClientConsumer;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientProducer;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.junit.Before;
import org.junit.Test;
import java.util.UUID;
/**
* A simple test-case used for documentation purposes.
*/
public class SimpleTest extends ActiveMQTestBase
{
protected ActiveMQServer server;
protected ClientSession session;
protected ClientSessionFactory sf;
protected ServerLocator locator;
@Override
@Before
public void setUp() throws Exception
{
/**
* Invoke org.apache.activemq.artemis.tests.util.ActiveMQTestBase's setUp() to bootstrap everything properly.
*/
super.setUp();
/**
* Create a configuration for an in-vm server.
* Use that configuration to instantiate a new server that doesn't use persistence, and then start it.
* Note that creating the server instance using this method ensures that the server will be cleaned up properly
* when the test is torn down.
*/
server = createServer(false, createDefaultInVMConfig());
server.start();
/**
* Create a ServerLocator for the in-vm server. Using this method instead of using, e.g. ActiveMQClient.createServerLocatorWithHA(..),
* ensures that the locator will be cleaned up properly when the test is torn down.
*/
locator = createInVMNonHALocator();
/**
* Create a session factory from the server locator. Using this method instead of using, e.g. ServerLocator.createSessionFactory(),
* ensures that the factory will be cleaned up properly when the test is torn down.
*/
sf = createSessionFactory(locator);
/**
* Create a session from the factory. The call to create the session is surrounded with addClientSession to
* ensure the session will be cleaned up properly when the test is torn down.
*/
session = addClientSession(sf.createSession(false, true, true));
}
@Test
public void simpleTest() throws Exception
{
final String data = "Simple Text " + UUID.randomUUID().toString();
final String queueName = "simpleQueue";
final String addressName = "simpleAddress";
// Create a queue bound to a particular address where the test will send to & consume from.
session.createQueue(addressName, queueName);
// Create a producer to send a message to the previously created address.
ClientProducer producer = session.createProducer(addressName);
// Create a non-durable message.
ClientMessage message = session.createMessage(false);
// Put some data into the message.
message.getBodyBuffer().writeString(data);
// Send the message. This send will be auto-committed based on the way the session was created in setUp()
producer.send(message);
// Close the producer.
producer.close();
// Create a consumer on the queue bound to the address where the message was sent.
ClientConsumer consumer = session.createConsumer(queueName);
// Start the session to allow messages to be consumed.
session.start();
// Receive the message we sent previously.
message = consumer.receive(1000);
// Ensure the message was received.
assertNotNull(message);
// Acknowledge the message.
message.acknowledge();
// Ensure the data in the message received matches the data in the message sent.
assertEquals(data, message.getBodyBuffer().readString());
}
}

View File

@ -0,0 +1,82 @@
/**
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.artemis.tests.integration;
import org.apache.activemq.artemis.api.core.client.ClientConsumer;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientProducer;
import org.apache.activemq.artemis.tests.util.SingleServerTestBase;
import org.junit.Test;
import java.util.UUID;
/**
* A simple test-case used for documentation purposes.
*/
public class SingleServerSimpleTest extends SingleServerTestBase
{
/**
* Because this class extends org.apache.activemq.artemis.tests.util.SingleServerTestBase and only uses a single
* instance of ActiveMQServer then no explicit setUp is required. The class simply needs tests which will use
* the server.
*/
@Test
public void simpleTest() throws Exception
{
final String data = "Simple Text " + UUID.randomUUID().toString();
final String queueName = "simpleQueue";
final String addressName = "simpleAddress";
// Create a queue bound to a particular address where the test will send to & consume from.
session.createQueue(addressName, queueName);
// Create a producer to send a message to the previously created address.
ClientProducer producer = session.createProducer(addressName);
// Create a non-durable message.
ClientMessage message = session.createMessage(false);
// Put some data into the message.
message.getBodyBuffer().writeString(data);
// Send the message. This send will be auto-committed based on the way the session was created in setUp()
producer.send(message);
// Close the producer.
producer.close();
// Create a consumer on the queue bound to the address where the message was sent.
ClientConsumer consumer = session.createConsumer(queueName);
// Start the session to allow messages to be consumed.
session.start();
// Receive the message we sent previously.
message = consumer.receive(1000);
// Ensure the message was received.
assertNotNull(message);
// Acknowledge the message.
message.acknowledge();
// Ensure the data in the message received matches the data in the message sent.
assertEquals(data, message.getBodyBuffer().readString());
}
}