From 139e3819cb1d1358300d9cb1bdfc7c4f71a5948c Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 11 Feb 2024 06:51:02 +1000 Subject: [PATCH] Don't fire listeners when deleting profiles during tests, and don't bother validating the UI during tests when the UI isn't visible. Adding more logging in the ProfileTests. --- src/urChatBasic/backend/logging/URLogger.java | 9 +++- .../backend/utils/URPreferencesUtil.java | 2 +- .../backend/utils/URProfilesUtil.java | 27 ++++++---- .../utils/URUncaughtExceptionHandler.java | 2 +- src/urChatBasic/frontend/UserGUI.java | 12 +++-- tests/backend/MessageHandlerTests.java | 2 +- tests/backend/ProfileTests.java | 53 ++++++++++++------- tests/frontend/AppearanceTests.java | 2 +- tests/frontend/LAFTests.java | 2 +- tests/frontend/LineFormatterTests.java | 2 +- tests/utils/TestDriverGUI.java | 21 ++++++-- 11 files changed, 89 insertions(+), 45 deletions(-) diff --git a/src/urChatBasic/backend/logging/URLogger.java b/src/urChatBasic/backend/logging/URLogger.java index df33c72..758155d 100644 --- a/src/urChatBasic/backend/logging/URLogger.java +++ b/src/urChatBasic/backend/logging/URLogger.java @@ -34,10 +34,15 @@ public static void init () throws IOException, URISyntaxException logDir.mkdir(); } - System.setProperty("log4j2.configurationFile", DriverGUI.class.getResource(LOG4J_CONFIG_FILE).toURI().toString()); + File logConfigFile = new File(DriverGUI.class.getResource(LOG4J_CONFIG_FILE).toURI()); + + if(!logConfigFile.exists()) + throw new IOException("LOG FILE NOT FOUND"); + // System.setProperty("log4j2.debug", "true"); + System.setProperty("log4j2.configurationFile", logConfigFile.toString()); - LOGGER = LoggerFactory.getLogger(URLogger.class); + LOGGER = LoggerFactory.getLogger("urchat"); Logger testLog = getLogger(LOGGER.getName(), Logger.class); diff --git a/src/urChatBasic/backend/utils/URPreferencesUtil.java b/src/urChatBasic/backend/utils/URPreferencesUtil.java index f56a601..8a16e6f 100644 --- a/src/urChatBasic/backend/utils/URPreferencesUtil.java +++ b/src/urChatBasic/backend/utils/URPreferencesUtil.java @@ -135,7 +135,7 @@ else if (URProfilesUtil.getActiveProfilePath().nodeExists(styleName)) } catch (Exception e) { - Constants.LOGGER.error("Active Profile: ["+URProfilesUtil.getActiveProfileName()+"] Unable to load ["+loadedStyle.getAttribute("name")+"]"+ " attempted with path: " + stylePrefPath); + Constants.LOGGER.error("Active Profile: ["+URProfilesUtil.getActiveProfileName()+"] Unable to load ["+loadedStyle.getAttribute("name")+"]"+ " attempted with path: " + stylePrefPath, e); return targetStyle; } diff --git a/src/urChatBasic/backend/utils/URProfilesUtil.java b/src/urChatBasic/backend/utils/URProfilesUtil.java index d060f2b..942147f 100644 --- a/src/urChatBasic/backend/utils/URProfilesUtil.java +++ b/src/urChatBasic/backend/utils/URProfilesUtil.java @@ -59,22 +59,17 @@ public static String[] getProfiles () return profileNames; } - public static void deleteProfile (String profileName) + { + deleteProfile(profileName, true); + } + + public static void deleteProfile (String profileName, boolean fireListeners) { try { String[] allProfiles = getProfiles(); - if(allProfiles.length > 1) - { - Constants.LOGGER.info( "Deleting profile [" + profileName + "]."); - Constants.BASE_PREFS.node(profileName).removeNode(); - fireListeners(EventType.DELETE); - } - else - throw new BackingStoreException("Unable to delete the last profile."); - if(profileName.equals(getActiveProfileName())) { if(profileExists(getDefaultProfile())) @@ -85,6 +80,16 @@ public static void deleteProfile (String profileName) } } + if(allProfiles.length > 1) + { + Constants.LOGGER.info( "Deleting profile [" + profileName + "]."); + Constants.BASE_PREFS.node(profileName).removeNode(); + if(fireListeners) + fireListeners(EventType.DELETE); + } + else + throw new BackingStoreException("Unable to delete the last profile."); + } catch (BackingStoreException e) { Constants.LOGGER.error("Problem deleting profile [" + profileName +"]." + e.getLocalizedMessage()); @@ -96,7 +101,7 @@ public static void deleteProfile (String profileName) */ public static void deleteProfile () { - deleteProfile(getActiveProfileName()); + deleteProfile(getActiveProfileName(), true); } public static String getActiveProfileName () diff --git a/src/urChatBasic/backend/utils/URUncaughtExceptionHandler.java b/src/urChatBasic/backend/utils/URUncaughtExceptionHandler.java index 8cdcc75..ab83635 100644 --- a/src/urChatBasic/backend/utils/URUncaughtExceptionHandler.java +++ b/src/urChatBasic/backend/utils/URUncaughtExceptionHandler.java @@ -6,6 +6,6 @@ public class URUncaughtExceptionHandler implements Thread.UncaughtExceptionHandl @Override public void uncaughtException(Thread t, Throwable e) { - Constants.LOGGER.error( "Uncaught exception: " + e.getLocalizedMessage(), e); + Constants.LOGGER.error( "Uncaught exception: " + e.getStackTrace(), e); } } \ No newline at end of file diff --git a/src/urChatBasic/frontend/UserGUI.java b/src/urChatBasic/frontend/UserGUI.java index 7859604..5fc6561 100644 --- a/src/urChatBasic/frontend/UserGUI.java +++ b/src/urChatBasic/frontend/UserGUI.java @@ -849,7 +849,7 @@ public void setNewLAF (String newLAFname) } } catch (Exception e) { - Constants.LOGGER.error("Failed to set Pluggable LAF! " + e.getLocalizedMessage()); + Constants.LOGGER.error("Failed to set Pluggable LAF! ", e); } finally { if (!flatLafAvailable) @@ -859,7 +859,7 @@ public void setNewLAF (String newLAFname) UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { - Constants.LOGGER.error("Failed to setLookAndFeel! " + e.getLocalizedMessage()); + Constants.LOGGER.error("Failed to setLookAndFeel! ", e); } } } @@ -878,10 +878,14 @@ public void setNewLAF (String newLAFname) clientFontPanel.setDefaultStyle(defaultStyle); - SwingUtilities.updateComponentTreeUI(DriverGUI.frame); + if(DriverGUI.frame.isVisible()) + SwingUtilities.updateComponentTreeUI(DriverGUI.frame); + updateExtras(); + // DriverGUI.frame.dispose(); - DriverGUI.frame.validate(); + if(DriverGUI.frame.isVisible()) + DriverGUI.frame.validate(); } // Update the fonts and popup menus - these aren't under the component tree diff --git a/tests/backend/MessageHandlerTests.java b/tests/backend/MessageHandlerTests.java index a6066e5..07487fb 100644 --- a/tests/backend/MessageHandlerTests.java +++ b/tests/backend/MessageHandlerTests.java @@ -65,7 +65,7 @@ public void tearDown () throws Exception testServer.quitChannels(); // URProfilesUtil.getActiveProfilePath().sync(); // URProfilesUtil.getActiveProfilePath().sync(); - URProfilesUtil.deleteProfile(testDriver.getTestProfileName()); + URProfilesUtil.deleteProfile(testDriver.getTestProfileName(), false); TestDriverGUI.closeWindow(); } diff --git a/tests/backend/ProfileTests.java b/tests/backend/ProfileTests.java index 12c3d08..f54a10c 100644 --- a/tests/backend/ProfileTests.java +++ b/tests/backend/ProfileTests.java @@ -1,6 +1,9 @@ package backend; -import static org.testng.AssertJUnit.*; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.Reporter.log; import java.nio.file.Path; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -9,8 +12,7 @@ import java.util.Optional; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; -import org.testng.Reporter; -import org.testng.annotations.AfterMethod; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import urChatBasic.backend.utils.URPreferencesUtil; @@ -29,11 +31,10 @@ public void setUp() throws Exception testDriver = new TestDriverGUI(); } - @AfterMethod(alwaysRun = true) + @AfterClass(alwaysRun = true) public void tearDown () throws Exception { - Reporter.log("Deleting testing profile.", true); - URProfilesUtil.deleteProfile(testDriver.getTestProfileName()); + TestDriverGUI.cleanupTestProfiles(); TestDriverGUI.closeWindow(); } @@ -49,54 +50,68 @@ public void createdTestProfileTest () } @Test - public void deleteTestProfileTest () + public void deleteTestProfileTest () throws InterruptedException { + log("Check it exists", true); if(!URProfilesUtil.profileExists(testDriver.getTestProfileName())) URProfilesUtil.createProfile(testDriver.getTestProfileName()); + + TestDriverGUI.waitForEverything(TestDriverGUI.gui); URProfilesUtil.setActiveProfileName(testDriver.getTestProfileName()); + TestDriverGUI.waitForEverything(TestDriverGUI.gui); assertTrue(URProfilesUtil.getActiveProfileName().equals(testDriver.getTestProfileName())); // Delete the active profile - URProfilesUtil.deleteProfile(testDriver.getTestProfileName()); + URProfilesUtil.deleteProfile(testDriver.getTestProfileName(), false); assertFalse(URProfilesUtil.profileExists(testDriver.getTestProfileName())); } @Test - public void createProfileAndDeleteTest () + public void createProfileAndDeleteTest () throws InterruptedException { String anotherTestProfileName = "createProfileAndDeleteTest" + (new SimpleDateFormat("yyMMdd")).format(new Date()); + log("Create Profile ["+anotherTestProfileName+"]", true); URProfilesUtil.createProfile(anotherTestProfileName); + + log("Wait for stuff", true); + TestDriverGUI.waitForEverything(TestDriverGUI.gui); // Profile Exists assertTrue(URProfilesUtil.profileExists(anotherTestProfileName)); + log("Set profile ["+anotherTestProfileName+"]", true); URProfilesUtil.setActiveProfileName(anotherTestProfileName); - + TestDriverGUI.waitForEverything(TestDriverGUI.gui); // Has the default setting assertEquals(Constants.DEFAULT_TIME_STAMP_FORMAT, URProfilesUtil.getActiveProfilePath().get(Constants.KEY_TIME_STAMP_FORMAT, "ERROR!")); - URProfilesUtil.deleteProfile(anotherTestProfileName); + URProfilesUtil.deleteProfile(anotherTestProfileName, false); } @Test - public void invalidProfileTest () + public void invalidProfileTest () throws InterruptedException { + log("Active Profile [" + URProfilesUtil.getActiveProfileName() + "]", true); String originalActiveProfile = URProfilesUtil.getActiveProfileName(); String anotherTestProfileName = "invalidProfileTest" + (new SimpleDateFormat("yyMMdd")).format(new Date()); // Profile Exists assertFalse("Profile ["+anotherTestProfileName+"] shouldn't exist!",URProfilesUtil.profileExists(anotherTestProfileName)); - + TestDriverGUI.waitForEverything(TestDriverGUI.gui); URProfilesUtil.setActiveProfileName(anotherTestProfileName); - + TestDriverGUI.waitForEverything(TestDriverGUI.gui); assertEquals(originalActiveProfile, URProfilesUtil.getActiveProfileName()); } @Test - public void cloneProfileTest () throws BackingStoreException + public void cloneProfileTest () throws BackingStoreException, InterruptedException { + log("Loading Profile [" + testDriver.getTestProfileName() + "]", true); Preferences originalPathRoot = URProfilesUtil.getProfilePath(testDriver.getTestProfileName()); + + log("Clone Profile [" + testDriver.getTestProfileName() + "]", true); Preferences clonedProfileRoot = URProfilesUtil.cloneProfile(testDriver.getTestProfileName(), Optional.empty()); ArrayList originalNodes = URPreferencesUtil.getAllNodes(originalPathRoot); - + TestDriverGUI.waitForEverything(TestDriverGUI.gui); + log("Checking preferences match original profile", true); for (Preferences originalPrefPath : originalNodes) { Preferences clonedPath = clonedProfileRoot; @@ -122,14 +137,16 @@ public void cloneProfileTest () throws BackingStoreException } @Test - public void switchToClonedProfileTest () throws BackingStoreException + public void switchToClonedProfileTest () throws BackingStoreException, InterruptedException { Preferences clonedProfileRoot = URProfilesUtil.cloneProfile(testDriver.getTestProfileName(), Optional.empty()); final String clonedProfileName; - + TestDriverGUI.waitForEverything(TestDriverGUI.gui); clonedProfileName = Arrays.stream(URProfilesUtil.getProfiles()).filter(e -> clonedProfileRoot.toString().endsWith(e)).findFirst().get(); URProfilesUtil.setActiveProfileName(clonedProfileName); + + TestDriverGUI.waitForEverything(TestDriverGUI.gui); // Delete the cloned profile clonedProfileRoot.removeNode(); } diff --git a/tests/frontend/AppearanceTests.java b/tests/frontend/AppearanceTests.java index 7f2625f..e5283f3 100644 --- a/tests/frontend/AppearanceTests.java +++ b/tests/frontend/AppearanceTests.java @@ -81,7 +81,7 @@ public void tearDown () throws Exception testServer.quitChannels(); // URProfilesUtil.getActiveProfilePath().sync(); // URProfilesUtil.getActiveProfilePath().sync(); - URProfilesUtil.deleteProfile(testDriver.getTestProfileName()); + URProfilesUtil.deleteProfile(testDriver.getTestProfileName(), false); TestDriverGUI.closeWindow(); } diff --git a/tests/frontend/LAFTests.java b/tests/frontend/LAFTests.java index 6ed827d..42c6965 100644 --- a/tests/frontend/LAFTests.java +++ b/tests/frontend/LAFTests.java @@ -33,7 +33,7 @@ public void setUp() throws Exception public void tearDown () throws Exception { Reporter.log("Deleting testing profile.", true); - URProfilesUtil.deleteProfile(testDriver.getTestProfileName()); + URProfilesUtil.deleteProfile(testDriver.getTestProfileName(), false); TestDriverGUI.closeWindow(); } diff --git a/tests/frontend/LineFormatterTests.java b/tests/frontend/LineFormatterTests.java index b840ac1..a28066b 100644 --- a/tests/frontend/LineFormatterTests.java +++ b/tests/frontend/LineFormatterTests.java @@ -58,7 +58,7 @@ public void tearDown () throws Exception log("Quit channels", true); testServer.quitChannels(); log("Delete test profile", true); - URProfilesUtil.deleteProfile(testDriver.getTestProfileName()); + URProfilesUtil.deleteProfile(testDriver.getTestProfileName(), false); log("Close test window", true); TestDriverGUI.closeWindow(); } diff --git a/tests/utils/TestDriverGUI.java b/tests/utils/TestDriverGUI.java index 46d3085..451b78f 100644 --- a/tests/utils/TestDriverGUI.java +++ b/tests/utils/TestDriverGUI.java @@ -5,11 +5,14 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; import javax.swing.JFrame; import javax.swing.SwingUtilities; +import org.testng.Reporter; import static org.testng.Reporter.log; import urChatBasic.backend.utils.URProfilesUtil; import urChatBasic.base.Constants; @@ -22,6 +25,7 @@ public class TestDriverGUI extends DriverGUI { final String testProfileName = "testingprofile" + (new SimpleDateFormat("yyMMddss")).format(new Date()); + static List testProfiles = new ArrayList<>(); public String getTestProfileName () { @@ -37,13 +41,16 @@ public TestDriverGUI () throws IOException, InvocationTargetException, Interrupt SwingUtilities.invokeAndWait(new Runnable() { public void run() { frame = new JFrame("urChat"); + log("Creating test profile [" + testProfileName + "]", true); URProfilesUtil.createProfile(testProfileName); + testProfiles.add(testProfileName); // This will load the default profile + log("Initialize test gui using test profile", true); gui = createGUI(Optional.of(testProfileName)); gui.setTimeLineString(Constants.DEFAULT_TIME_STAMP_FORMAT); gui.setNickFormatString(Constants.DEFAULT_NICK_FORMAT); gui.setupUserGUI(); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(gui); frame.pack(); frame.setVisible(false); @@ -88,6 +95,15 @@ public static void waitForEverything (UserGUI gui) throws InterruptedException } } + public static void cleanupTestProfiles () + { + URProfilesUtil.setActiveProfileName(URProfilesUtil.getDefaultProfile()); + for (String testProfileName : testProfiles) { + Reporter.log("Deleting testing profile ["+testProfileName+"]", true); + URProfilesUtil.deleteProfile(testProfileName, false); + } + } + public static void startTestGUI (UserGUI gui) throws InterruptedException { waitForEverything(gui); @@ -98,10 +114,7 @@ public static void startTestGUI (UserGUI gui) throws InterruptedException public static void closeWindow () { WindowEvent closingEvent = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING); - log("Post WINDOW_CLOSING event", true); Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent); // frame.dispose(); - log("Interrupt thread", true); - Thread.currentThread().interrupt(); } }