CannotLoadFixtureTest.java :  » Testing » StoryTestIQ » fit » Java Open Source

Java Open Source » Testing » StoryTestIQ 
StoryTestIQ » fit » CannotLoadFixtureTest.java
// Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later.
package fit;

import junit.framework.TestCase;
import fit.exception.*;
import fitnesse.fixtures.*;

public class CannotLoadFixtureTest extends TestCase {
  private FixtureLoader fixtureLoader;

  protected void setUp() throws Exception {
    fixtureLoader = new FixtureLoader();
  }

  public void testFixtureClassDoesNotExtendFixture() throws Throwable {
    assertCannotLoadFixture("Successfully loaded a fixture that does not extend Fixture!",
        WouldBeFixture.class.getName(), ClassIsNotFixtureException.class);
  }

  public void testFixtureClassNotEndingInFixtureDoesNotExtendFixture() throws Throwable {
    assertCannotLoadFixtureAfterChoppingOffFixture(
        "Successfully loaded a fixture that does not extend Fixture!", WouldBeFixture.class,
        ClassIsNotFixtureException.class);
  }

  public void testFixtureHasNoDefaultConstructor() throws Throwable {
    assertCannotLoadFixture("Successfully loaded a fixture with no default constructor!",
        NoDefaultConstructorFixture.class.getName(), NoDefaultConstructorFixtureException.class);
  }

  public void testFixtureClassNotEndingInFixtureHasNoDefaultConstructor() throws Throwable {
    assertCannotLoadFixtureAfterChoppingOffFixture(
        "Successfully loaded a fixture with no default constructor!",
        NoDefaultConstructorFixture.class, NoDefaultConstructorFixtureException.class);
  }

  public void testFixtureNameNotFound() throws Throwable {
    assertCannotLoadFixture("Successfully loaded a nonexistent fixture!", "BlahBlahBlah",
        NoSuchFixtureException.class);
  }

  public void testFixtureNameNotFoundEvenAfterAddingOnFixture() throws Throwable {
    try {
      fixtureLoader.disgraceThenLoad("BlahBlahBlah");
      fail("Successfully loaded a nonexistent fixture!");
    } catch (FixtureException expected) {
      assertEquals(NoSuchFixtureException.class, expected.getClass());
      assertEquals("BlahBlahBlah", expected.fixtureName);
    }
  }

  private String chopOffFixture(Class fixtureClass) {
    return fixtureClass.getName().replaceAll("Fixture", "");
  }

  private void assertCannotLoadFixture(String failureMessage, String fixtureName,
      Class expectedExceptionType) throws Throwable {
    try {
      fixtureLoader.disgraceThenLoad(fixtureName);
      fail(failureMessage);
    } catch (FixtureException expected) {
      assertEquals(expectedExceptionType, expected.getClass());
      assertEquals(fixtureName, expected.fixtureName);
    }
  }

  private void assertCannotLoadFixtureAfterChoppingOffFixture(String failureMessage, Class fixtureClass,
      Class expectedExceptionType) throws Throwable {
    try {
      fixtureLoader.disgraceThenLoad(chopOffFixture(fixtureClass));
      fail(failureMessage);
    } catch (FixtureException expected) {
      assertEquals("Got exception: " + expected, expectedExceptionType, expected.getClass());
      assertEquals(fixtureClass.getName(), expected.fixtureName);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.