forked from javasoze/zoie
/
TestJMSStreamDataProvider.java
146 lines (113 loc) · 3.88 KB
/
TestJMSStreamDataProvider.java
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
package proj.zoie.dataprovider.jms;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import proj.zoie.api.DataConsumer.DataEvent;
@RunWith(MockitoJUnitRunner.class)
public class TestJMSStreamDataProvider {
@Mock
TopicConnectionFactory connectionFactory;
@Mock
TopicFactory topicFactory;
@Mock
DataEventBuilder<Object> dataEventBuilder;
@Mock
TopicSubscriber subscriber;
@Mock
TopicSession session;
@Mock
TopicConnection connection;
@Mock
Message message;
JMSStreamDataProvider<Object> provider;
@Before
public void setUpJMSStreamDataProvider() throws JMSException {
String confdir = System.getProperty("conf.dir");
org.apache.log4j.PropertyConfigurator.configure(confdir+"/log4j.properties");
when(dataEventBuilder.buildDataEvent(any(Message.class)))
.thenReturn(new DataEvent<Object>(0, new Object()));
provider =
new JMSStreamDataProvider<Object>("topic", "clientID", connectionFactory,
topicFactory, dataEventBuilder);
}
/**
* The case, when JMS returns messages without problems
* @throws JMSException
*/
@Test
public void testSuccessfulNext() throws JMSException {
//stub successful connection
when(connectionFactory.createTopicConnection())
.thenReturn(connection);
when(connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE))
.thenReturn(session);
when(session.createDurableSubscriber(any(Topic.class), anyString()))
.thenReturn(subscriber);
//stub successful receiving messages
when(subscriber.receive())
.thenReturn(message, message, message);
provider.start();
for (int i = 0; i < 3; i++) {
//make sure events are generated
assertNotNull(provider.next());
}
//make sure event builder has been called 3 times
verify(dataEventBuilder, times(3)).buildDataEvent(any(Message.class));
provider.stop();
}
/**
* The case, when JMS throws exception
* @throws JMSException
*/
@Test
public void testExceptionRecovery() throws JMSException {
//stub some problems with connecting to topic, but
//at the end connect anyway
when(connectionFactory.createTopicConnection())
.thenReturn(connection)
.thenThrow(new JMSException("some problem 4"))
.thenReturn(connection); //the last stubbed value will be repeatedly returned
when(connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE))
.thenReturn(session)
.thenThrow(new JMSException("some problem 5"))
.thenReturn(session);
when(session.createDurableSubscriber(any(Topic.class), anyString()))
.thenReturn(subscriber)
.thenThrow(new JMSException("some problem 6"))
.thenReturn(subscriber); //the last stubbed value will be repeatedly returned
//stub some problems with receiving messages,
//in total receive 3 messages
when(subscriber.receive())
.thenThrow(new JMSException("some problem 0"))
.thenReturn(message)
.thenThrow(new JMSException("some problem 1"))
.thenReturn(message)
.thenThrow(new JMSException("some problem 2"))
.thenThrow(new JMSException("some problem 3"))
.thenReturn(message); //the last stubbed value will be repeatedly returned
//for testing set back off time very small
provider.setJMSErrorBackOffTime(1);
provider.start();
for (int i = 0; i < 3; i++) {
//make sure events are generated
assertNotNull(provider.next());
}
provider.stop();
}
}