001: package fitnesse.runner;
002:
003: import java.io.File;
004:
005: import fitnesse.Arguments;
006: import fitnesse.ComponentFactory;
007: import fitnesse.FitNesseContext;
008: import fitnesse.SuiteExporterArguments;
009: import fitnesse.authentication.Authenticator;
010: import fitnesse.authentication.MultiUserAuthenticator;
011: import fitnesse.authentication.OneUserAuthenticator;
012: import fitnesse.authentication.PromiscuousAuthenticator;
013: import fitnesse.components.CommandLine;
014: import fitnesse.components.Logger;
015: import fitnesse.html.HtmlPageFactory;
016: import fitnesse.html.StiqSuite;
017: import fitnesse.responders.ResponderFactory;
018: import fitnesse.wiki.FileSystemPage;
019: import fitnesse.wiki.PathParser;
020: import fitnesse.wiki.WikiPagePath;
021:
022: public class SuiteExporter {
023:
024: private static FitNesseContext context;
025: public static final String FILE_EXTENSION = ".html";
026: public static final String OPTION_SUITE = "s";
027: public static final String OPTION_DIR = "f";
028: public static final int DEFAULT_PORT = 8765;
029: private static final String exportFailedMessage = "Export failed.";
030:
031: public static void main(String[] args) throws Exception {
032: try {
033: SuiteExporterArguments arguments = parseCommandLine(args);
034: context = new FitNesseContext();
035: context = loadContext(arguments);
036:
037: SuiteExporter suiteExporter = new SuiteExporter(context);
038: suiteExporter.exportSuite(arguments.getSuiteWikiPagePath(),
039: arguments.getOutputDir());
040:
041: System.out.println("Suite ["
042: + arguments.getSuiteWikiPagePath()
043: + "] exported to [" + arguments.getOutputDir()
044: + "]");
045: } catch (Exception e) {
046: if (e.getMessage().length() > 0) {
047: System.out.println(e.getMessage());
048: }
049: System.out.println(exportFailedMessage);
050: }
051:
052: }
053:
054: public SuiteExporter(FitNesseContext context) throws Exception {
055: SuiteExporter.context = context;
056: }
057:
058: public void exportSuite(String suitePagePath, String outputDir)
059: throws Exception {
060: WikiPagePath wikiPagePath = PathParser.parse(suitePagePath);
061: StiqSuite stiqSuite = new StiqSuite(context, wikiPagePath);
062: stiqSuite.writeSuiteAndTests(outputDir);
063: }
064:
065: public static SuiteExporterArguments parseCommandLine(String[] args) {
066: CommandLine commandLine = new CommandLine(
067: "[-p port][-d dir][-r root][-l logDir][-e days][-o][-a userpass]"
068: + "[-" + OPTION_SUITE + " suiteWikiPagePath][-"
069: + OPTION_DIR + " outputDir]");
070: SuiteExporterArguments arguments = null;
071: if (commandLine.parse(args)) {
072: arguments = new SuiteExporterArguments();
073:
074: if (commandLine.hasOption("p"))
075: arguments.setPort(commandLine.getOptionArgument("p",
076: "port"));
077: if (commandLine.hasOption("d"))
078: arguments.setRootPath(commandLine.getOptionArgument(
079: "d", "dir"));
080: if (commandLine.hasOption("r"))
081: arguments.setRootDirectory(commandLine
082: .getOptionArgument("r", "root"));
083: if (commandLine.hasOption("l"))
084: arguments.setLogDirectory(commandLine
085: .getOptionArgument("l", "logDir"));
086: if (commandLine.hasOption("e"))
087: arguments.setDaysTillVersionsExpire(commandLine
088: .getOptionArgument("e", "days"));
089: arguments.setOmitUpdates(commandLine.hasOption("o"));
090: if (commandLine.hasOption("a"))
091: arguments.setUserpass(commandLine.getOptionArgument(
092: "a", "userpass"));
093: if (commandLine.hasOption(OPTION_SUITE))
094: arguments.setSuiteWikiPagePath(commandLine
095: .getOptionArgument(OPTION_SUITE,
096: "suiteWikiPagePath"));
097: if (commandLine.hasOption(OPTION_DIR))
098: arguments.setOutputDir(commandLine.getOptionArgument(
099: OPTION_DIR, "outputDir"));
100: }
101: return arguments;
102: }
103:
104: public static FitNesseContext loadContext(
105: SuiteExporterArguments arguments) throws Exception {
106: context.port = DEFAULT_PORT;
107: context.rootPath = arguments.getRootPath();
108: context.rootPageName = arguments.getRootDirectory();
109: context.rootPagePath = context.rootPath + "/"
110: + context.rootPageName;
111:
112: ComponentFactory componentFactory = new ComponentFactory(
113: context.rootPath);
114: context.root = componentFactory.getRootPage(FileSystemPage
115: .makeRoot(context.rootPath, context.rootPageName));
116:
117: // Other stuff that a FitNesse Context needs to have set per the FitNesse class's implementation
118: context.responderFactory = new ResponderFactory(
119: context.rootPagePath);
120: context.logger = makeLogger(arguments);
121: context.authenticator = makeAuthenticator(arguments
122: .getUserpass(), componentFactory);
123: context.htmlPageFactory = componentFactory
124: .getHtmlPageFactory(new HtmlPageFactory());
125:
126: String extraOutput = componentFactory
127: .loadResponderPlugins(context.responderFactory);
128: extraOutput += componentFactory.loadWikiWidgetPlugins();
129:
130: return context;
131: }
132:
133: private static Logger makeLogger(Arguments arguments) {
134: String logDirectory = arguments.getLogDirectory();
135: return logDirectory != null ? new Logger(logDirectory) : null;
136: }
137:
138: private static Authenticator makeAuthenticator(
139: String authenticationParameter,
140: ComponentFactory componentFactory) throws Exception {
141: Authenticator authenticator = new PromiscuousAuthenticator();
142: if (authenticationParameter != null) {
143: if (new File(authenticationParameter).exists())
144: authenticator = new MultiUserAuthenticator(
145: authenticationParameter);
146: else {
147: String[] values = authenticationParameter.split(":");
148: authenticator = new OneUserAuthenticator(values[0],
149: values[1]);
150: }
151: }
152: return componentFactory.getAuthenticator(authenticator);
153: }
154: }
|