001    package com.khubla.pragmatach.contrib.controllertester;
002    
003    import java.util.List;
004    
005    import org.apache.http.client.methods.HttpGet;
006    import org.apache.http.impl.client.DefaultHttpClient;
007    
008    /**
009     * @author tome
010     */
011    public class RouteTester {
012       /**
013        * client
014        */
015       private static final DefaultHttpClient client = new DefaultHttpClient();
016    
017       public static String generateRandomUrl(String host, RouteUrl routeUrl) {
018          String ret = host;
019          final List<String> parts = routeUrl.getParts();
020          for (final String part : parts) {
021             ret += "/" + part;
022          }
023          return ret;
024       }
025    
026       /**
027        * call route, report if we get something other than 200
028        */
029       public static void testRoute(String host, RouteUrl routeUrl, int trials) throws Exception {
030          try {
031             if (routeUrl.getMethod().compareTo("get") == 0) {
032                /*
033                 * url
034                 */
035                final String url = generateRandomUrl(host, routeUrl);
036                /*
037                 * show the url
038                 */
039                System.out.println(url);
040                /*
041                 * get
042                 */
043                final HttpGet httpGet = new HttpGet(url);
044                /*
045                 * get the return code
046                 */
047                final int ret = client.execute(httpGet).getStatusLine().getStatusCode();
048                /*
049                 * done
050                 */
051                httpGet.releaseConnection();
052                /*
053                 * check
054                 */
055                if (200 != ret) {
056                   System.out.println(ret + " " + routeUrl.getContext());
057                }
058             }
059          } catch (final Exception e) {
060             throw new Exception("Exception in testRoute: '" + routeUrl.getContext() + "'", e);
061          }
062       }
063    }