Skip to content

Commit

Permalink
Don't fire listeners when deleting profiles during tests, and don't b…
Browse files Browse the repository at this point in the history
…other validating the UI during tests when the UI isn't visible. Adding more logging in the ProfileTests.
  • Loading branch information
matty-r committed Feb 10, 2024
1 parent a295547 commit 139e381
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 45 deletions.
9 changes: 7 additions & 2 deletions src/urChatBasic/backend/logging/URLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/urChatBasic/backend/utils/URPreferencesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 16 additions & 11 deletions src/urChatBasic/backend/utils/URProfilesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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());
Expand All @@ -96,7 +101,7 @@ public static void deleteProfile (String profileName)
*/
public static void deleteProfile ()
{
deleteProfile(getActiveProfileName());
deleteProfile(getActiveProfileName(), true);
}

public static String getActiveProfileName ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
12 changes: 8 additions & 4 deletions src/urChatBasic/frontend/UserGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/backend/MessageHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
53 changes: 35 additions & 18 deletions tests/backend/ProfileTests.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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();
}

Expand All @@ -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<Preferences> originalNodes = URPreferencesUtil.getAllNodes(originalPathRoot);

TestDriverGUI.waitForEverything(TestDriverGUI.gui);
log("Checking preferences match original profile", true);
for (Preferences originalPrefPath : originalNodes) {
Preferences clonedPath = clonedProfileRoot;

Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/frontend/AppearanceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/frontend/LAFTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/frontend/LineFormatterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
21 changes: 17 additions & 4 deletions tests/utils/TestDriverGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +25,7 @@
public class TestDriverGUI extends DriverGUI
{
final String testProfileName = "testingprofile" + (new SimpleDateFormat("yyMMddss")).format(new Date());
static List<String> testProfiles = new ArrayList<>();

public String getTestProfileName ()
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}

0 comments on commit 139e381

Please sign in to comment.