1 package ch.qos.logback.classic.net;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7
8 import java.util.Properties;
9
10 import javax.jms.ObjectMessage;
11 import javax.naming.Context;
12
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16
17 import ch.qos.logback.classic.Level;
18 import ch.qos.logback.classic.net.mock.MockTopic;
19 import ch.qos.logback.classic.net.mock.MockTopicConnectionFactory;
20 import ch.qos.logback.classic.net.mock.MockTopicPublisher;
21 import ch.qos.logback.classic.spi.LoggingEvent;
22 import ch.qos.logback.classic.util.MockInitialContext;
23 import ch.qos.logback.classic.util.MockInitialContextFactory;
24 import ch.qos.logback.core.ContextBase;
25
26 public class JMSTopicAppenderTest {
27
28 ch.qos.logback.core.Context context;
29 JMSTopicAppender appender;
30
31
32 @Before
33 public void setUp() throws Exception {
34 context = new ContextBase();
35 appender = new JMSTopicAppender();
36 appender.setContext(context);
37 appender.setName("jmsTopic");
38 appender.tcfBindingName = "topicCnxFactory";
39 appender.topicBindingName = "testTopic";
40 appender.setProviderURL("url");
41 appender.setInitialContextFactoryName(MockInitialContextFactory.class.getName());
42
43 MockInitialContext mic = MockInitialContextFactory.getContext();
44 mic.map.put(appender.tcfBindingName, new MockTopicConnectionFactory());
45 mic.map.put(appender.topicBindingName, new MockTopic(appender.topicBindingName));
46
47 }
48
49
50 @After
51 public void tearDown() throws Exception {
52 appender = null;
53 context = null;
54 }
55
56 @Test
57 public void testAppendOk() {
58 appender.start();
59
60 LoggingEvent le = createLoggingEvent();
61 appender.append(le);
62
63 MockTopicPublisher tp = (MockTopicPublisher)appender.topicPublisher;
64 assertEquals(1, tp.getMessageList().size());
65 ObjectMessage message = (ObjectMessage) tp.getMessageList().get(0);
66 try {
67 assertEquals(le, message.getObject());
68 } catch (Exception e) {
69 fail();
70 }
71 }
72
73 @Test
74 public void testAppendFailure() {
75 appender.start();
76
77
78 appender.topicPublisher = null;
79
80 LoggingEvent le = createLoggingEvent();
81 for (int i = 1; i <= 3; i++) {
82 appender.append(le);
83 assertEquals(i, context.getStatusManager().getCount());
84 assertTrue(appender.isStarted());
85 }
86 appender.append(le);
87 assertEquals(4, context.getStatusManager().getCount());
88 assertFalse(appender.isStarted());
89 }
90
91 @Test
92 public void testBuildEnvProperties() {
93 appender.setInitialContextFactoryName("icfn");
94 appender.setProviderURL("url");
95 appender.setURLPkgPrefixes("pkgPref");
96 appender.setSecurityPrincipalName("user");
97 appender.setSecurityCredentials("cred");
98
99 Properties props = appender.buildEnvProperties();
100 assertEquals(5, props.size());
101 assertEquals(appender.getInitialContextFactoryName(), props
102 .getProperty(Context.INITIAL_CONTEXT_FACTORY));
103 assertEquals(appender.getProviderURL(), props.getProperty(Context.PROVIDER_URL));
104 assertEquals(appender.getURLPkgPrefixes(), props
105 .getProperty(Context.URL_PKG_PREFIXES));
106 assertEquals(appender.getSecurityPrincipalName(), props
107 .getProperty(Context.SECURITY_PRINCIPAL));
108 assertEquals(appender.getSecurityCredentials(), props
109 .getProperty(Context.SECURITY_CREDENTIALS));
110 }
111
112 @Test
113 public void testBuildEnvPropertiesWithNullProviderURL() {
114 appender.setInitialContextFactoryName("icfn");
115 appender.setProviderURL(null);
116 appender.setURLPkgPrefixes("pkgPref");
117 appender.setSecurityPrincipalName("user");
118 appender.setSecurityCredentials("cred");
119
120 Properties props = appender.buildEnvProperties();
121 assertEquals(4, props.size());
122 assertEquals(appender.getInitialContextFactoryName(), props
123 .getProperty(Context.INITIAL_CONTEXT_FACTORY));
124 assertEquals(null, props.getProperty(Context.PROVIDER_URL));
125 assertEquals(appender.getURLPkgPrefixes(), props
126 .getProperty(Context.URL_PKG_PREFIXES));
127 assertEquals(appender.getSecurityPrincipalName(), props
128 .getProperty(Context.SECURITY_PRINCIPAL));
129 assertEquals(appender.getSecurityCredentials(), props
130 .getProperty(Context.SECURITY_CREDENTIALS));
131
132 assertEquals(1, context.getStatusManager().getCount());
133 }
134
135 @Test
136 public void testBuildEnvPropertiesWithNullCredentials() {
137 appender.setInitialContextFactoryName("icfn");
138 appender.setProviderURL("url");
139 appender.setURLPkgPrefixes("pkgPref");
140 appender.setSecurityPrincipalName("user");
141 appender.setSecurityCredentials(null);
142
143 Properties props = appender.buildEnvProperties();
144 assertEquals(4, props.size());
145 assertEquals(appender.getInitialContextFactoryName(), props
146 .getProperty(Context.INITIAL_CONTEXT_FACTORY));
147 assertEquals(appender.getProviderURL(), props.getProperty(Context.PROVIDER_URL));
148 assertEquals(appender.getURLPkgPrefixes(), props
149 .getProperty(Context.URL_PKG_PREFIXES));
150 assertEquals(appender.getSecurityPrincipalName(), props
151 .getProperty(Context.SECURITY_PRINCIPAL));
152 assertEquals(null, props
153 .getProperty(Context.SECURITY_CREDENTIALS));
154
155 assertEquals(1, context.getStatusManager().getCount());
156 }
157
158 @Test
159 public void testBuildEnvPropertiesWithPkgNull() {
160 appender.setInitialContextFactoryName("icfn");
161 appender.setProviderURL("url");
162 appender.setURLPkgPrefixes(null);
163 appender.setSecurityPrincipalName("user");
164 appender.setSecurityCredentials("cred");
165
166 Properties props = appender.buildEnvProperties();
167 assertEquals(4, props.size());
168 assertEquals(appender.getInitialContextFactoryName(), props
169 .getProperty(Context.INITIAL_CONTEXT_FACTORY));
170 assertEquals(appender.getProviderURL(), props.getProperty(Context.PROVIDER_URL));
171 assertEquals(null, props
172 .getProperty(Context.URL_PKG_PREFIXES));
173 assertEquals(appender.getSecurityPrincipalName(), props
174 .getProperty(Context.SECURITY_PRINCIPAL));
175 assertEquals(appender.getSecurityCredentials(), props
176 .getProperty(Context.SECURITY_CREDENTIALS));
177
178 assertEquals(0, context.getStatusManager().getCount());
179 }
180
181 @Test
182 public void testStartMinimalInfo() {
183
184
185 appender.setProviderURL(null);
186 appender.start();
187
188 assertTrue(appender.isStarted());
189
190 try {
191 assertEquals(appender.topicBindingName, appender.topicPublisher.getTopic().getTopicName());
192 } catch (Exception e) {
193 fail();
194 }
195 }
196
197 @Test
198 public void testStartUserPass() {
199 appender.setUserName("test");
200 appender.setPassword("test");
201
202 appender.start();
203
204 assertTrue(appender.isStarted());
205
206 try {
207 assertEquals(appender.topicBindingName, appender.topicPublisher.getTopic().getTopicName());
208 } catch (Exception e) {
209 fail();
210 }
211 }
212
213 @Test
214 public void testStartFails() {
215 appender.topicBindingName = null;
216
217 appender.start();
218
219 assertFalse(appender.isStarted());
220 }
221
222 private LoggingEvent createLoggingEvent() {
223 LoggingEvent le = new LoggingEvent();
224 le.setLevel(Level.DEBUG);
225 le.setMessage("test message");
226 le.setTimeStamp(System.currentTimeMillis());
227 le.setThreadName(Thread.currentThread().getName());
228 return le;
229 }
230 }