Coverage Report - com.jeantessier.dependencyfinder.web.TestBase
 
Classes in this File Line Coverage Branch Coverage Complexity
TestBase
100%
65/65
N/A
1
 
 1  
 /*
 2  
  *  Copyright (c) 2001-2009, Jean Tessier
 3  
  *  All rights reserved.
 4  
  *
 5  
  *  Redistribution and use in source and binary forms, with or without
 6  
  *  modification, are permitted provided that the following conditions
 7  
  *  are met:
 8  
  *
 9  
  *      * Redistributions of source code must retain the above copyright
 10  
  *        notice, this list of conditions and the following disclaimer.
 11  
  *
 12  
  *      * Redistributions in binary form must reproduce the above copyright
 13  
  *        notice, this list of conditions and the following disclaimer in the
 14  
  *        documentation and/or other materials provided with the distribution.
 15  
  *
 16  
  *      * Neither the name of Jean Tessier nor the names of his contributors
 17  
  *        may be used to endorse or promote products derived from this software
 18  
  *        without specific prior written permission.
 19  
  *
 20  
  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  
  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  
  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  
  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 24  
  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25  
  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 26  
  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 27  
  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 28  
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 29  
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 30  
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package com.jeantessier.dependencyfinder.web;
 34  
 
 35  
 import java.io.*;
 36  
 import java.util.*;
 37  
 
 38  
 import javax.servlet.*;
 39  
 
 40  
 import junit.framework.*;
 41  
 
 42  
 import org.apache.log4j.*;
 43  
 
 44  
 import com.meterware.httpunit.*;
 45  
 import com.meterware.servletunit.*;
 46  
 
 47  
 import com.jeantessier.dependency.*;
 48  
 
 49  
 /**
 50  
  * Sets up a dependency graph like the one in <a href="graph.xml">graph.xml</a>.
 51  
  */
 52  72
 public abstract class TestBase extends TestCase {
 53  
     private static final String NO_GRAPH_MESSAGE = "There is no dependency graph at this time.";
 54  
 
 55  
     protected String fooPackageName;
 56  
     protected String fooClassName;
 57  
     protected String fooFeatureName;
 58  
     protected String foo2ClassName;
 59  
     protected String foo2FeatureName;
 60  
     protected String barPackageName;
 61  
     protected String barClassName;
 62  
     protected String barFeatureName;
 63  
     protected String bazPackageName;
 64  
     protected String bazClassName;
 65  
     protected String bazFeatureName;
 66  
     protected String leftPackageName;
 67  
     protected String leftClassName;
 68  
     protected String leftFeatureName;
 69  
     protected String rightPackageName;
 70  
     protected String rightClassName;
 71  
     protected String rightFeatureName;
 72  
 
 73  
     protected NodeFactory factory;
 74  
     protected ServletUnitClient client;
 75  
     protected WebRequest request;
 76  
     protected InvocationContext context;
 77  
 
 78  
     protected String label;
 79  
 
 80  
     protected void setUp() throws Exception {
 81  72
         super.setUp();
 82  
 
 83  72
         Logger.getLogger(getClass()).setLevel(Level.ALL);
 84  
 
 85  72
         Random random = new Random();
 86  
 
 87  72
         fooPackageName = "foo" + random.nextLong();
 88  72
         fooClassName = fooPackageName + ".Foo" + random.nextLong();
 89  72
         fooFeatureName = fooClassName + ".foo" + random.nextLong();
 90  72
         foo2ClassName = fooPackageName + ".Foo2" + random.nextLong();
 91  72
         foo2FeatureName = foo2ClassName + ".foo2" + random.nextLong();
 92  72
         barPackageName = "bar" + random.nextLong();
 93  72
         barClassName = barPackageName + ".Bar" + random.nextLong();
 94  72
         barFeatureName = barClassName + ".bar" + random.nextLong();
 95  72
         bazPackageName = "baz" + random.nextLong();
 96  72
         bazClassName = bazPackageName + ".Baz" + random.nextLong();
 97  72
         bazFeatureName = bazClassName + ".baz" + random.nextLong();
 98  72
         leftPackageName = "left" + random.nextLong();
 99  72
         leftClassName = leftPackageName + ".Left" + random.nextLong();
 100  72
         leftFeatureName = leftClassName + ".left" + random.nextLong();
 101  72
         rightPackageName = "right" + random.nextLong();
 102  72
         rightClassName = rightPackageName + ".Right" + random.nextLong();
 103  72
         rightFeatureName = rightClassName + ".right" + random.nextLong();
 104  
 
 105  72
         factory = new NodeFactory();
 106  72
         FeatureNode foo = factory.createFeature(fooFeatureName);
 107  72
         FeatureNode bar = factory.createFeature(barFeatureName);
 108  72
         FeatureNode baz = factory.createFeature(bazFeatureName);
 109  72
         FeatureNode left = factory.createFeature(leftFeatureName);
 110  72
         FeatureNode right = factory.createFeature(rightFeatureName);
 111  
 
 112  72
         foo.addDependency(bar);
 113  72
         bar.addDependency(baz);
 114  72
         left.addDependency(right);
 115  72
         right.addDependency(left);
 116  
 
 117  72
         ServletRunner runner = new ServletRunner(new File("web/WEB-INF/web.xml"), "/web");
 118  72
         client = runner.newClient();
 119  72
         request = new GetMethodWebRequest(getStartUrl());
 120  72
         context = client.newInvocation(request);
 121  
 
 122  72
         label = "label " + random.nextLong();
 123  
 
 124  72
         getApplication().setAttribute("label", label);
 125  72
         getApplication().setAttribute("factory", factory);
 126  72
     }
 127  
 
 128  
     public void testNoLabel() throws Exception {
 129  10
         getApplication().removeAttribute("label");
 130  
 
 131  10
         context.service();
 132  10
         WebResponse response = client.getResponse(request);
 133  10
         assertEquals("name", "Dependency Finder", response.getElementWithID("name").getText());
 134  10
         assertNull("label", response.getElementWithID("label"));
 135  10
     }
 136  
 
 137  
     public void testLabel() throws Exception {
 138  10
         context.service();
 139  10
         WebResponse response = client.getResponse(request);
 140  10
         assertEquals("name", "Dependency Finder", response.getElementWithID("name").getText());
 141  10
         assertEquals("label", label, response.getElementWithID("label").getText());
 142  10
     }
 143  
 
 144  
     public void testNoDependencyGraph() throws Exception {
 145  10
         getApplication().removeAttribute("factory");
 146  
 
 147  10
         context.service();
 148  10
         WebResponse response = client.getResponse(request);
 149  10
         assertTrue("Missing text \"" + NO_GRAPH_MESSAGE + "\"", response.getText().contains(NO_GRAPH_MESSAGE));
 150  10
     }
 151  
 
 152  
     public void testEmptyDependencyGraph() throws Exception {
 153  10
         getApplication().setAttribute("factory", new NodeFactory());
 154  
 
 155  10
         context.service();
 156  10
         WebResponse response = client.getResponse(request);
 157  10
         assertFalse("Unexpected text \"" + NO_GRAPH_MESSAGE + "\"", response.getText().contains(NO_GRAPH_MESSAGE));
 158  10
     }
 159  
 
 160  
     public void testTestDependencyGraph() throws Exception {
 161  10
         context.service();
 162  10
         WebResponse response = client.getResponse(request);
 163  10
         assertFalse("Unexpected text \"" + NO_GRAPH_MESSAGE + "\"", response.getText().contains(NO_GRAPH_MESSAGE));
 164  10
     }
 165  
 
 166  
     protected ServletContext getApplication() {
 167  176
       return client.getSession(true).getServletContext();
 168  
     }
 169  
 
 170  
     protected abstract String getStartUrl();
 171  
 }