summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/server/store/SplitStoreTest.java
blob: 9f244e78a4a9373c807be1befc93d8e9f337a2f5 (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
/*
 *
 * 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.server.store;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.qpid.configuration.ClientProperties;
import org.apache.qpid.server.model.VirtualHost;
import org.apache.qpid.test.utils.QpidBrokerTestCase;
import org.apache.qpid.test.utils.TestBrokerConfiguration;
import org.apache.qpid.util.FileUtils;

public class SplitStoreTest extends QpidBrokerTestCase
{
    private String _messageStorePath;
    private String _configStorePath;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();

        String virtualHostWorkDir = System.getProperty("QPID_WORK") + File.separator + TestBrokerConfiguration.ENTRY_NAME_VIRTUAL_HOST + File.separator;
        _messageStorePath =  virtualHostWorkDir  + "messageStore";
        _configStorePath =  virtualHostWorkDir  + "configStore";
    }

    @Override
    public void startBroker() throws Exception
    {
        // Overridden to prevent QBTC starting the Broker.
    }

    public void testJsonConfigurationStoreWithPersistentMessageStore() throws Exception
    {
        Map<String, Object> configurationStoreSettings = new HashMap<String, Object>();
        configurationStoreSettings.put(DurableConfigurationStore.STORE_TYPE, JsonFileConfigStore.TYPE);
        configurationStoreSettings.put(DurableConfigurationStore.STORE_PATH, _configStorePath);

        doTest(configurationStoreSettings);
    }

    public void testSeparateConfigurationAndMessageStoresOfTheSameType() throws Exception
    {
        Map<String, Object> configurationStoreSettings = new HashMap<String, Object>();
        configurationStoreSettings.put(DurableConfigurationStore.STORE_TYPE, getTestProfileMessageStoreType());
        configurationStoreSettings.put(DurableConfigurationStore.STORE_PATH, _configStorePath);

        doTest(configurationStoreSettings);
    }

    private void configureAndStartBroker(Map<String, Object> configurationStoreSettings) throws Exception
    {
        Map<String, Object> messageStoreSettings = new HashMap<String, Object>();
        messageStoreSettings.put(MessageStore.STORE_TYPE, getTestProfileMessageStoreType());
        messageStoreSettings.put(MessageStore.STORE_PATH, _messageStorePath);

        TestBrokerConfiguration config = getBrokerConfiguration();
        config.setObjectAttribute(TestBrokerConfiguration.ENTRY_NAME_VIRTUAL_HOST, VirtualHost.MESSAGE_STORE_SETTINGS, messageStoreSettings);
        config.setObjectAttribute(TestBrokerConfiguration.ENTRY_NAME_VIRTUAL_HOST, VirtualHost.CONFIGURATION_STORE_SETTINGS, configurationStoreSettings);

        super.startBroker();
    }

    private void doTest(Map<String, Object> configurationStoreSettings) throws Exception
    {
        configureAndStartBroker(configurationStoreSettings);

        Connection connection = getConnection();
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        Queue queue = session.createQueue(getTestQueueName());
        session.createConsumer(queue).close(); // Create durable queue by side effect
        sendMessage(session, queue, 2);
        connection.close();

        restartBroker();

        setTestSystemProperty(ClientProperties.QPID_DECLARE_QUEUES_PROP_NAME, "false");
        connection = getConnection();
        connection.start();
        session = connection.createSession(true, Session.SESSION_TRANSACTED);
        MessageConsumer consumer = session.createConsumer(queue);
        Message message = consumer.receive(1000);
        session.commit();

        assertNotNull("Message was not received after first restart", message);
        assertEquals("Unexpected message received after first restart", 0, message.getIntProperty(INDEX));

        stopBroker();
        File messageStoreFile = new File(_messageStorePath);
        FileUtils.delete(messageStoreFile, true);
        assertFalse("Store folder was not deleted", messageStoreFile.exists());
        super.startBroker();

        connection = getConnection();
        connection.start();
        session = connection.createSession(true, Session.SESSION_TRANSACTED);
        consumer = session.createConsumer(queue);
        message = consumer.receive(500);

        assertNull("Message was received after store removal", message);
    }

}