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