Coverage Report - com.jeantessier.dependency.TestCodeDependencyCollectorUsingMocks
 
Classes in this File Line Coverage Branch Coverage Complexity
TestCodeDependencyCollectorUsingMocks
100%
72/72
N/A
1
TestCodeDependencyCollectorUsingMocks$1
100%
7/7
N/A
1
TestCodeDependencyCollectorUsingMocks$10
100%
7/7
N/A
1
TestCodeDependencyCollectorUsingMocks$11
100%
3/3
N/A
1
TestCodeDependencyCollectorUsingMocks$12
100%
6/6
N/A
1
TestCodeDependencyCollectorUsingMocks$2
100%
13/13
N/A
1
TestCodeDependencyCollectorUsingMocks$3
100%
20/20
N/A
1
TestCodeDependencyCollectorUsingMocks$4
100%
19/19
N/A
1
TestCodeDependencyCollectorUsingMocks$5
100%
14/14
N/A
1
TestCodeDependencyCollectorUsingMocks$6
100%
4/4
N/A
1
TestCodeDependencyCollectorUsingMocks$7
100%
7/7
N/A
1
TestCodeDependencyCollectorUsingMocks$8
100%
8/8
N/A
1
TestCodeDependencyCollectorUsingMocks$9
100%
9/9
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.dependency;
 34  
 
 35  
 import java.util.*;
 36  
 
 37  
 import org.jmock.*;
 38  
 import org.jmock.api.*;
 39  
 import org.jmock.integration.junit3.*;
 40  
 import org.jmock.lib.legacy.*;
 41  
 import static org.jmock.lib.script.ScriptedAction.*;
 42  
 
 43  
 import com.jeantessier.classreader.*;
 44  
 
 45  77
 public class TestCodeDependencyCollectorUsingMocks extends MockObjectTestCase {
 46  
     private static final String TEST_CLASS_NAME = "a.A";
 47  
     private static final String TEST_SUPERCLASS_NAME = "a.Parent";
 48  
     private static final String TEST_INTERFACE_NAME = "a.I";
 49  
     private static final String TEST_EXCEPTION_NAME = "java.lang.Exception";
 50  
 
 51  
     private NodeFactory mockFactory;
 52  
     private Classfile mockClassfile;
 53  
     private ClassNode mockClassNode;
 54  
 
 55  
     private CodeDependencyCollector sut;
 56  
 
 57  
     protected void setUp() throws Exception {
 58  11
         super.setUp();
 59  
 
 60  11
         setImposteriser(ClassImposteriser.INSTANCE);
 61  
 
 62  11
         mockFactory = mock(NodeFactory.class);
 63  11
         mockClassfile = mock(Classfile.class);
 64  11
         mockClassNode = mock(ClassNode.class, "testclass");
 65  
 
 66  11
         sut = new CodeDependencyCollector(mockFactory);
 67  11
     }
 68  
 
 69  
     public void testVisitClass_info_exceptions() {
 70  1
         final Class_info mockException = mock(Class_info.class);
 71  1
         final ClassNode mockExceptionNode = mock(ClassNode.class);
 72  
 
 73  1
         checking(new Expectations() {{
 74  1
             one (mockException).getName();
 75  1
                 will(returnValue(TEST_EXCEPTION_NAME));
 76  1
             one (mockFactory).createClass(TEST_EXCEPTION_NAME);
 77  1
                 will(returnValue(mockExceptionNode));
 78  1
             one (mockClassNode).addDependency(mockExceptionNode);
 79  1
         }});
 80  
 
 81  1
         sut.setCurrent(mockClassNode);
 82  1
         sut.visitClass_info(mockException);
 83  1
     }
 84  
 
 85  
     public void testVisitClassfile_withoutsuperclass() {
 86  1
         expectClassNodeForClassname();
 87  
 
 88  1
         checking(new Expectations() {{
 89  1
             atLeast(1).of (mockClassfile).getClassName();
 90  1
                 will(returnValue(TEST_CLASS_NAME));
 91  1
             one (mockFactory).createClass(TEST_CLASS_NAME, true);
 92  1
                 will(returnValue(mockClassNode));
 93  1
             one (mockClassfile).getSuperclassIndex();
 94  1
                 will(returnValue(0));
 95  1
             never (mockClassNode).addParent(with(any(ClassNode.class)));
 96  1
             ignoring (mockClassfile).getAllInterfaces();
 97  1
             ignoring (mockClassfile).getAllFields();
 98  1
             ignoring (mockClassfile).getAllMethods();
 99  1
             ignoring (mockClassfile).getAttributes();
 100  1
         }});
 101  
 
 102  1
         sut.visitClassfile(mockClassfile);
 103  1
     }
 104  
 
 105  
     public void testVisitClassfile_withsuperclass() {
 106  1
         final Class_info mockRawSuperclass = mock(Class_info.class);
 107  1
         final ClassNode mockSuperclassNode = mock(ClassNode.class, "superclass");
 108  
 
 109  1
         expectClassNodeForClassname();
 110  
 
 111  1
         checking(new Expectations() {{
 112  1
             atLeast(1).of (mockClassfile).getClassName();
 113  1
                 will(returnValue(TEST_CLASS_NAME));
 114  1
             one (mockFactory).createClass(TEST_CLASS_NAME, true);
 115  1
                 will(returnValue(mockClassNode));
 116  1
             one (mockClassfile).getSuperclassIndex();
 117  1
                 will(returnValue(1));
 118  1
             one (mockClassfile).getRawSuperclass();
 119  1
                 will(returnValue(mockRawSuperclass));
 120  1
             one (mockRawSuperclass).accept(sut);
 121  1
             atLeast(1).of (mockRawSuperclass).getName();
 122  1
                 will(returnValue(TEST_SUPERCLASS_NAME));
 123  1
             atLeast(1).of (mockFactory).createClass(TEST_SUPERCLASS_NAME);
 124  1
                 will(returnValue(mockSuperclassNode));
 125  1
             one (mockClassNode).addParent(mockSuperclassNode);
 126  1
             ignoring (mockClassfile).getAllInterfaces();
 127  1
             ignoring (mockClassfile).getAllFields();
 128  1
             ignoring (mockClassfile).getAllMethods();
 129  1
             ignoring (mockClassfile).getAttributes();
 130  1
         }});
 131  
 
 132  1
         sut.visitClassfile(mockClassfile);
 133  1
     }
 134  
 
 135  
     public void testVisitClassfile_withinterface() {
 136  1
         final Class_info mockInterface = mock(Class_info.class);
 137  1
         final ClassNode mockInterfaceNode = mock(ClassNode.class, "interface");
 138  
 
 139  1
         final Collection<Class_info> allInterfaces = new ArrayList<Class_info>();
 140  1
         allInterfaces.add(mockInterface);
 141  
 
 142  1
         expectClassNodeForClassname();
 143  
 
 144  1
         checking(new Expectations() {{
 145  1
             atLeast(1).of (mockClassfile).getClassName();
 146  1
                 will(returnValue(TEST_CLASS_NAME));
 147  1
             one (mockFactory).createClass(TEST_CLASS_NAME, true);
 148  1
                 will(returnValue(mockClassNode));
 149  1
             one (mockClassfile).getSuperclassIndex();
 150  1
                 will(returnValue(0));
 151  1
             one (mockClassfile).getAllInterfaces();
 152  1
                 will(returnValue(allInterfaces));
 153  1
             one (mockInterface).accept(sut);
 154  1
             atLeast(1).of (mockInterface).getName();
 155  1
                 will(returnValue(TEST_INTERFACE_NAME));
 156  1
             atLeast(1).of (mockFactory).createClass(TEST_INTERFACE_NAME);
 157  1
                 will(returnValue(mockInterfaceNode));
 158  1
             one (mockClassNode).addParent(mockInterfaceNode);
 159  1
             ignoring (mockClassfile).getAllFields();
 160  1
             ignoring (mockClassfile).getAllMethods();
 161  1
             ignoring (mockClassfile).getAttributes();
 162  1
         }});
 163  
 
 164  1
         sut.visitClassfile(mockClassfile);
 165  1
     }
 166  
 
 167  
     public void testVisitClassfile_fireevents() {
 168  1
         final DependencyListener mockListener = mock(DependencyListener.class);
 169  
 
 170  1
         expectClassNodeForClassname();
 171  
 
 172  1
         checking(new Expectations() {{
 173  1
             atLeast(1).of (mockClassfile).getClassName();
 174  1
                 will(returnValue(TEST_CLASS_NAME));
 175  
 
 176  1
             one (mockListener).beginClass(with(any(DependencyEvent.class)));
 177  1
                 will(createEventCheckingAction(TEST_CLASS_NAME));
 178  
 
 179  1
             one (mockFactory).createClass(TEST_CLASS_NAME, true);
 180  
 
 181  1
             ignoring (mockClassfile).getSuperclassIndex();
 182  1
             ignoring (mockClassfile).getAllInterfaces();
 183  1
             ignoring (mockClassfile).getAllFields();
 184  1
             ignoring (mockClassfile).getAllMethods();
 185  1
             ignoring (mockClassfile).getAttributes();
 186  
 
 187  1
             one (mockListener).endClass(with(any(DependencyEvent.class)));
 188  1
                 will(createEventCheckingAction(TEST_CLASS_NAME));
 189  
 
 190  1
         }});
 191  
 
 192  1
         sut.addDependencyListener(mockListener);
 193  1
         sut.visitClassfile(mockClassfile);
 194  1
     }
 195  
 
 196  
     public void testVisitExceptionHandler_finally() {
 197  1
         final ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class);
 198  
 
 199  1
         checking(new Expectations() {{
 200  1
             one (mockExceptionHandler).getCatchTypeIndex();
 201  1
                 will(returnValue(0));
 202  1
         }});
 203  
 
 204  1
         sut.visitExceptionHandler(mockExceptionHandler);
 205  1
     }
 206  
 
 207  
     public void testVisitExceptionHandler_catch() {
 208  1
         final ExceptionHandler mockExceptionHandler = mock(ExceptionHandler.class);
 209  1
         final Class_info mockCatchType = mock(Class_info.class);
 210  
 
 211  1
         checking(new Expectations() {{
 212  1
             one (mockExceptionHandler).getCatchTypeIndex();
 213  1
                 will(returnValue(1));
 214  1
             one (mockExceptionHandler).getRawCatchType();
 215  1
                 will(returnValue(mockCatchType));
 216  1
             one (mockCatchType).accept(sut);
 217  1
         }});
 218  
 
 219  1
         sut.visitExceptionHandler(mockExceptionHandler);
 220  1
     }
 221  
 
 222  
     public void testVisitAnnotation() {
 223  1
         final Annotation mockAnnotation = mock(Annotation.class);
 224  1
         final ClassNode mockDependableClassNode = mock(ClassNode.class, "dependable");
 225  
 
 226  1
         checking(new Expectations() {{
 227  1
             one (mockAnnotation).getType();
 228  1
                 will(returnValue("dependable.Dependable"));
 229  1
             one (mockFactory).createClass("dependable.Dependable");
 230  1
                 will(returnValue(mockDependableClassNode));
 231  1
             one (mockClassNode).addDependency(mockDependableClassNode);
 232  
 
 233  1
             ignoring (mockAnnotation).getElementValuePairs();
 234  1
         }});
 235  
 
 236  1
         sut.setCurrent(mockClassNode);
 237  1
         sut.visitAnnotation(mockAnnotation);
 238  1
     }
 239  
 
 240  
     public void testVisitEnumElementValue() {
 241  1
         final EnumElementValue mockEnumElementValue = mock(EnumElementValue.class);
 242  1
         final FeatureNode mockDependableFeatureNode = mock(FeatureNode.class, "dependable.CONSTANT");
 243  
 
 244  1
         checking(new Expectations() {{
 245  1
             one (mockEnumElementValue).getTypeName();
 246  1
                 will(returnValue("dependable.Dependable"));
 247  1
             one (mockEnumElementValue).getConstName();
 248  1
                 will(returnValue("CONSTANT"));
 249  1
             one (mockFactory).createFeature("dependable.Dependable.CONSTANT");
 250  1
                 will(returnValue(mockDependableFeatureNode));
 251  1
             one (mockClassNode).addDependency(mockDependableFeatureNode);
 252  1
         }});
 253  
 
 254  1
         sut.setCurrent(mockClassNode);
 255  1
         sut.visitEnumElementValue(mockEnumElementValue);
 256  1
     }
 257  
 
 258  
     public void testVisitClassElementValue() {
 259  1
         final ClassElementValue mockClassElementValue = mock(ClassElementValue.class);
 260  1
         final ClassNode mockDependableClassNode = mock(ClassNode.class, "dependable");
 261  
 
 262  1
         checking(new Expectations() {{
 263  1
             one (mockClassElementValue).getClassInfo();
 264  1
                 will(returnValue("dependable.Dependable"));
 265  1
             one (mockFactory).createClass("dependable.Dependable");
 266  1
                 will(returnValue(mockDependableClassNode));
 267  1
             one (mockClassNode).addDependency(mockDependableClassNode);
 268  1
         }});
 269  
 
 270  1
         sut.setCurrent(mockClassNode);
 271  1
         sut.visitClassElementValue(mockClassElementValue);
 272  1
     }
 273  
 
 274  
     public void testVisitClassAnnotations() {
 275  1
         expectClassNodeForClassname();
 276  
 
 277  1
         checking(new Expectations() {{
 278  1
             ignoring (mockClassfile).getAttributes();
 279  1
         }});
 280  
 
 281  1
         sut.visitClassfileAttributes(mockClassfile);
 282  1
     }
 283  
 
 284  
     private void expectClassNodeForClassname() {
 285  5
         checking(new Expectations() {{
 286  5
             one (mockClassfile).getClassName();
 287  5
                 will(returnValue(TEST_CLASS_NAME));
 288  5
             one (mockFactory).createClass(TEST_CLASS_NAME);
 289  5
                 will(returnValue(mockClassNode));
 290  5
         }});
 291  5
     }
 292  
 
 293  
     private Action createEventCheckingAction(final String expectedClassName) {
 294  2
         return
 295  
             perform(
 296  
                 "junit.framework.Assert.assertEquals(\"source\", sut, $0.getSource());" +
 297  
                 "junit.framework.Assert.assertEquals(\"classname\", expectedClassName, $0.getClassName());" +
 298  
                 "junit.framework.Assert.assertNull(\"dependent\", $0.getDependent());" +
 299  
                 "junit.framework.Assert.assertNull(\"dependable\", $0.getDependable())")
 300  
                 .where("sut", sut)
 301  
                 .where("expectedClassName", expectedClassName);
 302  
     }
 303  
 }