Title: [jbehave] [602] trunk/core/src/java/jbehave/core/mock: [EK] Lots of little fixes; some Hellbound stuff.
Revision
602
Author
sirenian
Date
2006-12-01 10:04:35 -0600 (Fri, 01 Dec 2006)

Log Message

[EK] Lots of little fixes; some Hellbound stuff. Still struggling with the Swing components so 1 behaviour pending.

Modified Paths

Added Paths

Removed Paths

Diff

Modified: trunk/core/src/behaviour/jbehave/core/UsingConstraintsBehaviour.java (601 => 602)

--- trunk/core/src/behaviour/jbehave/core/UsingConstraintsBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/core/src/behaviour/jbehave/core/UsingConstraintsBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -1,9 +1,21 @@
 package jbehave.core;
 
+import jbehave.core.exception.VerificationException;
 import jbehave.core.mock.UsingConstraints;
 
 public class UsingConstraintsBehaviour extends UsingConstraints {
 
+    Block EXCEPTION_BLOCK = new Block() {
+        public void run() throws Exception {
+            throw new NumberFormatException();
+        }
+    };
+    
+    
+    Block EMPTY_BLOCK = new Block() {
+        public void run() throws Exception {}
+    };
+    
     public void shouldProvideConstraintsForDoubles() {
         ensureThat(5.0, eq(5.0));
         ensureThat(5.0, not(eq(5.1)));
@@ -28,4 +40,29 @@
         ensureThat(true, not(eq(false)));
         ensureThat(true, eq(true), "message");
     }
+    
+    public void shouldProvideConstraintToCheckForNull() {
+        ensureThat(null, isNull());
+        ensureThat(new Object(), isNotNull());
+    }
+
+    public void shouldEnsureThatBlocksThrowTheRightTypeOfExceptionOrNot() throws Exception {
+        
+        ensureThrows(IllegalArgumentException.class, EXCEPTION_BLOCK);
+        ensureDoesNotThrowException(EMPTY_BLOCK);
+        
+        try {
+            ensureThrows(IllegalArgumentException.class, EMPTY_BLOCK);
+            fail("Should have thrown a verification exception");
+        } catch (VerificationException e) {
+            // expected
+        }
+        
+        try {
+            ensureDoesNotThrowException(EXCEPTION_BLOCK);
+            fail("Should have thrown a verification exception");
+        } catch (VerificationException e) {
+            // expected
+        }
+    }
 }

Modified: trunk/core/src/java/jbehave/core/exception/VerificationException.java (601 => 602)

--- trunk/core/src/java/jbehave/core/exception/VerificationException.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/core/src/java/jbehave/core/exception/VerificationException.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -26,7 +26,13 @@
         this(message, null, null);
 	}
 
-	public Object getActual() {
+	public VerificationException(String message, Exception e) {
+        super(message, e);
+        expected = null;
+        actual = null;
+    }
+
+    public Object getActual() {
 		return actual;
 	}
     

Modified: trunk/core/src/java/jbehave/core/mock/UsingConstraints.java (601 => 602)

--- trunk/core/src/java/jbehave/core/mock/UsingConstraints.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/core/src/java/jbehave/core/mock/UsingConstraints.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -56,6 +56,16 @@
         };
     }
     
