summaryrefslogtreecommitdiff
path: root/java/broker/src/test/java/org/apache/qpid/server/store/OperationalLoggingListenerTest.java
blob: 42746f9119842ac3b1165b589b380383430f8ebc (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
package org.apache.qpid.server.store;

import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.apache.qpid.server.logging.LogActor;
import org.apache.qpid.server.logging.LogMessage;
import org.apache.qpid.server.logging.LogSubject;
import org.apache.qpid.server.logging.RootMessageLogger;
import org.apache.qpid.server.logging.actors.CurrentActor;
import org.apache.qpid.server.logging.messages.ConfigStoreMessages;
import org.apache.qpid.server.logging.messages.MessageStoreMessages;
import org.apache.qpid.server.logging.messages.TransactionLogMessages;

/**
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.
 */
public class OperationalLoggingListenerTest extends TestCase
{


    public static final String STORE_LOCATION = "The moon!";

    protected void setUp() throws Exception
    {
        super.setUp();
        
    }

    public void testOperationalLoggingWithStoreLocation() throws Exception
    {
        TestMessageStore messageStore = new TestMessageStore();
        LogSubject logSubject = LOG_SUBJECT;

        OperationalLoggingListener.listen(messageStore, logSubject);

        performTests(messageStore, true);

    }

    public void testOperationalLogging() throws Exception
    {
        TestMessageStore messageStore = new TestMessageStore();
        LogSubject logSubject = LOG_SUBJECT;

        OperationalLoggingListener.listen(messageStore, logSubject);

        performTests(messageStore, false);
    }

    private void performTests(TestMessageStore messageStore, boolean setStoreLocation)
    {
        final List<LogMessage> messages = new ArrayList<LogMessage>();

        CurrentActor.set(new TestActor(messages));

        if(setStoreLocation)
        {
            messageStore.setStoreLocation(STORE_LOCATION);
        }


        messageStore.attainState(State.CONFIGURING);
        assertEquals("Unexpected number of operational log messages on configuring", 1, messages.size());
        assertEquals(messages.remove(0).toString(), ConfigStoreMessages.CREATED().toString());

        messageStore.attainState(State.CONFIGURED);
        assertEquals("Unexpected number of operational log messages on CONFIGURED", setStoreLocation ? 3 : 2, messages.size());
        assertEquals(messages.remove(0).toString(), MessageStoreMessages.CREATED().toString());
        assertEquals(messages.remove(0).toString(), TransactionLogMessages.CREATED().toString());
        if(setStoreLocation)
        {
            assertEquals(messages.remove(0).toString(), MessageStoreMessages.STORE_LOCATION(STORE_LOCATION).toString());
        }

        messageStore.attainState(State.RECOVERING);
        assertEquals("Unexpected number of operational log messages on RECOVERING", 1, messages.size());
        assertEquals(messages.remove(0).toString(), MessageStoreMessages.RECOVERY_START().toString());


        messageStore.attainState(State.ACTIVE);
        assertEquals("Unexpected number of operational log messages on ACTIVE", 1, messages.size());
        assertEquals(messages.remove(0).toString(), MessageStoreMessages.RECOVERY_COMPLETE().toString());

        messageStore.attainState(State.CLOSING);
        assertEquals("Unexpected number of operational log messages on CLOSING", 0, messages.size());

        messageStore.attainState(State.CLOSED);
        assertEquals("Unexpected number of operational log messages on CLOSED", 1, messages.size());
        assertEquals(messages.remove(0).toString(), MessageStoreMessages.CLOSED().toString());
    }

    @Override
    protected void tearDown() throws Exception
    {
        super.tearDown();
        CurrentActor.remove();
    }


    private static final LogSubject LOG_SUBJECT = new LogSubject()
    {
        public String toLogString()
        {
            return "";
        }
    };

    private static final class TestMessageStore extends NullMessageStore
    {

        private final EventManager _eventManager = new EventManager();
        private final StateManager _stateManager = new StateManager(_eventManager);
        private String _storeLocation;

        public void attainState(State state)
        {
            _stateManager.attainState(state);
        }

        @Override
        public String getStoreLocation()
        {
            return _storeLocation;
        }

        public void setStoreLocation(String storeLocation)
        {
            _storeLocation = storeLocation;
        }

        @Override
        public void addEventListener(EventListener eventListener, Event... events)
        {
            _eventManager.addEventListener(eventListener, events);
        }
    }

    private static class TestActor implements LogActor
    {
        private final List<LogMessage> _messages;

        public TestActor(List<LogMessage> messages)
        {
            _messages = messages;
        }

        public void message(LogSubject subject, LogMessage message)
        {
            _messages.add(message);
        }

        public void message(LogMessage message)
        {
            _messages.add(message);
        }

        public RootMessageLogger getRootMessageLogger()
        {
            return null;
        }

        public String getLogMessage()
        {
            return null;
        }
    }
}