summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/test/framework/TestUtils.java
blob: 919faa4754ceca03f9dbd3c9ef89947761de10c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 *
 * 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.test.framework;

import org.apache.log4j.Logger;

import org.apache.qpid.junit.extensions.util.ParsedProperties;

import static org.apache.qpid.test.framework.MessagingTestConfigProperties.BROKER_PROPNAME;
import static org.apache.qpid.test.framework.MessagingTestConfigProperties.CONNECTION_NAME;
import static org.apache.qpid.test.framework.MessagingTestConfigProperties.PASSWORD_PROPNAME;
import static org.apache.qpid.test.framework.MessagingTestConfigProperties.USERNAME_PROPNAME;
import static org.apache.qpid.test.framework.MessagingTestConfigProperties.VIRTUAL_HOST_PROPNAME;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Map;

/**
 * TestUtils provides static helper methods that are usefull for writing tests against QPid.
 *
 * <p/><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Create connections from test properties. <td> {@link MessagingTestConfigProperties}
 * <tr><td> Create test messages.
 * <tr><td> Inject a short pause in a test.
 * <tr><td> Serialize properties into a message.
 * </table>
 */
public class TestUtils
{
    /** Used for debugging. */
    private static Logger log = Logger.getLogger(TestUtils.class);

    /** Some dummy data to stuff all test messages with. */
    private static final byte[] MESSAGE_DATA_BYTES =
        "Test Message -- Test Message -- Test Message -- Test Message -- Test Message -- Test Message -- Test Message -- "
        .getBytes();

    /**
     * Establishes a JMS connection using a set of properties and qpids built in JNDI implementation. This is a simple
     * convenience method for code that does not anticipate handling connection failures. All exceptions that indicate
     * that the connection has failed, are wrapped as rutime exceptions, presumably handled by a top level failure
     * handler.
     *
     * <p/>This utility makes use of the following test parameters from {@link MessagingTestConfigProperties} to control
     * the connection creation:
     *
     * <p/><table>
     * <tr><td> {@link MessagingTestConfigProperties#USERNAME_PROPNAME} <td> The username.
     * <tr><td> {@link MessagingTestConfigProperties#PASSWORD_PROPNAME} <td> The password.
     * <tr><td> {@link MessagingTestConfigProperties#VIRTUAL_HOST_PROPNAME} <td> The virtual host name.
     * <tr><td> {@link MessagingTestConfigProperties#BROKER_PROPNAME} <td> The broker URL.
     * <tr><td> {@link MessagingTestConfigProperties#CONNECTION_NAME} <td> The broker name in the initial context.
     *
     * @param messagingProps Connection properties as defined in {@link MessagingTestConfigProperties}.
     *
     * @return A JMS conneciton.
     */
    public static Connection createConnection(ParsedProperties messagingProps)
    {
        log.debug("public static Connection createConnection(ParsedProperties messagingProps = " + messagingProps
            + "): called");

        try
        {
            // Extract the configured connection properties from the test configuration.
            String conUsername = messagingProps.getProperty(USERNAME_PROPNAME);
            String conPassword = messagingProps.getProperty(PASSWORD_PROPNAME);
            String virtualHost = messagingProps.getProperty(VIRTUAL_HOST_PROPNAME);
            String brokerUrl = messagingProps.getProperty(BROKER_PROPNAME);

            // Create the broker connection url.
            String connectionString =
                "amqp://" + conUsername + ":" + conPassword + "@clientid/" + ((virtualHost != null) ? virtualHost : "")
                + "?brokerlist='" + brokerUrl + "'";

            // Create properties to create the initial context from, and inject the connection factory configuration
            // for the defined connection name into it.
            messagingProps.setProperty("connectionfactory." + CONNECTION_NAME, connectionString);

            Context ctx = new InitialContext(messagingProps);

            ConnectionFactory cf = (ConnectionFactory) ctx.lookup(CONNECTION_NAME);

            return cf.createConnection();
        }
        catch (NamingException e)
        {
            throw new RuntimeException("Got JNDI NamingException whilst looking up the connection factory.", e);
        }
        catch (JMSException e)
        {
            throw new RuntimeException("Could not establish connection due to JMSException.", e);
        }
    }

    /**
     * Creates a test message of the specified size, on the given JMS session.
     *
     * @param session The JMS session.
     * @param size    The size of the message in bytes.
     *
     * @return A bytes message, of the specified size, filled with dummy data.
     *
     * @throws JMSException Any underlying JMSExceptions are allowed to fall through.
     */
    public static Message createTestMessageOfSize(Session session, int size) throws JMSException
    {
        BytesMessage message = session.createBytesMessage();

        if (size > 0)
        {
            int div = size / MESSAGE_DATA_BYTES.length;
            int mod = size % MESSAGE_DATA_BYTES.length;

            for (int i = 0; i < div; i++)
            {
                message.writeBytes(MESSAGE_DATA_BYTES);
            }

            if (mod != 0)
            {
                message.writeBytes(MESSAGE_DATA_BYTES, 0, mod);
            }
        }

        return message;
    }

    /**
     * Pauses for the specified length of time. In the event of failing to pause for at least that length of time
     * due to interuption of the thread, a RutimeException is raised to indicate the failure. The interupted status
     * of the thread is restores in that case. This method should only be used when it is expected that the pause
     * will be succesfull, for example in test code that relies on inejecting a pause.
     *
     * @param t The minimum time to pause for in milliseconds.
     */
    public static void pause(long t)
    {
        try
        {
            Thread.sleep(t);
        }
        catch (InterruptedException e)
        {
            // Restore the interrupted status
            Thread.currentThread().interrupt();

            throw new RuntimeException("Failed to generate the requested pause length.", e);
        }
    }

    /**
     * Sets properties of different types on a JMS Message.
     *
     * @param message    The message to set properties on.
     * @param properties The property name/value pairs to set.
     *
     * @throws javax.jms.JMSException All underlying JMSExceptions are allowed to fall through.
     *
     * @todo Move this helper method somewhere else. For example, TestUtils.
     */
    public static void setPropertiesOnMessage(Message message, Map<Object, Object> properties) throws JMSException
    {
        for (Map.Entry<Object, Object> entry : properties.entrySet())
        {
            String name = entry.getKey().toString();
            Object value = entry.getValue();

            message.setObjectProperty(name, value);
        }
    }
}