Axel
Axel

Reputation: 14169

JUnit/Ant in Hudson: How to start tests in headless mode?

I am having trouble with setting up unit tests for a project developed under Netbeans in Hudson. So far, Hudson is running fine, monitoring my git repository, and automatically starting builds.

But when building is finished, Hudson starts the unit tests. And here lies the problem: Some classes use awt, and when started from within Hudson, the tests fail like this:

[junit] Testcase: testParse_SimpleTextAndSymbols(com.dua3.util.text.LatexParserTest):   Caused an ERROR
[junit] Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.
[junit] java.lang.InternalError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.
[junit]     at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
[junit]     at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
[junit]     at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:110)
[junit]     at java.security.AccessController.doPrivileged(Native Method)
[junit]     at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:190)
[junit]     at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:102)
[junit]     at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
[junit]     at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:119)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:190)
[junit]     at java.awt.Toolkit$2.run(Toolkit.java:868)
[junit]     at java.security.AccessController.doPrivileged(Native Method)
[junit]     at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:860)
[junit]     at sun.swing.SwingUtilities2.getSystemMnemonicKeyMask(SwingUtilities2.java:1877)
[junit]     at javax.swing.plaf.basic.BasicLookAndFeel.initComponentDefaults(BasicLookAndFeel.java:752)
[junit]     at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(MetalLookAndFeel.java:434)
[junit]     at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(BasicLookAndFeel.java:148)
[junit]     at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(MetalLookAndFeel.java:1589)
[junit]     at javax.swing.UIManager.setLookAndFeel(UIManager.java:536)
[junit]     at javax.swing.UIManager.setLookAndFeel(UIManager.java:576)
[junit]     at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1345)
[junit]     at javax.swing.UIManager.initialize(UIManager.java:1455)
[junit]     at javax.swing.UIManager.maybeInitialize(UIManager.java:1422)
[junit]     at javax.swing.UIManager.getLookAndFeelDefaults(UIManager.java:1034)
[junit]     at com.dua3.util.graph.javase.swing.SwingFont$SwingFontProvider.getDefaultFont(SwingFont.java:50)
[junit]     at com.dua3.util.graph.Font.getDefault(Font.java:52)
[junit]     at com.dua3.util.text.LatexParser$Settings.<init>(LatexParser.java:39)
[junit]     at com.dua3.util.text.LatexParser.parse(LatexParser.java:61)
[junit]     at com.dua3.util.text.LatexParserTest.testParse_SimpleTextAndSymbols(LatexParserTest.java:49)

I thought it should be enough to check GraphicsEnvironment.isHeadless(), but obviously it isn't. This is the code snippet were it goes wrong:

if(!GraphicsEnvironment.isHeadless()) {
    final UIDefaults lookAndFeelDefaults = UIManager.getLookAndFeelDefaults(); // SwingFont.java:50
    if (lookAndFeelDefaults!=null) {
        swingFont = lookAndFeelDefaults.getFont("defaultFont"); 
    }
}

I tried in the following in Hudson:

All of that doesn't work. It seems those are only passed on to ant, but not onwards when invoking java to run the JUnit tests.

Is there any way to tell ant that I want my tests be run in headless mode?

Upvotes: 1

Views: 1899

Answers (1)

Axel
Axel

Reputation: 14169

I spend some time looking for this and finally found the answer. In short: setting java.awt.headless=true is not enough on linux. You have to unset DISPLAY. So, go to "Manage Hudson", "Configure System", make sure "Environment variables" is checked under "Global Properties" and add a new variable of name DISPLAY (leave the value field blank).

Upvotes: 1

Related Questions