+
+    
+    public CustomConstraint isNull() {
+        return new CustomConstraint("object is null") {
+            public boolean matches(Object arg) {
+                return arg == null;
+            }
+        };
+    }
+    
 	/** ensures object equals expected value */
 	public CustomConstraint eq(final Object expectedArg) {
 	    return new CustomConstraint("equal to <" + expectedArg + ">") {
@@ -245,15 +255,29 @@
     public void ensureThrows(Class exceptionType, Block block) throws Exception {
         try {
             block.run();
-            fail("should have thrown " + exceptionType.getName());
+            fail("Should have thrown " + exceptionType.getName());
         }
         catch (Exception e) {
             if (!exceptionType.isAssignableFrom(e.getClass())) {
-                throw e;
+                fail("Got exception of wrong type", exceptionType.getName(), e.getClass().getName());
             }
         }
     }
     
+    /**
+     * This allows checks for eg: PendingExceptions, or anything else which would ordinarily
+     * count as a pass. It can also be used for emphasis, or where an exception should
+     * be interpreted as a misbehaviour rather than an error.
+     */
+    public void ensureDoesNotThrowException(Block block) throws Exception {
+        try {
+            block.run();
+        }
+        catch (Exception e) {
+            fail("Should not have thrown exception", e);
+        }
+    }
+    
     /** ensure(...) without constraints */
     public void ensureThat(boolean condition, String message) {
     	if (!condition) {
@@ -271,6 +295,10 @@
     public void fail(String message) {
         throw new VerificationException(message);
     }
+    
+    public void fail(String message, Exception e) {
+        throw new VerificationException(message, e);
+    }
 
     public void fail(String message, Object expected, Object actual) {
         throw new VerificationException(message, expected, actual);

Modified: trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/AllBehaviours.java (601 => 602)

--- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/AllBehaviours.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/AllBehaviours.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -4,6 +4,7 @@
 
 import com.sirenian.hellbound.domain.SegmentsBehaviour;
 import com.sirenian.hellbound.domain.glyph.GlyphBehaviour;
+import com.sirenian.hellbound.domain.glyph.JunkBehaviour;
 import com.sirenian.hellbound.domain.glyph.LivingGlyphBehaviour;
 import com.sirenian.hellbound.engine.GameBehaviour;
 import com.sirenian.hellbound.engine.PseudoRandomGlyphFactoryBehaviour;
@@ -34,7 +35,8 @@
 				
 			FrontPanelBehaviour.class,
 			HellboundFrameBehaviour.class,
-			PitPanelBehaviour.class
+			PitPanelBehaviour.class,
+            JunkBehaviour.class
 		};
 	}
 

Modified: trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/gui/HellboundFrameBehaviour.java (601 => 602)

--- trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/gui/HellboundFrameBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/behaviour/com/sirenian/hellbound/gui/HellboundFrameBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -38,50 +38,65 @@
 	}
 	
 	public void shouldDisplayTheFrontPanelWhenTheGameIsReady() {
-        setUp();
 		frame.reportGameStateChanged(GameState.READY);		
 		ensureThat(frontPanel.isShowing());
 		ensureThat(!gamePanel.isShowing());
-        tearDown();
 	}
 	
 	public void shouldDisplayGamePanelWhenTheGameIsRunning() {
-        setUp();
 		frame.reportGameStateChanged(GameState.RUNNING);
 		ensureThat(!frontPanel.isShowing());
 		ensureThat(gamePanel.isShowing());
-        tearDown();
 	}
     
     public void shouldRequestThatTheShapeIsDroppedWhenTheSpaceKeyIsPressed() throws Exception {
-        setUp();
-        ensureThatKeycodeProducesRequest(KeyEvent.VK_SPACE, "requestDropGlyph");
-        tearDown();
+        ensureThatKeycodeProducesRequest(' ', "requestDropGlyph");
     }
     
     public void shouldRequestThatTheShapeIsMovedRightWhenTheRightKeyIsPressed() throws Exception {
-        setUp();
         ensureThatKeycodeProducesRequest(KeyEvent.VK_RIGHT, "requestMoveGlyphRight");
-        tearDown();
     }
     
     public void shouldRequestThatTheShapeIsMovedLeftWhenTheMoveLeftKeyIsPressed() throws Exception {
-        setUp();
         ensureThatKeycodeProducesRequest(KeyEvent.VK_LEFT, "requestMoveGlyphLeft");
-        tearDown();
     }
     
     public void shouldRequestThatTheShapeIsMovedDownWhenTheMoveDownKeyIsPressed() throws Exception {
-        setUp();
         ensureThatKeycodeProducesRequest(KeyEvent.VK_DOWN, "requestMoveGlyphDown");
-        tearDown();
     }
+    
+    public void shouldRequestThatTheShapeIsRotatedLeftWhenTheZKeyIsPressed() throws Exception {
+        ensureThatKeycodeProducesRequest('z', "requestRotateGlyphLeft");
+    }
+    
+    public void shouldRequestThatTheShapeIsRotatedRightWhenTheXKeyIsPressed() throws Exception {
+        ensureThatKeycodeProducesRequest('x', "requestRotateGlyphRight");
 
+    }
+
     private void ensureThatKeycodeProducesRequest(int keycode, String expectedRequest) throws TimeoutException {
-        gameRequestListener.expects(expectedRequest).once();
-        
-        frame.reportGameStateChanged(GameState.RUNNING);
-        windowWrapper.pressKey(keycode);
-        verifyMocks();
+        setUp();
+        try {
+            gameRequestListener.expects(expectedRequest).once();
+            
+            frame.reportGameStateChanged(GameState.RUNNING);
+            windowWrapper.pressKeycode(keycode);
+            verifyMocks();
+        } finally {
+            tearDown();
+        }
     }
+    
+    private void ensureThatKeycodeProducesRequest(char keyChar, String expectedRequest) throws TimeoutException {
+        setUp();
+        try {
+            gameRequestListener.expects(expectedRequest).once();
+            
+            frame.reportGameStateChanged(GameState.RUNNING);
+            windowWrapper.pressKeychar(keyChar);
+            verifyMocks();
+        } finally {
+            tearDown();
+        }
+    }
 }

Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/game/GameRequestListener.java (601 => 602)

--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/game/GameRequestListener.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/domain/game/GameRequestListener.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -8,6 +8,8 @@
         public void requestMoveGlyphDown() {}
         public void requestMoveGlyphLeft() {}
         public void requestMoveGlyphRight() {}
+        public void requestRotateGlyphLeft() {}
+        public void requestRotateGlyphRight() {}
     };
 
     void requestStartGame();
