Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WFLY-5232] - Naming store is null before CDI lifecycle BeforeShutdow…
…n event fires. JIRA: https://issues.jboss.org/browse/WFLY-5232 BZ-1252583: https://bugzilla.redhat.com/show_bug.cgi?id=1252583
- Loading branch information
1 parent
a2147d2
commit efdb123
Showing
5 changed files
with
194 additions
and
19 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...de/src/test/java/org/jboss/as/test/manualmode/weld/extension/BeforeShutdownExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.jboss.as.test.manualmode.weld.extension; | ||
|
||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.spi.BeforeShutdown; | ||
import javax.enterprise.inject.spi.Extension; | ||
import javax.naming.InitialContext; | ||
import javax.naming.NamingException; | ||
import javax.transaction.TransactionSynchronizationRegistry; | ||
import javax.transaction.UserTransaction; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Ryan Emerson | ||
*/ | ||
public class BeforeShutdownExtension implements Extension { | ||
|
||
private UserTransaction userTx = null; | ||
private TransactionSynchronizationRegistry txSynchRegistry = null; | ||
|
||
void lookupBeforeShutdown(@Observes final BeforeShutdown beforeShutdown) throws Exception { | ||
try { | ||
userTx = lookup("java:jboss/UserTransaction"); | ||
userTx.getStatus(); | ||
|
||
txSynchRegistry = lookup("java:jboss/TransactionSynchronizationRegistry"); | ||
txSynchRegistry.getTransactionStatus(); | ||
} catch (Exception e) { | ||
writeOutput(e); | ||
throw e; | ||
} | ||
writeOutput(null); | ||
} | ||
|
||
private <T> T lookup(String jndiName) { | ||
try { | ||
InitialContext initialContext = new InitialContext(); | ||
return (T) initialContext.lookup(jndiName); | ||
} catch (NamingException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
// Necessary so that BeforeShutdownJNDILookupTestCase can see the outcome of the BeforeShutdown JNDI lookups. | ||
private void writeOutput(Exception exception) throws Exception { | ||
List<String> output = new ArrayList<>(); | ||
if (exception != null) { | ||
output.add("Exception"); | ||
output.add(exception + "," + Arrays.toString(exception.getStackTrace())); | ||
} else { | ||
output.add("UserTransaction"); | ||
output.add(userTx.toString()); | ||
output.add("TransactionSynchronizationRegistry"); | ||
output.add(txSynchRegistry.toString()); | ||
} | ||
File parent = new File(BeforeShutdownJNDILookupTestCase.TEST_URL).getParentFile(); | ||
if (!parent.exists()) | ||
parent.mkdirs(); | ||
Files.write(Paths.get("", BeforeShutdownJNDILookupTestCase.TEST_URL), output, StandardOpenOption.CREATE, StandardOpenOption.WRITE); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...st/java/org/jboss/as/test/manualmode/weld/extension/BeforeShutdownJNDILookupTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.jboss.as.test.manualmode.weld.extension; | ||
|
||
import junit.framework.AssertionFailedError; | ||
import org.jboss.arquillian.container.test.api.ContainerController; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.arquillian.container.test.api.TargetsContainer; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ArchivePaths; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.AfterClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import javax.enterprise.inject.spi.Extension; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* A test to ensure that the UserTransaction and TransactionSynchronizationRegistry can be retrieved via JNDI when | ||
* an extensions BeforeShutdown method is invoked. | ||
* <p/> | ||
* See WFLY-5232 | ||
* | ||
* @author Ryan Emerson | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class BeforeShutdownJNDILookupTestCase { | ||
|
||
public static final String TEST_URL = "target" + File.separator + "results.txt"; | ||
|
||
private static final String CONTAINER = "default-jbossas"; | ||
private static final String DEPLOYMENT = "test.war"; | ||
private static final Path TEST_PATH = Paths.get("", TEST_URL); | ||
|
||
@Deployment(name = DEPLOYMENT, managed = true) | ||
@TargetsContainer(CONTAINER) | ||
public static Archive<?> deploy() throws Exception { | ||
return ShrinkWrap | ||
.create(WebArchive.class, DEPLOYMENT) | ||
.addClasses(BeforeShutdownJNDILookupTestCase.class, BeforeShutdownExtension.class) | ||
.add(EmptyAsset.INSTANCE, ArchivePaths.create("WEB-INF/beans.xml")) | ||
.add(new StringAsset(BeforeShutdownExtension.class.getName()), "META-INF/services/" + Extension.class.getName()); | ||
} | ||
|
||
@ArquillianResource | ||
ContainerController controller; | ||
|
||
@Test | ||
public void testTransactionJNDILookupDuringShutdownEvent() throws Exception { | ||
controller.start(CONTAINER); | ||
controller.kill(CONTAINER); | ||
|
||
List<String> output = Files.readAllLines(TEST_PATH); | ||
if (output.get(0).equals("Exception")) { | ||
String stacktrace = output.get(1).replaceAll(",", System.getProperty("line.separator")); | ||
String msg = "An exception was thrown by the deployment %s during shutdown. The server stacktrace is shown below: %n%s"; | ||
throw new AssertionFailedError(String.format(msg, DEPLOYMENT, stacktrace)); | ||
} | ||
assertEquals("Contents of result.txt is not valid!", "UserTransaction", output.get(0)); | ||
} | ||
|
||
@AfterClass | ||
public static void cleanup() throws Exception { | ||
Files.delete(TEST_PATH); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters