summaryrefslogtreecommitdiff
path: root/qpid/dotnet/Qpid.Integration.Tests/framework/LocalCircuitFactory.csx
blob: 45ecf26ffec371204eb17fa9709e4daa077b5901 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *
 * 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.
 *
 */
using log4net;

using Apache.Qpid.Integration.Tests.framework.localcircuit;//.LocalCircuitImpl;
//using Apache.Qpid.Integration.Tests.framework.localcircuit.LocalPublisherImpl;
//using Apache.Qpid.Integration.Tests.framework.localcircuit.LocalReceiverImpl;
//using Apache.Qpid.Integration.Tests.framework.sequencers.CircuitFactory;
//using org.apache.qpid.util.ConversationFactory;

//using uk.co.thebadgerset.junit.extensions.util.ParsedProperties;

//using javax.jms.*;

using System.Collections.Generic;//.IList;
//using java.util.Properties;
//using java.util.concurrent.atomic.AtomicLong;

namespace Apache.Qpid.Integration.Tests.framework
{
    /// <summary>
    /// LocalCircuitFactory is a circuit factory that creates test circuits with publishing and receiving ends rooted
    /// on the same JVM. The ends of the circuit are presented as <see cref="Publisher"/> and <see cref="Receiver"/> interfaces, which
    /// in turn provide methods to apply assertions to the circuit. The creation of the circuit ends, and the presentation
    /// of the ends as publisher/receiver interfaces, are designed to be overriden, so that circuits and assertions that
    /// use messaging features not available in JMS can be written. This provides an extension point for writing tests
    /// against proprietary features of JMS implementations.
    ///
    /// <p/><table id="crc"><caption>CRC Card</caption>
    /// <tr><th> Responsibilities <th> Collaborations
    /// <tr><td> Provide a standard test procedure over a test circuit.
    /// <tr><td> Construct test circuits appropriate to a tests context.
    /// </table>
    /// </summary>
    public class LocalCircuitFactory : CircuitFactory
    {
        /// <summary> Used for debugging. </summary>
        private static ILog log = LogManager.GetLogger(typeof(LocalCircuitFactory));

        /// <summary> Used to create unique destination names for each test. </summary>
        protected static AtomicLong uniqueDestsId = new AtomicLong();

        /// <summary>
        /// Holds a test coordinating conversation with the test clients. This should consist of assigning the test roles,
        /// begining the test and gathering the test reports from the participants.
        /// </summary>
        /// <param name="testCircuit">    The test circuit. </param>
        /// <param name="assertions">     The list of assertions to apply to the test circuit. </param>
        /// <param name="testProperties"> The test case definition. </param>
        public void sequenceTest(Circuit testCircuit, IList<Assertion> assertions, Properties testProperties)
        {
            FrameworkBaseCase.assertNoFailures(testCircuit.test(1, assertions));
        }

        /// <summary>
        /// Creates a test circuit for the test, configered by the test parameters specified.
        /// </summary>
        /// <param name="testProperties"> The test parameters. </param>
        ///
        /// <return> A test circuit. </return>
        public Circuit createCircuit(TestModel testProperties)
        {
            Circuit result;

            // Create a standard publisher/receivers test client pair on a shared connection, individual sessions.
            try
            {
                // Get a unique offset to append to destination names to make them unique to the connection.
                long uniqueId = uniqueDestsId.incrementAndGet();

                // Set up the connection.
                Connection connection = TestUtils.createConnection(testProperties);

                // Add the connection exception listener to assert on exception conditions with.
                // ExceptionMonitor exceptionMonitor = new ExceptionMonitor();
                // connection.setExceptionListener(exceptionMonitor);

                // Set up the publisher.
                CircuitEndBase publisherEnd = createPublisherCircuitEnd(connection, testProps, uniqueId);

                // Set up the receiver.
                CircuitEndBase receiverEnd = createReceiverCircuitEnd(connection, testProps, uniqueId);

                // Start listening for incoming messages.
                connection.start();

                // Namespace everything up.
                LocalPublisherImpl publisher = createPublisherFromCircuitEnd(publisherEnd);
                LocalReceiverImpl receiver = createReceiverFromCircuitEnd(receiverEnd);

                result = new LocalCircuitImpl(testProperties, publisher, receiver, connection, publisher.getExceptionMonitor());
            }
            catch (JMSException e)
            {
                throw new RuntimeException("Could not create publisher/receivers pair due to a JMSException.", e);
            }

            return result;
        }