@@ -20,4 +22,8 @@
 
     void requestMoveGlyphDown();
 
+    void requestRotateGlyphLeft();
+
+    void requestRotateGlyphRight();
+
 }

Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/Game.java (601 => 602)

--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/Game.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/Game.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -124,6 +124,16 @@
         Logger.debug(this, "Move right glyph requested");
         glyph.requestMoveRight();
     }
+
+    public void requestRotateGlyphLeft() {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public void requestRotateGlyphRight() {
+        // TODO Auto-generated method stub
+        
+    }
 	
 	
 

Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/ThreadedEngineQueue.java (601 => 602)

--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/ThreadedEngineQueue.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/engine/ThreadedEngineQueue.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -65,4 +65,14 @@
         });
     }
 
+    public void requestRotateGlyphLeft() {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public void requestRotateGlyphRight() {
+        // TODO Auto-generated method stub
+        
+    }
+
 }

Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/ActionFactory.java (601 => 602)

--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/ActionFactory.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/ActionFactory.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -44,6 +44,24 @@
                 gameRequestListener.requestDropGlyph();
             }
         };
+    }
+
+    public Action leftRotate(final GameRequestListener gameRequestListener) {
+        return new AbstractAction() {
+            public void actionPerformed(ActionEvent e) {
+                Logger.debug(this, "Adding left rotate action to frame action map");
+                gameRequestListener.requestRotateGlyphLeft();
+            }
+        };
+    }
+
+    public Action rightRotate(final GameRequestListener gameRequestListener) {
+        return new AbstractAction() {
+            public void actionPerformed(ActionEvent e) {
+                Logger.debug(this, "Adding right rotate action to frame action map");
+                gameRequestListener.requestRotateGlyphRight();
+            }
+        };
     }    
 
 }

Modified: trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/HellboundFrame.java (601 => 602)

--- trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/HellboundFrame.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/java/com/sirenian/hellbound/gui/HellboundFrame.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -29,10 +29,12 @@
         contentPanel.add(readyPanel, GameState.READY.toString());
         contentPanel.add(runningPanel, GameState.RUNNING.toString());
         
-        contentPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "drop");
+        contentPanel.getInputMap().put(KeyStroke.getKeyStroke(' '), "drop");
         contentPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
         contentPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
         contentPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");
+        contentPanel.getInputMap().put(KeyStroke.getKeyStroke('z'), "leftRotate");
+        contentPanel.getInputMap().put(KeyStroke.getKeyStroke('x'), "rightRotate");
         
         this.pack();
         this.setVisible(true);
