|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.nio.file.Path;
|
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Properties; |
21 | 24 |
|
22 | 25 | import javax.jms.ConnectionFactory;
|
23 | 26 | import javax.jms.TemporaryQueue;
|
24 | 27 | import javax.jms.XAConnection;
|
25 | 28 | import javax.jms.XAConnectionFactory;
|
26 | 29 | import javax.jms.XASession;
|
| 30 | +import javax.naming.Context; |
| 31 | +import javax.naming.InitialContext; |
| 32 | +import javax.naming.NamingException; |
27 | 33 | import javax.sql.DataSource;
|
28 | 34 | import javax.sql.XADataSource;
|
29 | 35 | import javax.transaction.UserTransaction;
|
|
34 | 40 | import com.atomikos.jms.AtomikosConnectionFactoryBean;
|
35 | 41 | import org.junit.jupiter.api.AfterEach;
|
36 | 42 | import org.junit.jupiter.api.Test;
|
| 43 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 44 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 45 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 46 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 47 | +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
| 48 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 49 | +import org.junit.jupiter.api.extension.ParameterResolutionException; |
| 50 | +import org.junit.jupiter.api.extension.ParameterResolver; |
37 | 51 | import org.junit.jupiter.api.io.TempDir;
|
| 52 | +import org.junit.jupiter.params.ParameterizedTest; |
| 53 | +import org.junit.jupiter.params.provider.Arguments; |
| 54 | +import org.junit.jupiter.params.provider.MethodSource; |
| 55 | +import org.osjava.sj.loader.JndiLoader; |
38 | 56 |
|
39 | 57 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
40 | 58 | import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
|
|
49 | 67 | import org.springframework.context.annotation.Bean;
|
50 | 68 | import org.springframework.context.annotation.Configuration;
|
51 | 69 | import org.springframework.transaction.jta.JtaTransactionManager;
|
| 70 | +import org.springframework.transaction.jta.UserTransactionAdapter; |
52 | 71 |
|
53 | 72 | import static org.assertj.core.api.Assertions.assertThat;
|
54 | 73 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -76,6 +95,31 @@ void closeContext() {
|
76 | 95 | if (this.context != null) {
|
77 | 96 | this.context.close();
|
78 | 97 | }
|
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + @ParameterizedTest |
| 102 | + @ExtendWith(JndiExtension.class) |
| 103 | + @MethodSource("transactionManagerJndiEntries") |
| 104 | + void transactionManagerFromJndi(JndiEntry jndiEntry, InitialContext initialContext) throws NamingException { |
| 105 | + jndiEntry.register(initialContext); |
| 106 | + this.context = new AnnotationConfigApplicationContext(JtaAutoConfiguration.class); |
| 107 | + JtaTransactionManager transactionManager = this.context.getBean(JtaTransactionManager.class); |
| 108 | + if (jndiEntry.value instanceof UserTransaction) { |
| 109 | + assertThat(transactionManager.getUserTransaction()).isEqualTo(jndiEntry.value); |
| 110 | + assertThat(transactionManager.getTransactionManager()).isNull(); |
| 111 | + } |
| 112 | + else { |
| 113 | + assertThat(transactionManager.getUserTransaction()).isInstanceOf(UserTransactionAdapter.class); |
| 114 | + assertThat(transactionManager.getTransactionManager()).isEqualTo(jndiEntry.value); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + static List<Arguments> transactionManagerJndiEntries() { |
| 119 | + return Arrays.asList(Arguments.of(new JndiEntry("java:comp/UserTransaction", UserTransaction.class)), |
| 120 | + Arguments.of(new JndiEntry("java:appserver/TransactionManager", TransactionManager.class)), |
| 121 | + Arguments.of(new JndiEntry("java:pm/TransactionManager", TransactionManager.class)), |
| 122 | + Arguments.of(new JndiEntry("java:/TransactionManager", TransactionManager.class))); |
79 | 123 | }
|
80 | 124 |
|
81 | 125 | @Test
|
@@ -199,4 +243,79 @@ DataSource pooledDataSource(XADataSourceWrapper wrapper) throws Exception {
|
199 | 243 |
|
200 | 244 | }
|
201 | 245 |
|
| 246 | + private static final class JndiEntry { |
| 247 | + |
| 248 | + private final String name; |
| 249 | + |
| 250 | + private final Class<?> type; |
| 251 | + |
| 252 | + private final Object value; |
| 253 | + |
| 254 | + private JndiEntry(String name, Class<?> type) { |
| 255 | + this.name = name; |
| 256 | + this.type = type; |
| 257 | + this.value = mock(type); |
| 258 | + } |
| 259 | + |
| 260 | + private void register(InitialContext initialContext) throws NamingException { |
| 261 | + String[] components = this.name.split("/"); |
| 262 | + String subcontextName = components[0]; |
| 263 | + String entryName = components[1]; |
| 264 | + Context javaComp = initialContext.createSubcontext(subcontextName); |
| 265 | + JndiLoader loader = new JndiLoader(initialContext.getEnvironment()); |
| 266 | + Properties properties = new Properties(); |
| 267 | + properties.setProperty(entryName + "/type", this.type.getName()); |
| 268 | + properties.put(entryName + "/valueToConvert", this.value); |
| 269 | + loader.load(properties, javaComp); |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public String toString() { |
| 274 | + return this.name; |
| 275 | + } |
| 276 | + |
| 277 | + } |
| 278 | + |
| 279 | + private static final class JndiExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver { |
| 280 | + |
| 281 | + @Override |
| 282 | + public void beforeEach(ExtensionContext context) throws Exception { |
| 283 | + Namespace namespace = Namespace.create(getClass(), context.getUniqueId()); |
| 284 | + context.getStore(namespace).getOrComputeIfAbsent(InitialContext.class, (k) -> createInitialContext(), |
| 285 | + InitialContext.class); |
| 286 | + } |
| 287 | + |
| 288 | + private InitialContext createInitialContext() { |
| 289 | + try { |
| 290 | + return new InitialContext(); |
| 291 | + } |
| 292 | + catch (Exception ex) { |
| 293 | + throw new RuntimeException(); |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public void afterEach(ExtensionContext context) throws Exception { |
| 299 | + Namespace namespace = Namespace.create(getClass(), context.getUniqueId()); |
| 300 | + InitialContext initialContext = context.getStore(namespace).remove(InitialContext.class, |
| 301 | + InitialContext.class); |
| 302 | + initialContext.removeFromEnvironment("org.osjava.sj.jndi.ignoreClose"); |
| 303 | + initialContext.close(); |
| 304 | + } |
| 305 | + |
| 306 | + @Override |
| 307 | + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| 308 | + throws ParameterResolutionException { |
| 309 | + return InitialContext.class.isAssignableFrom(parameterContext.getParameter().getType()); |
| 310 | + } |
| 311 | + |
| 312 | + @Override |
| 313 | + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) |
| 314 | + throws ParameterResolutionException { |
| 315 | + Namespace namespace = Namespace.create(getClass(), extensionContext.getUniqueId()); |
| 316 | + return extensionContext.getStore(namespace).get(InitialContext.class); |
| 317 | + } |
| 318 | + |
| 319 | + } |
| 320 | + |
202 | 321 | }
|
0 commit comments