        /// <summary>
        /// Creates a local <see cref="Receiver"/> from a <see cref="CircuitEnd"/>. Sub-classes may override this to provide more
        /// specialized receivers if necessary.
        /// </summary>
        /// <param name="receiverEnd"> The receiving circuit end. </param>
        ///
        /// <return> A <see cref="Receiver"/>. </return>
        protected LocalReceiverImpl createReceiverFromCircuitEnd(CircuitEndBase receiverEnd)
        {
            return new LocalReceiverImpl(receiverEnd);
        }

        /// <summary>
        /// Creates a local <see cref="Publisher"/> from a <see cref="CircuitEnd"/>. Sub-classes may override this to provide more
        /// specialized receivers if necessary.
        /// </summary>
        /// <param name="publisherEnd"> The publishing circuit end. </param>
        ///
        /// <return> A <see cref="Receiver"/>. </return>
        protected LocalPublisherImpl createPublisherFromCircuitEnd(CircuitEndBase publisherEnd)
        {
            return new LocalPublisherImpl(publisherEnd);
        }

        /// <summary>
        /// Builds a circuit end suitable for the publishing side of a test circuit, from standard test parameters.
        /// </summary>
        /// <param name="connection"> The connection to build the circuit end on. </param>
        /// <param name="testProps">  The test parameters to configure the circuit end construction. </param>
        /// <param name="uniqueId">   A unique number to being numbering destinations from, to make this circuit unique. </param>
        ///
        /// <return> A circuit end suitable for the publishing side of a test circuit. </return>
        ///
        /// <exception cref="JMSException"> Any underlying JMSExceptions are allowed to fall through and fail the creation. </exception>
        public CircuitEndBase createPublisherCircuitEnd(Connection connection, TestModel testProps, long uniqueId)
            throws JMSException
        {
            log.debug(
                      "public CircuitEndBase createPublisherCircuitEnd(Connection connection, TestModel testProps, long uniqueId = "
                      + uniqueId + "): called");

            // Check that the test properties do not contain AMQP/Qpid specific settings, and fail if they do.
            if (testProps.getImmediate() || testProps.getMandatory())
            {
                throw new RuntimeException(
                                           "Cannot create a pure JMS circuit as the test properties require AMQP specific options.");
            }

            Session session = connection.createSession(testProps.getPublisherTransacted(), testProps.getAckMode());

            Destination destination =
                testProps.getPubsub() ? session.createTopic(testProps.getSendDestinationNameRoot() + "_" + uniqueId)
                : session.createQueue(testProps.getSendDestinationNameRoot() + "_" + uniqueId);

            MessageProducer producer = testProps.getPublisherProducerBind() ? session.createProducer(destination) : null;

            MessageConsumer consumer =
                testProps.getPublisherConsumerBind()
                ? session.createConsumer(session.createQueue(testProps.getReceiveDestinationNameRoot() + "_" + uniqueId)) : null;

            MessageMonitor messageMonitor = new MessageMonitor();

            if (consumer != null)
            {
                consumer.setMessageListener(messageMonitor);
            }

            ExceptionMonitor exceptionMonitor = new ExceptionMonitor();
            connection.setExceptionListener(exceptionMonitor);

            if (!testProps.getPublisherConsumerActive() && (consumer != null))
            {
                consumer.close();
            }

            return new CircuitEndBase(producer, consumer, session, messageMonitor, exceptionMonitor);
        }

