Index: trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java	(revision 9917)
+++ trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java	(revision 9918)
@@ -6,4 +6,5 @@
 
 import java.awt.Dimension;
+import java.awt.GraphicsEnvironment;
 import java.awt.GridBagLayout;
 import java.awt.event.ActionEvent;
@@ -108,13 +109,15 @@
             formats.setToolTipText(tr("Select image format for WMS layer"));
 
-            if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) { {
-                final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
-                scrollPane.setPreferredSize(new Dimension(400, 400));
-                final JPanel panel = new JPanel(new GridBagLayout());
-                panel.add(scrollPane, GBC.eol().fill());
-                panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
-                setContent(panel);
-            } }.showDialog().getValue()) {
-                return null;
+            if (!GraphicsEnvironment.isHeadless()) {
+                if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) { {
+                    final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
+                    scrollPane.setPreferredSize(new Dimension(400, 400));
+                    final JPanel panel = new JPanel(new GridBagLayout());
+                    panel.add(scrollPane, GBC.eol().fill());
+                    panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
+                    setContent(panel);
+                } }.showDialog().getValue()) {
+                    return null;
+                }
             }
 
@@ -141,12 +144,20 @@
             return ret;
         } catch (MalformedURLException ex) {
-            JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
-                    tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            if (!GraphicsEnvironment.isHeadless()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
+                        tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            }
+            Main.error(ex, false);
         } catch (IOException ex) {
-            JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
-                    tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            if (!GraphicsEnvironment.isHeadless()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
+                        tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            }
+            Main.error(ex, false);
         } catch (WMSImagery.WMSGetCapabilitiesException ex) {
-            JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
-                    tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            if (!GraphicsEnvironment.isHeadless()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
+                        tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            }
             Main.error("Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
         }
Index: trunk/src/org/openstreetmap/josm/gui/HelpAwareOptionPane.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/HelpAwareOptionPane.java	(revision 9917)
+++ trunk/src/org/openstreetmap/josm/gui/HelpAwareOptionPane.java	(revision 9918)
@@ -245,5 +245,5 @@
             doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
         }
-        return (Integer) pane.getValue();
+        return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
     }
 
Index: trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java	(revision 9918)
+++ trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java	(revision 9918)
@@ -0,0 +1,75 @@
+// License: GPL. For details, see LICENSE file.
+package org.openstreetmap.josm.actions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openstreetmap.josm.JOSMFixture;
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.imagery.ImageryInfo;
+import org.openstreetmap.josm.gui.layer.TMSLayer;
+import org.openstreetmap.josm.gui.layer.WMSLayer;
+
+/**
+ * Unit tests for class {@link AddImageryLayerAction}.
+ */
+public final class AddImageryLayerActionTest {
+
+    /**
+     * Setup test.
+     */
+    @BeforeClass
+    public static void setUp() {
+        JOSMFixture.createUnitTestFixture().init(true);
+    }
+
+    /**
+     * Unit test of {@link AddImageryLayerAction#updateEnabledState}.
+     */
+    @Test
+    public void testEnabledState() {
+        assertFalse(new AddImageryLayerAction(new ImageryInfo()).isEnabled());
+        assertFalse(new AddImageryLayerAction(new ImageryInfo("google", "http://maps.google.com/api", "tms", null, null)).isEnabled());
+        assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).isEnabled());
+        assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_bing", "http://bar", "bing", null, null)).isEnabled());
+        assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_scanex", "http://bar", "scanex", null, null)).isEnabled());
+        assertFalse(new AddImageryLayerAction(new ImageryInfo("foo_wms_endpoint", "http://bar", "wms_endpoint", null, null)).isEnabled());
+    }
+
+    /**
+     * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases.
+     */
+    @Test
+    public void testActionPerformedEnabled() {
+        assertTrue(Main.map.mapView.getLayersOfType(TMSLayer.class).isEmpty());
+        new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).actionPerformed(null);
+        List<TMSLayer> tmsLayers = Main.map.mapView.getLayersOfType(TMSLayer.class);
+        assertEquals(1, tmsLayers.size());
+
+        try {
+            new AddImageryLayerAction(new ImageryInfo("osm-wms.de", "http://129.206.228.72/cached/osm?",
+                    "wms_endpoint", null, null)).actionPerformed(null);
+            List<WMSLayer> wmsLayers = Main.map.mapView.getLayersOfType(WMSLayer.class);
+            assertEquals(1, wmsLayers.size());
+
+            Main.map.mapView.removeLayer(wmsLayers.get(0));
+        } finally {
+            Main.map.mapView.removeLayer(tmsLayers.get(0));
+        }
+    }
+
+    /**
+     * Unit test of {@link AddImageryLayerAction#actionPerformed} - disabled case.
+     */
+    @Test
+    public void testActionPerformedDisabled() {
+        assertTrue(Main.map.mapView.getLayersOfType(TMSLayer.class).isEmpty());
+        new AddImageryLayerAction(new ImageryInfo()).actionPerformed(null);
+        assertTrue(Main.map.mapView.getLayersOfType(TMSLayer.class).isEmpty());
+    }
+}
Index: trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java
===================================================================
--- trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java	(revision 9917)
+++ trunk/test/unit/org/openstreetmap/josm/gui/preferences/map/TaggingPresetPreferenceTestIT.java	(revision 9918)
@@ -77,6 +77,6 @@
             }
         }
-        assertTrue(allErrors.isEmpty());
-        assertTrue(allMessages.isEmpty());
+        assertTrue(allErrors.toString(), allErrors.isEmpty());
+        assertTrue(allMessages.toString(), allMessages.isEmpty());
     }
 
