Example #1
0
  private Object getValueOfField(String fieldname, Class<?> sourceClass, Object source) {
    String sourceClassName = sourceClass.getName();

    Field field = null;
    try {
      field = sourceClass.getDeclaredField(fieldname);
      field.setAccessible(true);
    } catch (SecurityException e) {
      fail(
          "Unable to retrieve "
              + fieldname
              + " field from "
              + sourceClassName
              + ": "
              + e.getCause());
    } catch (NoSuchFieldException e) {
      fail(
          "Unable to retrieve "
              + fieldname
              + " field from "
              + sourceClassName
              + ": "
              + e.getCause());
    }

    assertNotNull("." + fieldname + " field is null!?!", field);
    Object fieldValue = null;
    try {
      fieldValue = field.get(source);
    } catch (IllegalArgumentException e) {
      fail(
          "Unable to retrieve value of "
              + fieldname
              + " from "
              + sourceClassName
              + ": "
              + e.getCause());
    } catch (IllegalAccessException e) {
      fail(
          "Unable to retrieve value of "
              + fieldname
              + " from "
              + sourceClassName
              + ": "
              + e.getCause());
    }
    return fieldValue;
  }
Example #2
0
 public static void authenticateEndpoint(Endpoint endpoint, String user, String password) {
   try {
     Field userField = endpoint.getClass().getSuperclass().getDeclaredField("user");
     userField.setAccessible(true);
     userField.set(endpoint, user);
     Field passwordField = endpoint.getClass().getSuperclass().getDeclaredField("password");
     passwordField.setAccessible(true);
     passwordField.set(endpoint, password);
   } catch (SecurityException e) {
     fail(e.getMessage());
     e.printStackTrace();
   } catch (NoSuchFieldException e) {
     fail(e.getMessage());
     e.printStackTrace();
   } catch (IllegalArgumentException e) {
     fail(e.getMessage());
     e.printStackTrace();
   } catch (IllegalAccessException e) {
     fail(e.getMessage());
     e.printStackTrace();
   }
 }
  /**
   * Tests the update function's functionality. Update is called on every game timer firing.
   *
   * <p>The update function has to move the paddle by the paddle's x velocity and not move in the y
   * direction. It also can not move the paddle past the game window's boundaries.
   */
  @Test
  public final void testUpdate() {
    // Test that the update function can move a paddle.
    Robot robot;
    try {
      robot = new Robot();
      Field paddleField, directionField;
      Paddle paddle;

      // Check that direction starts at 0
      {
        // Gain access to the paddle
        paddleField = testgame.getClass().getDeclaredField("paddle");
        paddleField.setAccessible(true);
        paddle = (Paddle) paddleField.get(testgame);
        // Gain access to the direction field of the paddle
        directionField = paddle.getClass().getDeclaredField("direction");
        directionField.setAccessible(true);

        assertEquals(0, directionField.get(paddle));
      }

      // Check that when 'a' is pressed direction is set to -1
      {
        // Press 'A'
        robot.keyPress(KeyEvent.VK_A);
        // The game needs a little time to register the key
        Thread.sleep(SLEEPDELAY);

        // Gain access to the paddle
        paddleField = testgame.getClass().getDeclaredField("paddle");
        paddleField.setAccessible(true);
        paddle = (Paddle) paddleField.get(testgame);
        directionField = paddle.getClass().getDeclaredField("direction");
        directionField.setAccessible(true);

        assertEquals(-1, directionField.get(paddle));
      }

      // Check that when 'a' is released direction is reset to 0
      {
        // Release 'A'
        robot.keyRelease(KeyEvent.VK_A);
        // The game needs a little time to register the key
        Thread.sleep(SLEEPDELAY);

        // Gain access to the paddle
        paddleField = testgame.getClass().getDeclaredField("paddle");
        paddleField.setAccessible(true);
        paddle = (Paddle) paddleField.get(testgame);
        directionField = paddle.getClass().getDeclaredField("direction");
        directionField.setAccessible(true);

        assertEquals(0, directionField.get(paddle));
      }

      // Check that when 'd' is pressed with 'a' released direction is set
      // to 1
      {
        // Press 'D'
        robot.keyPress(KeyEvent.VK_D);
        // The game needs a little time to register the key
        Thread.sleep(SLEEPDELAY);

        // Gain access to the paddle after a change
        paddleField = testgame.getClass().getDeclaredField("paddle");
        paddleField.setAccessible(true);
        paddle = (Paddle) paddleField.get(testgame);
        directionField = paddle.getClass().getDeclaredField("direction");
        directionField.setAccessible(true);

        assertEquals(1, directionField.get(paddle));
      }

      // Check that when 'd' is released direction is reset to 0
      {
        // Release 'D'
        robot.keyRelease(KeyEvent.VK_D);
        // The game needs a little time to register the key
        Thread.sleep(SLEEPDELAY);

        // Gain access to the paddle after a change
        paddleField = testgame.getClass().getDeclaredField("paddle");
        paddleField.setAccessible(true);
        paddle = (Paddle) paddleField.get(testgame);
        directionField = paddle.getClass().getDeclaredField("direction");
        directionField.setAccessible(true);
        assertEquals(0, directionField.get(paddle));
      }

      // Check that when two direction buttons are pressed, the first
      // one pressed is dominant
      {

        // Press keys, a first.
        robot.keyPress(KeyEvent.VK_A);
        // The game needs a little time to register the key
        Thread.sleep(SLEEPDELAY);
        robot.keyPress(KeyEvent.VK_D);
        // The game needs a little time to register the key
        Thread.sleep(SLEEPDELAY);

        // Gain access to the paddle after a change
        paddleField = testgame.getClass().getDeclaredField("paddle");
        paddleField.setAccessible(true);
        paddle = (Paddle) paddleField.get(testgame);
        directionField = paddle.getClass().getDeclaredField("direction");
        directionField.setAccessible(true);

        assertEquals(-1, directionField.get(paddle));

        //                // Release D. A should stay active.
        //                robot.keyRelease(KeyEvent.VK_D);
        //                // The game needs a little time to register the key. This case
        //                // may take a little longer than usual
        //                Thread.sleep(SLEEPDELAY);
        //
        //                // Gain access to the paddle after a change
        //                paddleField = testgame.getClass().getDeclaredField("paddle");
        //                paddleField.setAccessible(true);
        //                paddle = (Paddle) paddleField.get(testgame);
        //                directionField =
        //                        paddle.getClass().getDeclaredField("direction");
        //                directionField.setAccessible(true);
        //
        //                assertEquals(-1, directionField.get(paddle));
        //
        //                // Reapply D and then release A. D should go active.
        //                robot.keyPress(KeyEvent.VK_D);
        //                // The game needs a little time to register the key
        //                Thread.sleep(SLEEPDELAY);
        //                robot.keyRelease(KeyEvent.VK_A);
        //                // The game needs a little time to register the key
        //                Thread.sleep(SLEEPDELAY);
        //
        //                // Gain access to the paddle after a change
        //                paddleField = testgame.getClass().getDeclaredField("paddle");
        //                paddleField.setAccessible(true);
        //                paddle = (Paddle) paddleField.get(testgame);
        //                directionField =
        //                        paddle.getClass().getDeclaredField("direction");
        //                directionField.setAccessible(true);
        //
        //                assertEquals(1, directionField.get(paddle));
      }

    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      fail("Can't access the direction field.");
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
      fail("The direction field does not exist.");
    } catch (AWTException e) {
      e.printStackTrace();
      fail("Can't create the testing robot.");
    } catch (InterruptedException e1) {
      // Fired when the sleep function fails
      e1.printStackTrace();
    } finally {
      robot = null;
    }
  }