summaryrefslogtreecommitdiff
path: root/java/client/src/test/java/org/apache/qpid/transacted/TransactedTest.java
blob: 200c89bebe3878b2ca39809a38efab21fb4c3703 (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
/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed 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.transacted;

import junit.framework.JUnit4TestAdapter;
import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQQueue;
import org.apache.qpid.client.AMQSession;
import org.apache.qpid.client.transport.TransportConnection;
import org.apache.qpid.client.vmbroker.AMQVMBrokerCreationException;
import org.junit.After;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;

import javax.jms.*;

public class TransactedTest
{
    private AMQQueue queue1;
    private AMQQueue queue2;

    private AMQConnection con;
    private Session session;
    private MessageConsumer consumer;
    private MessageProducer producer;

    private AMQConnection prepCon;
    private Session prepSession;
    private MessageProducer prepProducer;

    private AMQConnection testCon;
    private Session testSession;
    private MessageConsumer testConsumer1;
    private MessageConsumer testConsumer2;

    @Before
    public void setup() throws Exception
    {
        try
        {
            TransportConnection.createVMBroker(1);
        }
        catch (AMQVMBrokerCreationException e)
        {
            Assert.fail("Unable to create VM Broker");
        }

        queue1 = new AMQQueue("Q1", false);
        queue2 = new AMQQueue("Q2", false);

        con = new AMQConnection("vm://:1", "guest", "guest", "TransactedTest", "/test");
        session = con.createSession(true, 0);
        consumer = session.createConsumer(queue1);
        producer = session.createProducer(queue2);
        con.start();

        prepCon = new AMQConnection("vm://:1", "guest", "guest", "PrepConnection", "/test");
        prepSession = prepCon.createSession(false, AMQSession.NO_ACKNOWLEDGE);
        prepProducer = prepSession.createProducer(queue1);
        prepCon.start();

        //add some messages
        prepProducer.send(prepSession.createTextMessage("A"));
        prepProducer.send(prepSession.createTextMessage("B"));
        prepProducer.send(prepSession.createTextMessage("C"));


        testCon = new AMQConnection("vm://:1", "guest", "guest", "TestConnection", "/test");
        testSession = testCon.createSession(false, AMQSession.NO_ACKNOWLEDGE);
        testConsumer1 = testSession.createConsumer(queue1);
        testConsumer2 = testSession.createConsumer(queue2);
        testCon.start();
    }

    @After
    public void shutdown() throws Exception
    {
        con.close();
        testCon.close();
        prepCon.close();

        TransportConnection.killVMBroker(1);
    }

    @Test
    public void commit() throws Exception
    {
        //send and receive some messages
        producer.send(session.createTextMessage("X"));
        producer.send(session.createTextMessage("Y"));
        producer.send(session.createTextMessage("Z"));
        expect("A", consumer.receive(1000));
        expect("B", consumer.receive(1000));
        expect("C", consumer.receive(1000));

        //commit
        session.commit();

        //ensure sent messages can be received and received messages are gone
        expect("X", testConsumer2.receive(1000));
        expect("Y", testConsumer2.receive(1000));
        expect("Z", testConsumer2.receive(1000));

        assertTrue(null == testConsumer1.receive(1000));
        assertTrue(null == testConsumer2.receive(1000));
    }

    @Test
    public void rollback() throws Exception
    {
        producer.send(session.createTextMessage("X"));
        producer.send(session.createTextMessage("Y"));
        producer.send(session.createTextMessage("Z"));
        expect("A", consumer.receive(1000));
        expect("B", consumer.receive(1000));
        expect("C", consumer.receive(1000));

        //rollback
        session.rollback();

        //ensure sent messages are not visible and received messages are requeued
        expect("A", consumer.receive(1000));
        expect("B", consumer.receive(1000));
        expect("C", consumer.receive(1000));

        assertTrue(null == testConsumer1.receive(1000));
        assertTrue(null == testConsumer2.receive(1000));
    }

    private void expect(String text, Message msg) throws JMSException
    {
        assertTrue(msg instanceof TextMessage);
        assertEquals(text, ((TextMessage) msg).getText());
    }

    public static junit.framework.Test suite()
    {
        return new JUnit4TestAdapter(TransactedTest.class);
    }

}