@@ -41,10 +43,13 @@
 	}
 
     public void setGameRequestListener(final GameRequestListener gameRequestListener) {
+        contentPanel.getActionMap().clear();
         contentPanel.getActionMap().put("drop", actionFactory.drop(gameRequestListener));
         contentPanel.getActionMap().put("left", actionFactory.left(gameRequestListener));
         contentPanel.getActionMap().put("right", actionFactory.right(gameRequestListener));
         contentPanel.getActionMap().put("down", actionFactory.down(gameRequestListener));
+        contentPanel.getActionMap().put("leftRotate", actionFactory.leftRotate(gameRequestListener));
+        contentPanel.getActionMap().put("rightRotate", actionFactory.rightRotate(gameRequestListener));
     }
 
 	public void reportGameStateChanged(GameState state) {

Modified: trunk/examples/hellbound/src/stories/com/sirenian/hellbound/events/HellboundEvent.java (601 => 602)

--- trunk/examples/hellbound/src/stories/com/sirenian/hellbound/events/HellboundEvent.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/examples/hellbound/src/stories/com/sirenian/hellbound/events/HellboundEvent.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -37,7 +37,7 @@
 	protected void pressKey(int keycode, World world) {
 		WindowWrapper wrapper = (WindowWrapper) world.get(WorldKey.WINDOW_WRAPPER, null);
 		try {
-			wrapper.pressKey(keycode);
+			wrapper.pressKeycode(keycode);
 		} catch (SwingBehaviourException e) {
 			throw new RuntimeException(e);
 		}

Modified: trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/ClassMockObjectBehaviour.java (601 => 602)

--- trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/ClassMockObjectBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/ClassMockObjectBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -3,7 +3,6 @@
 import java.util.HashMap;
 
 import jbehave.core.Ensure;
-import jbehave.core.exception.PendingException;
 import jbehave.core.minimock.UsingMiniMock;
 import jbehave.core.mock.Mock;
 

Deleted: trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/ConstructorFactoryBehaviour.java (601 => 602)

--- trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/ConstructorFactoryBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/ConstructorFactoryBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -1,15 +0,0 @@
-package jbehave.extensions.classmock;
-
-import jbehave.core.exception.PendingException;
-
-public class ConstructorFactoryBehaviour {
-
-	public void shouldConstructStuff() {
-		// TODO: The ConstructorFactory behaviour was pulled out of the ClassMockObject.
-		// I haven't got round to writing the behaviour yet.
-		// It may be worth injecting this into UsingClassMock; that way users
-		// can provide default instantiations of eg: GlyphType.
-		
-		throw new PendingException();
-	}
-}

Modified: trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/UsingClassMockBehaviour.java (601 => 602)

--- trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/UsingClassMockBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/classmocks/src/behaviour/jbehave/extensions/classmock/UsingClassMockBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -2,10 +2,8 @@
 
 import java.util.HashMap;
 
-import jbehave.core.exception.PendingException;
 import jbehave.core.minimock.UsingMiniMock;
 import jbehave.core.mock.Mock;
-import jbehave.extensions.classmock.ClassMockObjectBehaviour.AClassWithNoConstructorsAtAll;
 
 public class UsingClassMockBehaviour extends UsingMiniMock {
 

Deleted: trunk/extensions/classmocks/src/java/jbehave/extensions/classmock/ConstructorFactory.java (601 => 602)

--- trunk/extensions/classmocks/src/java/jbehave/extensions/classmock/ConstructorFactory.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/classmocks/src/java/jbehave/extensions/classmock/ConstructorFactory.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -1,47 +0,0 @@
-package jbehave.extensions.classmock;
-
-import java.lang.reflect.Modifier;
-
-public class ConstructorFactory {
-
-	static Object construct(Class clazz) throws InstantiationException, IllegalAccessException {
-		Object result = null;
-		if (clazz.isPrimitive()) {
-			result = constructPrimitive(clazz);
-		} else if (clazz.isArray()) {
-			result = new Object[] {};
-		} else if (Modifier.isFinal(clazz.getModifiers())) {
-			result = clazz.newInstance();
-		} else if (clazz.isInterface()) {
-			result = new UsingClassMock().mock(clazz);
-		} else if (clazz.getConstructors().length == 0) {
-			result = null; // and hope they're not using it
-		} else {
-			result = new UsingClassMock().mock(clazz);
-		}
-		return result;
-	}
-
-	private static Object constructPrimitive(Class clazz) {
-		if (clazz == byte.class) {
-			return new Byte((byte) 0);
-		} else if (clazz == boolean.class) {
-			return Boolean.FALSE;
-		} else if (clazz == char.class) {
-			return new Character(' ');
-		} else if (clazz == double.class) {
-			return new Double(0);
-		} else if (clazz == float.class) {
-			return new Float(0);
-		} else if (clazz == int.class) {
-			return new Integer(0);
-		} else if (clazz == long.class) {
-			return new Long(0L);
-		} else if (clazz == short.class) {
-			return new Short((short) 0);
-		} else {
-			throw new IllegalArgumentException("Never heard of a primitive called " + clazz + " before. ");
-		}
-	}
-
-}

Modified: trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/WindowGrabberBehaviour.java (601 => 602)

--- trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/WindowGrabberBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/WindowGrabberBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -5,14 +5,28 @@
 
 import jbehave.core.minimock.UsingMiniMock;
 import jbehave.core.mock.Mock;
-import jbehave.extensions.threaded.QueuedMiniMap;
-import jbehave.extensions.threaded.WindowGrabber;
+import jbehave.extensions.threaded.swing.HeadlessChecker;
 import jbehave.extensions.threaded.time.TimeoutException;
 
 
 public class WindowGrabberBehaviour extends UsingMiniMock {
     
+
+    public void shouldGetAnyWindowFromMiniMap() throws TimeoutException {
+        checkForHeadless();
+        Mock miniMapMock = mock(QueuedMiniMap.class);
+        
+        WindowGrabber grabber = new WindowGrabber((QueuedMiniMap)miniMapMock);
+        
+        miniMapMock.expects("get").with(eq("frame.name"), eq(WindowGrabber.DEFAULT_WINDOW_TIMEOUT));
+        grabber.getWindow("frame.name");
+        
+        verifyMocks();
+        grabber.dispose();
+    }
+    
     public void shouldAddNewWindowsToAndRemoveNewWindowsFromMiniMap() {
+        checkForHeadless();
         Mock miniMapMock = mock(QueuedMiniMap.class);
         WindowGrabber grabber = new WindowGrabber((QueuedMiniMap)miniMapMock);
         
@@ -33,23 +47,12 @@
             // Ensures that window grabber gets close event before mocks verified
             SwingUtilities.invokeAndWait(new Runnable() { public void run() {}});
         } catch (Exception e) {
-            ensureThat(thisDoesntHappen());
+            throw new RuntimeException(e);
         }
     }
 
-	public void shouldGetAnyWindowFromMiniMap() throws TimeoutException {
-        Mock miniMapMock = mock(QueuedMiniMap.class);
-        
-        WindowGrabber grabber = new WindowGrabber((QueuedMiniMap)miniMapMock);
-        
-        miniMapMock.expects("get").with(eq("frame.name"), eq(WindowGrabber.DEFAULT_WINDOW_TIMEOUT));
-        grabber.getWindow("frame.name");
-        
-        verifyMocks();
-        grabber.dispose();
+    
+    private void checkForHeadless() {
+        new HeadlessChecker().check();
     }
-	
-    private boolean thisDoesntHappen() {
-		return false;
-	}	
 }

Modified: trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/ComponentFinderBehaviour.java (601 => 602)

--- trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/ComponentFinderBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/ComponentFinderBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -17,7 +17,7 @@
 public class ComponentFinderBehaviour extends UsingMiniMock {
     
     public void shouldFindMatchingComponentsFromFilter() {
-        
+        checkForHeadless();
         JFrame frame = new JFrame();
         JPanel centrePanel = new JPanel();
         JPanel southPanel = new JPanel();
@@ -49,8 +49,9 @@
         
         verifyMocks();
     }
-    
+
     public void shouldFindExactMatchingComponentFromFilter() throws ComponentFinderException {
+        checkForHeadless();
         JFrame frame = new JFrame();
         JPanel centrePanel = new JPanel();
         JPanel southPanel = new JPanel();
@@ -78,6 +79,7 @@
     }
     
     public void shouldThrowExceptionIfExactComponentSoughtAndNoComponentFound() {
+        checkForHeadless();
         JFrame frame = new JFrame();
                                
         ComponentFinder componentFinder = new ComponentFinder();
@@ -95,6 +97,7 @@
     }
 
 	public void shouldThrowExceptionIfExactComponentSoughtAndMoreThanOneFound() {
+        checkForHeadless();
     	JFrame frame = new JFrame();
     	frame.getContentPane().add(new JButton());
     	frame.getContentPane().add(new JButton());
@@ -118,4 +121,8 @@
 		return false;
 	}
 	
+    
+    private void checkForHeadless() {
+        new HeadlessChecker().check();
+    }
 }

Modified: trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/DefaultWindowWrapperBehaviour.java (601 => 602)

--- trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/DefaultWindowWrapperBehaviour.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/DefaultWindowWrapperBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -14,6 +14,7 @@
 import javax.swing.KeyStroke;
 import javax.swing.text.JTextComponent;
 
+import jbehave.core.exception.PendingException;
 import jbehave.core.minimock.UsingMiniMock;
 import jbehave.core.mock.Constraint;
 import jbehave.core.mock.Mock;
@@ -22,118 +23,171 @@
 public class DefaultWindowWrapperBehaviour extends UsingMiniMock {
 	
 	public void shouldClickAButtonOnAWindow() throws Exception {
-		
-		JFrame frame = new JFrame();
-		frame.setName("a.window");
-		
-		JButton button = new JButton("Press Me!");
-		button.setName("a.button");
-		
-		Mock actionListener = mock(ActionListener.class);
-		actionListener.expects("actionPerformed");
-		
-		button.addActionListener((ActionListener)actionListener);
-		
+		checkForHeadless();
 		DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
-		
-		frame.getContentPane().add(button);
-		frame.pack();
-		frame.setVisible(true);
-		
-		wrapper.clickButton("a.button");
-		
-		frame.dispose();
-		
-		verifyMocks();
+        
+		try {
+            JFrame frame = disposeOnCloseFrame();
+    		frame.setName("a.window");
+    		
+    		JButton button = new JButton("Press Me!");
+    		button.setName("a.button");
+    		
+    		Mock actionListener = mock(ActionListener.class);
+    		actionListener.expects("actionPerformed");
+    		
+    		button.addActionListener((ActionListener)actionListener);
+    		
+    		
+    		frame.getContentPane().add(button);
+    		frame.pack();
+    		frame.setVisible(true);
+    		
+    		wrapper.clickButton("a.button");
+    		
+    		verifyMocks();
+        } finally {
+            wrapper.closeWindow();
+        }
 	}
+
 	
 	public void shouldEnterTextIntoTextComponents() throws Exception {
-		
-		JFrame frame = new JFrame();
-		frame.setName("a.window");
-		
-		JTextComponent textField = new JTextField();
-		textField.setName("a.textfield");
-		
-		JTextComponent textArea = new JTextArea();
-		textArea.setName("b.textarea");
-		
-		frame.getContentPane().setLayout(new FlowLayout());
-		
-		frame.getContentPane().add(textField);
-		frame.getContentPane().add(textArea);
-		frame.pack();
-		
-		DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
-		
-		frame.setVisible(true);
-		wrapper.enterText("a.textfield", "Text1");
-		wrapper.enterText("b.textarea", "Text2");
-		
-		ensureThat(textField.getText(), eq("Text1"));
-		ensureThat(textArea.getText(), eq("Text2"));
-		
-		frame.dispose();
+        checkForHeadless();
+        DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
+
+        try {
+            JFrame frame = disposeOnCloseFrame();
+    		frame.setName("a.window");
+    		
+    		JTextComponent textField = new JTextField();
+    		textField.setName("a.textfield");
+    		
+    		JTextComponent textArea = new JTextArea();
+    		textArea.setName("b.textarea");
+    		
+    		frame.getContentPane().setLayout(new FlowLayout());
+    		
+    		frame.getContentPane().add(textField);
+    		frame.getContentPane().add(textArea);
+    		frame.pack();
+    		
+    		
+    		frame.setVisible(true);
+    		wrapper.enterText("a.textfield", "Text1");
+    		wrapper.enterText("b.textarea", "Text2");
+    		
+    		ensureThat(textField.getText(), eq("Text1"));
+    		ensureThat(textArea.getText(), eq("Text2"));
+        } finally {
+            wrapper.closeWindow();
+        }
 	}
 	
 	public void shouldFindComponent() throws ComponentFinderException, TimeoutException  {
-		DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
-		
-		JFrame frame = new JFrame();
-		frame.setName("a.window");
-		
-		JPanel panel = new JPanel();
-		panel.setName("a.panel");
-		
-		frame.getContentPane().add(panel);
-		frame.setVisible(true);
-		
-		ensureThat(wrapper.findComponent("a.panel"), eq(panel));
-		
-		frame.dispose();
+	    checkForHeadless();
+	    DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
+        try {
+    		
+    		JFrame frame = disposeOnCloseFrame();
+    		frame.setName("a.window");
+    		
+    		JPanel panel = new JPanel();
+    		panel.setName("a.panel");
+    		
+    		frame.getContentPane().add(panel);
+    		frame.setVisible(true);
+    		
+    		ensureThat(wrapper.findComponent("a.panel"), eq(panel));
+        } finally {
+            wrapper.closeWindow();
+        }
 	}
     
     public void shouldCloseWindows() throws TimeoutException {
+        checkForHeadless();
         DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
         
-        JFrame frame = new JFrame();
+        JFrame frame = disposeOnCloseFrame();
         frame.setName("a.window");
         frame.setVisible(true);
         
         wrapper.closeWindow();
-        
         ensureThat(!frame.isShowing());
+        frame.dispose();
     }
+
+
+    private JFrame disposeOnCloseFrame() {
+        JFrame frame = new JFrame();
+        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+        return frame;
+    }
     
-    public void shouldSimulateKeyPresses() throws TimeoutException {
+    public void shouldSimulateKeyPressesForInputMap() throws TimeoutException {
+        checkForHeadless();
+        if (true) { throw new PendingException(); }
 		DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
 		
-        JFrame frame = new JFrame();
-        JPanel panel = new JPanel();
-        frame.setContentPane(panel);
-        frame.setName("a.window");
-        frame.setVisible(true);
+        try {
+            JFrame frame = disposeOnCloseFrame();
+            JPanel panel = new JPanel();
+            panel.getInputMap().put(KeyStroke.getKeyStroke(' '), "an action");
+            frame.setContentPane(panel);
+            frame.setName("a.window");
+            frame.pack();
+            frame.setVisible(true);
+            
+            
+            Mock action = ""
+            action.expects("actionPerformed");
+            panel.getActionMap().put("an action", (Action) action);
+            
+            wrapper.pressKeychar(' ');
+            
+            verifyMocks();
+        } finally {
+            wrapper.closeWindow();
+        }
+    }
+    
+    public void shouldSimulateKeyPressesForKeyListeners() throws TimeoutException {
         
+        checkForHeadless();
+        DefaultWindowWrapper wrapper = new DefaultWindowWrapper("a.window");
         
-        Constraint spaceKeyEvent = new Constraint() {
-			public boolean matches(Object arg) {
-				return ((KeyEvent)arg).getKeyCode() == KeyEvent.VK_SPACE;
-			}
-        };
-        
-        Mock keyListener = mock(KeyListener.class);
-        keyListener.stubs("keyTyped");
-        keyListener.stubs("keyPressed");
-        keyListener.expects("keyReleased").once().with(spaceKeyEvent);
-        
-        Mock action = ""
-        action.expects("actionPerformed");
-        
-        panel.getInputMap().put(KeyStroke.getKeyStroke(' '), "drop");
-        panel.getActionMap().put("drop", (Action) action);
-        
-        frame.addKeyListener((KeyListener) keyListener);
-        
-        wrapper.pressKey(KeyEvent.VK_SPACE);
+        try {
+            JFrame frame = disposeOnCloseFrame();
+            JPanel panel = new JPanel();
+            frame.setContentPane(panel);
+            frame.setName("a.window");
+            frame.pack();
+            frame.setVisible(true);
+            
+            Constraint spaceKeyEvent = new Constraint() {
+                public boolean matches(Object arg) {
+                    return ((KeyEvent)arg).getKeyCode() == KeyEvent.VK_SPACE ||
+                        ((KeyEvent)arg).getKeyChar() == ' ';
+                }
+            };
+            Mock keyListener = mock(KeyListener.class);
+            keyListener.stubs("keyTyped").once().with(spaceKeyEvent);
+            keyListener.stubs("keyPressed").once().with(spaceKeyEvent);
+            keyListener.expects("keyReleased").once().with(spaceKeyEvent);
+            frame.addKeyListener((KeyListener) keyListener);    
+            
+            wrapper.pressKeychar(' ');
+            
+            verifyMocks();
+        } finally {
+            wrapper.closeWindow();
+        }
     }
+    
+
+    private void checkForHeadless() {
+        new HeadlessChecker().check();
+    }
+
+
 }

Added: trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/HeadlessCheckerBehaviour.java (0 => 602)

--- trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/HeadlessCheckerBehaviour.java	                        (rev 0)
+++ trunk/extensions/swing/src/behaviour/jbehave/extensions/threaded/swing/HeadlessCheckerBehaviour.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -0,0 +1,55 @@
+package jbehave.extensions.threaded.swing;
+
+import jbehave.core.Block;
+import jbehave.core.exception.PendingException;
+import jbehave.core.mock.UsingConstraints;
+
+/**
+ * Use this class at the beginning of any Swing behaviours which
+ * shouldn't be run in Headless mode. Those behaviours will be
+ * shown as pending instead (which won't break your automated
+ * build).
+ */
+public class HeadlessCheckerBehaviour extends UsingConstraints {
+
+    private String envHeadless;
+
+    public void shouldThrowAPendingExceptionIfRunningInHeadlessMode() throws Exception {
+        storeCurrentHeadlessMode();
+        
+        final HeadlessChecker headlessChecker = new HeadlessChecker();
+        
+        ensureThrowsExceptionOnHeadless(headlessChecker);
+        ensureDoesNotThrowExceptionWhenNotHeadless(headlessChecker);
+        
+        resetOriginalHeadlessMode();
+    }
+
+    private void ensureDoesNotThrowExceptionWhenNotHeadless(final HeadlessChecker headlessChecker) throws Exception {
+        System.getProperties().remove("java.awt.headless");
+        ensureDoesNotThrowException(new Block() {
+            public void run() throws Exception {
+                headlessChecker.check();
+            }
+        });
+    }
+
+    private void ensureThrowsExceptionOnHeadless(final HeadlessChecker headlessChecker) throws Exception {
+        System.setProperty("java.awt.headless", "true");
+        ensureThrows(PendingException.class, new Block() {
+            public void run() throws Exception {
+                headlessChecker.check();
+            }
+        });
+    }
+
+    private void resetOriginalHeadlessMode() {
+        if (envHeadless != null) {
+            System.setProperty("java.awt.headless", envHeadless);
+        }
+    }
+
+    private void storeCurrentHeadlessMode() {
+        envHeadless = System.getProperty("java.awt.headless");
+    }
+}

Modified: trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/DefaultWindowWrapper.java (601 => 602)

--- trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/DefaultWindowWrapper.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/DefaultWindowWrapper.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -1,7 +1,6 @@
 package jbehave.extensions.threaded.swing;
 
 import java.awt.AWTEvent;
-import java.awt.AWTException;
 import java.awt.Component;
 import java.awt.EventQueue;
 import java.awt.TextComponent;
@@ -83,8 +82,10 @@
         idler.waitForIdle();
     }   
 	
-	public void pressKey(int keycode) throws TimeoutException {
-        
+    /**
+     * Use this for any key which doesn't have a corresponding character.
+     */
+	public void pressKeycode(int keycode) throws TimeoutException {
         //NB: Don't use the Robot.
         //Why not the Robot?
         //Because the Robot tries to press the space bar, which is really annoying if you're running things
@@ -95,6 +96,15 @@
             
 		idler.waitForIdle();
 	}
+    
+    public void pressKeychar(char key) throws TimeoutException {
+        sysQueue.postEvent(createKeyPressEvent(getWindow(), key, KeyEvent.KEY_PRESSED));
+        sysQueue.postEvent(createKeyPressEvent(getWindow(), key, KeyEvent.KEY_RELEASED));
+        sysQueue.postEvent(createKeyPressEvent(getWindow(), key, KeyEvent.KEY_TYPED));
+            
+        idler.waitForIdle();
+    }
+    
 	public Component findComponent(String componentName) throws ComponentFinderException, TimeoutException {
 		return finder.findExactComponent(getWindow(), new NamedComponentFilter(componentName));
 	}
@@ -136,4 +146,9 @@
                 keycode,
                 KeyEvent.CHAR_UNDEFINED);
     }
+
+    public void requestWindowFocus() throws TimeoutException {
+        getWindow().requestFocus();
+        idler.waitForIdle();
+    }
 }

Added: trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/HeadlessChecker.java (0 => 602)

--- trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/HeadlessChecker.java	                        (rev 0)
+++ trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/HeadlessChecker.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -0,0 +1,13 @@
+package jbehave.extensions.threaded.swing;
+
+import jbehave.core.exception.PendingException;
+
+public class HeadlessChecker {
+
+    public void check() {
+        if ("true".equals(System.getProperty("java.awt.headless"))) {
+            throw new PendingException("Cannot verify behaviour when performing in headless mode.");
+        }
+    }
+
+}

Modified: trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/WindowWrapper.java (601 => 602)

--- trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/WindowWrapper.java	2006-12-01 15:44:40 UTC (rev 601)
+++ trunk/extensions/swing/src/java/jbehave/extensions/threaded/swing/WindowWrapper.java	2006-12-01 16:04:35 UTC (rev 602)
@@ -15,5 +15,7 @@
 	
 	public void closeWindow() throws TimeoutException;
 
-	public void pressKey(int keycode) throws TimeoutException;
+	public void pressKeycode(int keycode) throws TimeoutException;
+    
+    public void pressKeychar(char keychar) throws TimeoutException;
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to