001 package com.khubla.pragmatach.contrib.controllertester; 002 003 import java.io.FileInputStream; 004 import java.util.List; 005 006 import org.apache.commons.cli.CommandLine; 007 import org.apache.commons.cli.CommandLineParser; 008 import org.apache.commons.cli.HelpFormatter; 009 import org.apache.commons.cli.Option; 010 import org.apache.commons.cli.OptionBuilder; 011 import org.apache.commons.cli.Options; 012 import org.apache.commons.cli.PosixParser; 013 014 /** 015 * @author tome 016 */ 017 public class ControllerTester { 018 /** 019 * controllers file option 020 */ 021 private static final String FILE_OPTION = "file"; 022 /** 023 * target option 024 */ 025 private static final String URL_OPTION = "url"; 026 027 /** 028 * the usual 029 */ 030 @SuppressWarnings("static-access") 031 public static void main(String[] args) { 032 try { 033 /* 034 * options for commons-cli 035 */ 036 final Options options = new Options(); 037 final Option fo = OptionBuilder.withArgName(FILE_OPTION).isRequired(true).withType(String.class).hasArg().withDescription("controllers file").create(FILE_OPTION); 038 options.addOption(fo); 039 final Option uo = OptionBuilder.withArgName(URL_OPTION).isRequired(true).withType(String.class).hasArg().withDescription("application url").create(URL_OPTION); 040 options.addOption(uo); 041 /* 042 * parse 043 */ 044 final CommandLineParser parser = new PosixParser(); 045 CommandLine cmd = null; 046 try { 047 cmd = parser.parse(options, args); 048 } catch (final Exception e) { 049 e.printStackTrace(); 050 final HelpFormatter formatter = new HelpFormatter(); 051 formatter.printHelp("posix", options); 052 System.exit(0); 053 } 054 /* 055 * vars 056 */ 057 final String url = cmd.getOptionValue(URL_OPTION); 058 final String file = cmd.getOptionValue(FILE_OPTION); 059 /* 060 * read 061 */ 062 final List<RouteUrl> controllerUrls = RouteUrl.readRoutes(new FileInputStream(file)); 063 /* 064 * test 065 */ 066 testUrls(controllerUrls, url); 067 } catch (final Exception e) { 068 e.printStackTrace(); 069 } 070 } 071 072 /** 073 * test the routes 074 */ 075 private static void testUrls(List<RouteUrl> controllerUrls, String url) throws Exception { 076 try { 077 for (final RouteUrl routeUrl : controllerUrls) { 078 RouteTester.testRoute(url, routeUrl, 1); 079 } 080 } catch (final Exception e) { 081 throw new Exception("Exception in testUrls", e); 082 } 083 } 084 }