Issue #23507 gf-client-module now uses junit5 instead of junit4 - DefaultGUICallbackHandlerTest can be executed in Eclipse, depends on AWT UI - several tests enabled - AppClientContainerTest deleted, depended on unknown classes and was commented out
diff --git a/appserver/appclient/client/acc/pom.xml b/appserver/appclient/client/acc/pom.xml index e2a1cc3..2056692 100755 --- a/appserver/appclient/client/acc/pom.xml +++ b/appserver/appclient/client/acc/pom.xml
@@ -329,6 +329,10 @@ </dependency> <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + </dependency> + <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest</artifactId> </dependency>
diff --git a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/CLIBootstrapTest.java b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/CLIBootstrapTest.java index 424b090..b953097 100644 --- a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/CLIBootstrapTest.java +++ b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/CLIBootstrapTest.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -16,14 +17,14 @@ package org.glassfish.appclient.client; -import org.glassfish.appclient.client.acc.UserError; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * @@ -31,90 +32,53 @@ */ public class CLIBootstrapTest { - public CLIBootstrapTest() { - } - - @BeforeClass - public static void setUpClass() throws Exception { - - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before + @BeforeEach public void setUp() { System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + "AS_JAVA", ""); System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + "JAVA_HOME", ""); - System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + "PATH", - System.getenv("PATH")); + System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + "PATH", System.getenv("PATH")); System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + "_AS_INSTALL", - "/Users/Tim/asgroup/v3/H/publish/glassfish6/glassfish"); + "/Users/Tim/asgroup/v3/H/publish/glassfish6/glassfish"); } - @After - public void tearDown() { - } - - @Ignore @Test public void testChooseJavaASJAVAAsCurrent() { runTest("AS_JAVA"); } - @Ignore @Test public void testChooseJavaJAVAHOMEAsCurrent() { runTest("JAVA_HOME"); } - @Ignore @Test public void testChooseJavaASJAVAAsBad() { runTestUsingBadLocation("AS_JAVA"); } - @Ignore @Test public void testChooseJAVAHOMEAsBad() { runTestUsingBadLocation("JAVA_HOME"); } private void runTestUsingBadLocation(final String envVarName) { - try { - final CLIBootstrap boot = new CLIBootstrap(); - System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + envVarName, - "shouldnotexistanywhere"); - CLIBootstrap.JavaInfo javaInfo = boot.initJava(); - - } catch (UserError ex) { - /* - * We expect this exception because we tried to use a non-sensical - * setting for the java location. - */ - } + final CLIBootstrap boot = assertDoesNotThrow(() -> new CLIBootstrap()); + System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + envVarName, "shouldnotexistanywhere"); + CLIBootstrap.JavaInfo javaInfo = boot.initJava(); + assertNotNull(javaInfo); } private void runTest(final String envVarName) { - System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + envVarName, - System.getProperty("java.home")); - try { - final CLIBootstrap boot = new CLIBootstrap(); - CLIBootstrap.JavaInfo javaInfo = boot.initJava(); - if (javaInfo == null) { - fail("chooseJava found no match; expected to match on " + envVarName); - } - if ( ! javaInfo.toString().equals(envVarName)) { - fail("Expected to choose " + envVarName + " but chose " + javaInfo.toString() + " instead"); - } - if ( ! javaInfo.isValid()) { - fail("Correctly chose " + envVarName + " but it should have been valid, derived as it was from PATH, but was not"); - } - } catch (UserError ex) { - fail(ex.getMessage()); - } + String javaHome = System.getProperty("java.home"); + System.setProperty(CLIBootstrap.ENV_VAR_PROP_PREFIX + envVarName, javaHome); + final CLIBootstrap boot = assertDoesNotThrow(() -> new CLIBootstrap()); + CLIBootstrap.JavaInfo javaInfo = boot.initJava(); + assertAll( + () -> assertNotNull(javaInfo, "found no match; expected to match on " + envVarName), + () -> assertEquals(javaHome, javaInfo.javaBinDir().getAbsoluteFile().getParent()), + () -> assertFalse(javaInfo.isValid(), "it should have been valid, derived as it was from PATH, but was not") + ); } }
diff --git a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/AppClientContainerTest.java b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/AppClientContainerTest.java deleted file mode 100644 index a0e6fc8..0000000 --- a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/AppClientContainerTest.java +++ /dev/null
@@ -1,99 +0,0 @@ -/* - * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0, which is available at - * http://www.eclipse.org/legal/epl-2.0. - * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the - * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, - * version 2 with the GNU Classpath Exception, which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 - */ - -package org.glassfish.appclient.client.acc; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author tjquinn - */ -public class AppClientContainerTest { - - private static final String[] SERVER_SPECS = new String[] {"a","a:3701","b:3700"}; -// private static final ORBEndpoint[] SERVER_ANSWERS = new ORBEndpoint[] { -// new ORBEndpoint("a", 3700), -// new ORBEndpoint("a", 3701), -// new ORBEndpoint("b", 3700) -// }; - - public AppClientContainerTest() { - } - - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of newContainer method, of class AppClientContainer. - */ - @Ignore - @Test - public void testNewContainer() { - System.out.println("newContainer"); -// TargetServer[] targetServers = new TargetServer[] { -// new TargetServer("localhost", 3700) -// }; -// AppClientContainer expResult = null; -// AppClientContainer result = null; -// result = AppClientContainer.newContainer(targetServers); -// assertEquals(expResult, result); - } - -// @Test -// public void testServerArray() { -// try { -// ACCOption.SERVER serverOption = ACCOption.SERVER(SERVER_SPECS); -// assertArrayEquals(serverOption.get(), SERVER_ANSWERS); -// } catch (ValidationException e) { -// fail("Unexpected exception: " + e.toString()); -// } -// } - -// @Test -// public void testServerObjectArray() { -// Object[] servers = new Object[] {"c", "c:3701", "c:3700"}; -// ORBEndpoint[] result = null; -// try { -// ACCOption.SERVER.set(servers); -// fail("Passing array of objects to set SERVER should have failed but it worked"); -// } catch (ACCOption.TypeException ex) { -// System.out.println("Received expected " + ex); -// } catch (ValidationException ex) { -// fail(ex.toString()); -// } -// } - -}
diff --git a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/AppclientCommandArgumentsTest.java b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/AppclientCommandArgumentsTest.java index dcbe6c4..fec1252 100644 --- a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/AppclientCommandArgumentsTest.java +++ b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/AppclientCommandArgumentsTest.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -16,23 +17,27 @@ package org.glassfish.appclient.client.acc; -import java.io.IOException; -import java.util.Properties; -import java.io.File; import com.sun.enterprise.transaction.JavaEETransactionManagerSimplified; import com.sun.logging.LogDomains; + +import java.io.File; import java.io.FileWriter; -import java.util.logging.Logger; +import java.io.IOException; import java.util.Arrays; -import java.util.MissingResourceException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import static org.junit.Assert.*; +import java.util.Properties; +import java.util.logging.Logger; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** - * * @author tjquinn */ public class AppclientCommandArgumentsTest { @@ -42,136 +47,79 @@ private static final String EXPECTED_TARGETSERVER_VALUE = "A:1234,B:5678"; private static final String EXPECTED_PASSWORD_IN_PASSWORD_FILE = "mySecretPassword"; - public AppclientCommandArgumentsTest() { - } - - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - @Test public void testA() throws Exception, UserError { - AppclientCommandArguments info = AppclientCommandArguments.newInstance( - Arrays.asList("-textauth", "-user", USER_VALUE)); + AppclientCommandArguments info = AppclientCommandArguments + .newInstance(Arrays.asList("-textauth", "-user", USER_VALUE)); - assertEquals("text auth not set", true, info.isTextauth()); - assertEquals("noappinvoke incorrectly set", false, info.isNoappinvoke()); - assertEquals("user incorrectly set", USER_VALUE, info.getUser()); - /* - * Make sure an unspecified argument is repoted as null. - */ - assertEquals("mainclass wrong", null, info.getMainclass()); - + assertTrue(info.isTextauth(), "text auth not set"); + assertFalse(info.isNoappinvoke(), "noappinvoke incorrectly set"); + assertEquals(USER_VALUE, info.getUser(), "user incorrectly set"); + assertNull(info.getMainclass(), "mainclass wrong"); } @Test public void testB() throws Exception, UserError { - AppclientCommandArguments info = AppclientCommandArguments.newInstance( - Arrays.asList("-mainclass", PASSWORDFILE_NAME, "-noappinvoke")); + AppclientCommandArguments info = AppclientCommandArguments + .newInstance(Arrays.asList("-mainclass", PASSWORDFILE_NAME, "-noappinvoke")); - - assertEquals("wrong main class", PASSWORDFILE_NAME, info.getMainclass()); - assertEquals("noappinvoke not set", true, info.isNoappinvoke()); - assertEquals("user should have been null", null, info.getUser()); - assertEquals("textauth found but should be absent", false, info.isTextauth()); + assertEquals(PASSWORDFILE_NAME, info.getMainclass(), "wrong main class"); + assertEquals(true, info.isNoappinvoke(), "noappinvoke not set"); + assertEquals(null, info.getUser(), "user should have been null"); + assertEquals(false, info.isTextauth(), "textauth found but should be absent"); } - @Ignore @Test - public void invalidArgumentTest() throws Exception, UserError { - try { - AppclientCommandArguments.newInstance( - Arrays.asList("-badarg")); - fail("did not throw expected IllegalArgumentException due to an invalid arg"); - } catch (IllegalArgumentException e) { - // no-op so test passes - } catch (Exception e) { - fail("expected IllegalArgumentException but got " + e.getClass().getName()); - } + @Disabled("Doesn't throw anything.") + public void invalidArgumentTest() throws Exception { + assertThrows(IllegalArgumentException.class, + () -> AppclientCommandArguments.newInstance(Arrays.asList("-badarg"))); } @Test public void disallowMainclassAndName() throws Exception { - try { - AppclientCommandArguments.newInstance( - Arrays.asList("-mainclass","x.y.Main","-name","some-display-name")); - fail("did not detect incorrect spec of mainclass and name"); - } catch (UserError e) { - // suppress exception for a successful test - } + assertThrows(UserError.class, () -> AppclientCommandArguments + .newInstance(Arrays.asList("-mainclass", "x.y.Main", "-name", "some-display-name"))); } @Test public void allowMultiValuedTargetServer() throws Exception, UserError { final AppclientCommandArguments cmdArgs = AppclientCommandArguments.newInstance( Arrays.asList("-targetserver","\"" + EXPECTED_TARGETSERVER_VALUE + "\"")); - assertEquals("did not process targetserver list correctly", - EXPECTED_TARGETSERVER_VALUE, - cmdArgs.getTargetServer()); + assertEquals(EXPECTED_TARGETSERVER_VALUE, cmdArgs.getTargetServer(), + "did not process targetserver list correctly"); } @Test public void useTransactionLogString() { - final Logger logger = LogDomains.getLogger(JavaEETransactionManagerSimplified.class, - LogDomains.JTA_LOGGER); + final Logger logger = LogDomains.getLogger(JavaEETransactionManagerSimplified.class, LogDomains.JTA_LOGGER); final String target = "enterprise_used_delegate_name"; - try { - final String result = logger.getResourceBundle().getString(target); - assertTrue("message key look-up failed", (result != null && - ! target.equals(result))); - } catch (MissingResourceException ex) { - fail("could not find message key"); - } + final String result = logger.getResourceBundle().getString(target); + assertEquals("DTX5019: Transaction Manager is ready. Using [{0}] as the delegate", result, + "message key look-up failed"); } @Test - public void checkPasswordInFile() { + public void checkPasswordInFile() throws Exception, UserError { final Properties props = new Properties(); - props.setProperty(AppclientCommandArguments.PASSWORD_FILE_PASSWORD_KEYWORD, - EXPECTED_PASSWORD_IN_PASSWORD_FILE); - try { - final AppclientCommandArguments cmdArgs = prepareWithPWFile(props); - final char[] pwInObject = cmdArgs.getPassword(); - assertTrue("Password " + EXPECTED_PASSWORD_IN_PASSWORD_FILE + - " in password file does not match password " + new String(pwInObject) + - " returned from AppclientCommandArguments object", - Arrays.equals(pwInObject, EXPECTED_PASSWORD_IN_PASSWORD_FILE.toCharArray())); - - } catch (Throwable t) { - throw new RuntimeException(t); - } + props.setProperty(AppclientCommandArguments.PASSWORD_FILE_PASSWORD_KEYWORD, EXPECTED_PASSWORD_IN_PASSWORD_FILE); + final AppclientCommandArguments cmdArgs = prepareWithPWFile(props); + assertArrayEquals(EXPECTED_PASSWORD_IN_PASSWORD_FILE.toCharArray(), cmdArgs.getPassword(), + "Password in password file does not match expected password"); } - - @Test public void checkErrorHandlingIfRequiredPasswordInPasswordFileIsMissing() { final Properties props = new Properties(); props.setProperty("UNEXPECTED", EXPECTED_PASSWORD_IN_PASSWORD_FILE); - try { - final AppclientCommandArguments cmdArgs = prepareWithPWFile(props); - fail("Missing password in password file NOT correctly detected and flagged"); - } catch (UserError ue) { - /* - * This is what we expect - a UserError complaining about the - * missing password value. - */ - return; - } catch (IOException ex) { - throw new RuntimeException(ex); - } + assertThrows(UserError.class, () -> prepareWithPWFile(props), + "Missing password in password file NOT correctly detected and flagged"); } - private AppclientCommandArguments prepareWithPWFile( - final Properties props) throws UserError, IOException { + private AppclientCommandArguments prepareWithPWFile(final Properties props) throws UserError, IOException { final File passwordFile = createTempPWFile(props); - - return AppclientCommandArguments.newInstance( - Arrays.asList("-passwordfile","\"" + passwordFile.getAbsolutePath() + "\"")); + return AppclientCommandArguments + .newInstance(Arrays.asList("-passwordfile", "\"" + passwordFile.getAbsolutePath() + "\"")); } private File createTempPWFile(final Properties props) throws IOException { @@ -180,6 +128,4 @@ tempFile.deleteOnExit(); return tempFile; } - - }
diff --git a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/CommandLaunchInfoTest.java b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/CommandLaunchInfoTest.java index 7837927..1bd9fb4 100644 --- a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/CommandLaunchInfoTest.java +++ b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/CommandLaunchInfoTest.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -19,18 +20,16 @@ import java.io.File; import java.net.URL; -import org.junit.Ignore; -import static org.junit.Assert.*; - import java.util.Arrays; import java.util.List; + import org.glassfish.appclient.client.acc.CommandLaunchInfo.ClientLaunchType; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** - * * @author tjquinn */ public class CommandLaunchInfoTest { @@ -45,35 +44,23 @@ private static final List<String> expectedCommandArgs = Arrays.asList(FIRST_ACC_ARG, SECOND_ACC_ARG); - public CommandLaunchInfoTest() { - } - - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - @Test public void testA() throws Exception, UserError { - final AgentArguments agentArgs = AgentArguments.newInstance( "mode=acscript" + ",client=jar=" + JAR_CLIENT_NAME + ",arg=-textauth" + ",arg=-user,arg=" + USER_VALUE); CommandLaunchInfo info = CommandLaunchInfo.newInstance(agentArgs); - assertEquals("wrong client type", ClientLaunchType.JAR, info.getClientLaunchType()); - assertEquals("wrong client name", JAR_CLIENT_NAME, info.getClientName()); + assertEquals(ClientLaunchType.JAR, info.getClientLaunchType(), "wrong client type"); + assertEquals(JAR_CLIENT_NAME, info.getClientName(), "wrong client name"); } @Test public void testB() throws Exception, UserError { URL testFileURL = getClass().getResource(PASSWORDFILE_PATH); - assertNotNull("test file URL came back null", testFileURL); + assertNotNull(testFileURL, "test file URL came back null"); File testFile = new File(testFileURL.toURI()); final AgentArguments agentArgs = AgentArguments.newInstance( "mode=acscript" + @@ -82,8 +69,8 @@ ",arg=-noappinvoke"); CommandLaunchInfo info = CommandLaunchInfo.newInstance(agentArgs); - assertEquals("wrong client type", ClientLaunchType.DIR, info.getClientLaunchType()); - assertEquals("wrong client name", DIR_CLIENT_NAME, info.getClientName()); + assertEquals(ClientLaunchType.DIR, info.getClientLaunchType(), "wrong client type"); + assertEquals(DIR_CLIENT_NAME, info.getClientName(), "wrong client name"); } }
diff --git a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/callbackhandler/DefaultGUICallbackHandlerTest.java b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/callbackhandler/DefaultGUICallbackHandlerTest.java index f11d9c8..612e660 100644 --- a/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/callbackhandler/DefaultGUICallbackHandlerTest.java +++ b/appserver/appclient/client/acc/src/test/java/org/glassfish/appclient/client/acc/callbackhandler/DefaultGUICallbackHandlerTest.java
@@ -1,5 +1,6 @@ /* * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -16,7 +17,10 @@ package org.glassfish.appclient.client.acc.callbackhandler; -import java.io.IOException; +import com.sun.enterprise.module.single.StaticModulesRegistry; +import com.sun.enterprise.security.ssl.SSLUtils; +import com.sun.enterprise.security.ssl.impl.SecuritySupportImpl; + import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.ChoiceCallback; @@ -26,48 +30,64 @@ import javax.security.auth.callback.PasswordCallback; import javax.security.auth.callback.TextInputCallback; import javax.security.auth.callback.TextOutputCallback; -import javax.security.auth.callback.UnsupportedCallbackException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import static org.junit.Assert.*; + +import org.glassfish.api.admin.ProcessEnvironment; +import org.glassfish.hk2.api.DynamicConfiguration; +import org.glassfish.hk2.api.DynamicConfigurationService; +import org.glassfish.hk2.api.ServiceLocator; +import org.glassfish.internal.api.Globals; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledIfSystemProperty; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.lessThan; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** + * This test is here just to give example code for driving the callback mechanism. * * @author tjquinn */ +@DisabledIfSystemProperty( + named = "java.awt.headless", + matches = "true", + disabledReason = "The test cannot work in headless mode and without human interaction, but it is still worth as an example.") public class DefaultGUICallbackHandlerTest { - public DefaultGUICallbackHandlerTest() { + private static final String[] CHOICES = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh"}; + private StaticModulesRegistry registry; + + @BeforeEach + public void init() { + registry = new StaticModulesRegistry(getClass().getClassLoader()); + ServiceLocator locator = registry.createServiceLocator(getClass().getSimpleName()); + Globals.setDefaultHabitat(locator); + DynamicConfiguration config = locator.getService(DynamicConfigurationService.class).createDynamicConfiguration(); + config.addActiveDescriptor(ProcessEnvironment.class); + config.addActiveDescriptor(SecuritySupportImpl.class); + config.addActiveDescriptor(SSLUtils.class); + config.commit(); } - @BeforeClass - public static void setUpClass() throws Exception { + @AfterEach + public void shutdown() { + if (registry != null) { + registry.shutdown(); + } } - @AfterClass - public static void tearDownClass() throws Exception { - } - - /** - * The following test is here just to give example code for driving - * the callback mechanism. - * - * @throws java.lang.Exception - */ - @Ignore @Test public void testHandle() throws Exception { - run(); - } - - private void run() throws IOException, UnsupportedCallbackException { - CallbackHandler ch = new DefaultGUICallbackHandler(); ChoiceCallback choiceCB = new ChoiceCallback( "Choose one", - new String[] { - "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh"}, + CHOICES, 0, false); ConfirmationCallback confirmationCB = new ConfirmationCallback( @@ -78,26 +98,32 @@ NameCallback nameCB = new NameCallback("Username", "who"); PasswordCallback passwordCB = new PasswordCallback("Password", false); + passwordCB.setPassword("".toCharArray()); + TextInputCallback textInCB = new TextInputCallback("Enter something interesting", "Good stuff to start with..."); TextOutputCallback textOutCB = new TextOutputCallback(TextOutputCallback.WARNING, "Some fascinating text of great interest to the user goes here"); LanguageCallback langCB = new LanguageCallback(); - Callback [] callbacks = new Callback[] { + Callback[] callbacks = new Callback[] { choiceCB, confirmationCB, nameCB, passwordCB, textInCB, textOutCB, langCB }; + CallbackHandler ch = new DefaultGUICallbackHandler(); ch.handle(callbacks); - System.out.println("ChoiceCallback choice(s):"); - for (int index : choiceCB.getSelectedIndexes()) { - if (index > 0) { - System.out.println(" " + choiceCB.getChoices()[index]); - } else { + if (choiceCB.getSelectedIndexes() == null) { + System.out.println("ChoiceCallback: nothing selected."); + } else { + assertEquals(1, choiceCB.getSelectedIndexes().length, "ChoiceCallback choice"); + final int firstSelected = choiceCB.getSelectedIndexes()[0]; + if (firstSelected == -1) { System.out.println(" Selection not made"); + } else { + assertThat("ChoiceCallback choice", choiceCB.getSelectedIndexes()[0], + allOf(greaterThanOrEqualTo(0), lessThan(CHOICES.length))); } } - System.out.print("ConfirmationCallback result: "); if (confirmationCB.getOptions() == null) { System.out.println(confirmationResultToString(confirmationCB.getSelectedIndex())); @@ -105,10 +131,13 @@ System.out.println(confirmationCB.getOptions()[confirmationCB.getSelectedIndex()]); } - System.out.println("NameCallback result: " + nameCB.getName()); - System.out.println("PasswordCallback result: " + new String(passwordCB.getPassword())); - System.out.println("TextInputCallback result: " + textInCB.getText()); - System.out.println("LanguageCallback result: " + langCB.getLocale().getDisplayName()); + assertNotNull(passwordCB.getPassword()); + assertAll( + () -> assertEquals("who", nameCB.getName(), "NameCallback result"), + () -> assertEquals("", new String(passwordCB.getPassword()), "PasswordCallback result"), + () -> assertNull(textInCB.getText(), "TextInputCallback result"), + () -> assertNull(langCB.getLocale(), "LanguageCallback result") + ); } private String confirmationResultToString(int result) {