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: // Copyright (C) 2003,2004 by Object Mentor, Inc. All rights reserved.
05: // Released under the terms of the GNU General Public License version 2 or
06: // later.
07: package fit;
08:
09: import java.util.*;
10:
11: import fit.exception.*;
12:
13: // REFACTOR The fixture path is really the only part of this
14: // class that needs to be globally accessible.
15: public class FixtureLoader {
16: private static FixtureLoader instance;
17:
18: public static FixtureLoader instance() {
19: if (instance == null) {
20: instance = new FixtureLoader();
21: }
22:
23: return instance;
24: }
25:
26: public Set fixturePathElements = new HashSet() {
27: {
28: add("fit");
29: }
30: };
31:
32: public Fixture disgraceThenLoad(String tableName) throws Throwable {
33: FixtureName fixtureName = new FixtureName(tableName);
34: Fixture fixture = instantiateFirstValidFixtureClass(fixtureName);
35: addPackageToFixturePath(fixture);
36: return fixture;
37: }
38:
39: private void addPackageToFixturePath(Fixture fixture) {
40: Package fixturePackage = fixture.getClass().getPackage();
41: if (fixturePackage != null)
42: addPackageToPath(fixturePackage.getName());
43: }
44:
45: public void addPackageToPath(String name) {
46: fixturePathElements.add(name);
47: }
48:
49: private Fixture instantiateFixture(String fixtureName)
50: throws Throwable {
51: Class classForFixture = loadFixtureClass(fixtureName);
52: FixtureClass fixtureClass = new FixtureClass(classForFixture);
53: return fixtureClass.newInstance();
54: }
55:
56: private Class loadFixtureClass(String fixtureName) {
57: try {
58: return Class.forName(fixtureName);
59: } catch (ClassNotFoundException deadEnd) {
60: if (deadEnd.getMessage().equals(fixtureName))
61: throw new NoSuchFixtureException(fixtureName);
62: else
63: throw new CouldNotLoadComponentFitFailureException(
64: deadEnd.getMessage(), fixtureName);
65: }
66: }
67:
68: private Fixture instantiateFirstValidFixtureClass(
69: FixtureName fixtureName) throws Throwable {
70: for (Iterator i = fixtureName.getPotentialFixtureClassNames(
71: fixturePathElements).iterator(); i.hasNext();) {
72: String each = (String) i.next();
73: try {
74: return instantiateFixture(each);
75: } catch (NoSuchFixtureException ignoreAndTryTheNextCandidate) {
76: }
77: }
78:
79: throw new NoSuchFixtureException(fixtureName.toString());
80: }
81: }
|