summaryrefslogtreecommitdiff
path: root/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/upgrade/AbstractUpgradeTestCase.java
blob: cd2654f79f48c66f541d880e70423913d9dc69db (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
/*
 *
 * 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.berkeleydb.upgrade;

import java.io.File;

import org.apache.qpid.server.logging.LogSubject;
import org.apache.qpid.server.logging.subjects.TestBlankSubject;
import org.apache.qpid.test.utils.QpidTestCase;
import org.apache.qpid.util.FileUtils;

import com.sleepycat.je.Database;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Transaction;

public abstract class AbstractUpgradeTestCase extends QpidTestCase
{
    protected static final class StaticAnswerHandler implements UpgradeInteractionHandler
    {
        private UpgradeInteractionResponse _response;

        public StaticAnswerHandler(UpgradeInteractionResponse response)
        {
            _response = response;
        }

        @Override
        public UpgradeInteractionResponse requireResponse(String question, UpgradeInteractionResponse defaultResponse,
                UpgradeInteractionResponse... possibleResponses)
        {
            return _response;
        }
    }

    public static final String[] QUEUE_NAMES = { "clientid:myDurSubName", "clientid:mySelectorDurSubName", "myUpgradeQueue",
            "queue-non-durable", "nonexclusive-with-erroneous-owner" };
    public static int[] QUEUE_SIZES = { 1, 1, 10, 3, 0};
    public static int TOTAL_MESSAGE_NUMBER = 15;
    protected static final LogSubject LOG_SUBJECT = new TestBlankSubject();

    // one binding per exchange
    protected static final int TOTAL_BINDINGS = QUEUE_NAMES.length * 2;
    protected static final int TOTAL_EXCHANGES = 5;

    private File _storeLocation;
    protected Environment _environment;

    @Override
    public void setUp() throws Exception
    {
        super.setUp();
        _storeLocation = copyStore(getStoreDirectoryName());

        _environment = createEnvironment(_storeLocation);
    }

    /** @return eg "bdbstore-v4" - used for copying store */
    protected abstract String getStoreDirectoryName();

    protected Environment createEnvironment(File storeLocation)
    {
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        envConfig.setTransactional(true);
        envConfig.setConfigParam("je.lock.nLockTables", "7");
        envConfig.setReadOnly(false);
        envConfig.setSharedCache(false);
        envConfig.setCacheSize(0);
        return new Environment(storeLocation, envConfig);
    }

    @Override
    public void tearDown() throws Exception
    {
        try
        {
            _environment.close();
        }
        finally
        {
            _environment = null;
            deleteDirectoryIfExists(_storeLocation);
        }
        super.tearDown();
    }

    private File copyStore(String storeDirectoryName) throws Exception
    {
        String src = getClass().getClassLoader().getResource("upgrade/" + storeDirectoryName).toURI().getPath();
        File storeLocation = new File(new File(TMP_FOLDER), "test-store");
        deleteDirectoryIfExists(storeLocation);
        FileUtils.copyRecursive(new File(src), new File(TMP_FOLDER));
        return storeLocation;
    }

    protected void deleteDirectoryIfExists(File dir)
    {
        if (dir.exists())
        {
            assertTrue("The provided file " + dir + " is not a directory", dir.isDirectory());

            boolean deletedSuccessfully = FileUtils.delete(dir, true);

            assertTrue("Files at '" + dir + "' should have been deleted", deletedSuccessfully);
        }
    }

    protected void assertDatabaseRecordCount(String databaseName, final long expectedCountNumber)
    {
        long count = getDatabaseCount(databaseName);
        assertEquals("Unexpected database '" + databaseName + "' entry number", expectedCountNumber, count);
    }

    protected long getDatabaseCount(String databaseName)
    {
        DatabaseCallable<Long> operation = new DatabaseCallable<Long>()
        {

            @Override
            public Long call(Database sourceDatabase, Database targetDatabase, Transaction transaction)
            {
                return new Long(sourceDatabase.count());

            }
        };
        Long count = new DatabaseTemplate(_environment, databaseName, null).call(operation);
        return count.longValue();
    }

    public String getVirtualHostName()
    {
        return getName();
    }
}