        /// <summary>
        /// Builds a circuit end suitable for the receiving side of a test circuit, from standard test parameters.
        /// </summary>
        /// <param name="connection"> The connection to build the circuit end on. </param>
        /// <param name="testProps">  The test parameters to configure the circuit end construction. </param>
        /// <param name="uniqueId">   A unique number to being numbering destinations from, to make this circuit unique. </param>
        ///
        /// <return> A circuit end suitable for the receiving side of a test circuit. </return>
        ///
        /// <exception cref="JMSException"> Any underlying JMSExceptions are allowed to fall through and fail the creation. </exception>
        public CircuitEndBase createReceiverCircuitEnd(Connection connection, TestModel testProps, long uniqueId)
            throws JMSException
        {
            log.debug(
                      "public CircuitEndBase createReceiverCircuitEnd(Connection connection, TestModel testProps, long uniqueId = "
                      + uniqueId + "): called");

            // Check that the test properties do not contain AMQP/Qpid specific settings, and fail if they do.
            if (testProps.getImmediate() || testProps.getMandatory())
            {
                throw new RuntimeException(
                                           "Cannot create a pure JMS circuit as the test properties require AMQP specific options.");
            }

            Session session = connection.createSession(testProps.getPublisherTransacted(), testProps.getAckMode());

            MessageProducer producer =
                testProps.getReceiverProducerBind()
                ? session.createProducer(session.createQueue(testProps.getReceiveDestinationNameRoot() + "_" + uniqueId)) : null;

            Destination destination =
                testProps.getPubsub() ? session.createTopic(testProps.getSendDestinationNameRoot() + "_" + uniqueId)
                : session.createQueue(testProps.getSendDestinationNameRoot() + "_" + uniqueId);

            MessageConsumer consumer =
                testProps.getReceiverConsumerBind()
                ? ((testProps.getDurableSubscription() && testProps.getPubsub())
                   ? session.createDurableSubscriber((Topic) destination, "testsub") : session.createConsumer(destination))
                : null;

            MessageMonitor messageMonitor = new MessageMonitor();

            if (consumer != null)
            {
                consumer.setMessageListener(messageMonitor);
            }

            if (!testProps.getReceiverConsumerActive() && (consumer != null))
            {
                consumer.close();
            }

            return new CircuitEndBase(producer, consumer, session, messageMonitor, null);
        }

        /*
        /// <summary>
        /// Sets the sender test client to coordinate the test with.
        /// </summary>
        /// <param name="sender"> The contact details of the sending client in the test. </param>
        public void setSender(TestClientDetails sender)
        {
            throw new RuntimeException("Not implemented.");
        }

        /// <summary>
        /// Sets the receiving test client to coordinate the test with.
        /// </summary>
        /// <param name="receiver"> The contact details of the sending client in the test. </param>
        public void setReceiver(TestClientDetails receiver)
        {
            throw new RuntimeException("Not implemented.");
        }

        /// <summary>
        /// Supplies the sending test client.
        /// </summary>
        /// <return> The sending test client. </return>
        public TestClientDetails getSender()
        {
            throw new RuntimeException("Not implemented.");
        }

        /// <summary>
        /// Supplies the receiving test client.
        /// </summary>
        /// <return> The receiving test client. </return>
        public IList<TestClientDetails> getReceivers()
        {
            throw new RuntimeException("Not implemented.");
        }
        */

        /*
        /// <summary>
        /// Accepts the conversation factory over which to hold the test coordinating conversation.
        /// </summary>
        /// <param name="conversationFactory"> The conversation factory to coordinate the test over. </param>
        public void setConversationFactory(ConversationFactory conversationFactory)
        {
            throw new RuntimeException("Not implemented.");
        }
        */
    }
}