Skip to content

Commit

Permalink
JBR-6984 Fix BoxLayout/NPECheckRequests test
Browse files Browse the repository at this point in the history
It was initially written and tested on Linux, but it turns out that
on other systems validate() can be called in between init() and start()
calls, which would break the test even though BoxLayout isn't broken.
  • Loading branch information
Sergei Tachenov authored and jbrbot committed Apr 20, 2024
1 parent 9769d61 commit bd22491
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,33 @@
import java.awt.BorderLayout;
import java.lang.reflect.InvocationTargetException;
import java.awt.Dimension;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NPECheckRequests {
private static final Logger LOG = Logger.getLogger(NPECheckRequests.class.getName());
JFrame frame;
JPanel p;
BrokenComponent foo;

public void init() {
frame = new JFrame();
frame = new JFrame() {
@Override
public void validate() {
try {
super.validate();
} catch (BrokenComponentException e) {
if (foo.broken) {
LOG.info("Caught BrokenComponentException in JFrame.validate() - expected, ignored");
} else {
throw e;
}
}
}
};
p = new JPanel();
BoxLayout boxLayout = new BoxLayout(p, BoxLayout.X_AXIS);
p.setLayout(boxLayout);
Expand All @@ -52,8 +67,8 @@ public void init() {
frame.add(p, BorderLayout.CENTER);
try {
frame.pack();
} catch (RuntimeException ignored) {
// our broken component threw an exception
} catch (BrokenComponentException ignored) {
LOG.info("Caught BrokenComponentException in JFrame.pack() - expected, ignored");
}
}

Expand All @@ -77,10 +92,16 @@ private class BrokenComponent extends JPanel {
@Override
public Dimension getPreferredSize() {
if (broken) {
throw new RuntimeException("Broken component");
throw new BrokenComponentException();
}
return super.getPreferredSize();
}
}

private static class BrokenComponentException extends RuntimeException {
BrokenComponentException() {
super("Broken component");
}
}

}

0 comments on commit bd22491

Please sign in to comment.