01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fit;
04:
05: import junit.framework.TestCase;
06: import fit.exception.*;
07: import fitnesse.fixtures.*;
08:
09: public class CannotLoadFixtureTest extends TestCase {
10: private FixtureLoader fixtureLoader;
11:
12: protected void setUp() throws Exception {
13: fixtureLoader = new FixtureLoader();
14: }
15:
16: public void testFixtureClassDoesNotExtendFixture() throws Throwable {
17: assertCannotLoadFixture(
18: "Successfully loaded a fixture that does not extend Fixture!",
19: WouldBeFixture.class.getName(),
20: ClassIsNotFixtureException.class);
21: }
22:
23: public void testFixtureClassNotEndingInFixtureDoesNotExtendFixture()
24: throws Throwable {
25: assertCannotLoadFixtureAfterChoppingOffFixture(
26: "Successfully loaded a fixture that does not extend Fixture!",
27: WouldBeFixture.class, ClassIsNotFixtureException.class);
28: }
29:
30: public void testFixtureHasNoDefaultConstructor() throws Throwable {
31: assertCannotLoadFixture(
32: "Successfully loaded a fixture with no default constructor!",
33: NoDefaultConstructorFixture.class.getName(),
34: NoDefaultConstructorFixtureException.class);
35: }
36:
37: public void testFixtureClassNotEndingInFixtureHasNoDefaultConstructor()
38: throws Throwable {
39: assertCannotLoadFixtureAfterChoppingOffFixture(
40: "Successfully loaded a fixture with no default constructor!",
41: NoDefaultConstructorFixture.class,
42: NoDefaultConstructorFixtureException.class);
43: }
44:
45: public void testFixtureNameNotFound() throws Throwable {
46: assertCannotLoadFixture(
47: "Successfully loaded a nonexistent fixture!",
48: "BlahBlahBlah", NoSuchFixtureException.class);
49: }
50:
51: public void testFixtureNameNotFoundEvenAfterAddingOnFixture()
52: throws Throwable {
53: try {
54: fixtureLoader.disgraceThenLoad("BlahBlahBlah");
55: fail("Successfully loaded a nonexistent fixture!");
56: } catch (FixtureException expected) {
57: assertEquals(NoSuchFixtureException.class, expected
58: .getClass());
59: assertEquals("BlahBlahBlah", expected.fixtureName);
60: }
61: }
62:
63: private String chopOffFixture(Class fixtureClass) {
64: return fixtureClass.getName().replaceAll("Fixture", "");
65: }
66:
67: private void assertCannotLoadFixture(String failureMessage,
68: String fixtureName, Class expectedExceptionType)
69: throws Throwable {
70: try {
71: fixtureLoader.disgraceThenLoad(fixtureName);
72: fail(failureMessage);
73: } catch (FixtureException expected) {
74: assertEquals(expectedExceptionType, expected.getClass());
75: assertEquals(fixtureName, expected.fixtureName);
76: }
77: }
78:
79: private void assertCannotLoadFixtureAfterChoppingOffFixture(
80: String failureMessage, Class fixtureClass,
81: Class expectedExceptionType) throws Throwable {
82: try {
83: fixtureLoader
84: .disgraceThenLoad(chopOffFixture(fixtureClass));
85: fail(failureMessage);
86: } catch (FixtureException expected) {
87: assertEquals("Got exception: " + expected,
88: expectedExceptionType, expected.getClass());
89: assertEquals(fixtureClass.getName(), expected.fixtureName);
90: }
91: }
92: }
|