Clover coverage report - Dependency Finder
Coverage timestamp: Mon Nov 29 2010 15:00:50 PST
file stats: LOC: 472   Methods: 18
NCLOC: 354   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TestClassfile.java - 100% 100% 100%
coverage
 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.classreader.impl;
 34   
 35    import com.jeantessier.classreader.ClassfileLoader;
 36    import org.jmock.Expectations;
 37    import org.jmock.Mockery;
 38    import org.jmock.integration.junit4.JMock;
 39    import org.jmock.lib.legacy.ClassImposteriser;
 40    import org.junit.Before;
 41    import org.junit.Test;
 42    import org.junit.runner.RunWith;
 43   
 44    import java.util.Collection;
 45    import java.util.Collections;
 46   
 47    import static org.hamcrest.Matchers.is;
 48    import static org.hamcrest.Matchers.nullValue;
 49    import static org.junit.Assert.assertThat;
 50   
 51    @RunWith(JMock.class)
 52    public class TestClassfile {
 53    private static final String TEST_PACKAGE_NAME = "foo";
 54    private static final String TEST_CLASS_NAME = TEST_PACKAGE_NAME + ".Foo";
 55    private static final String TEST_FIELD_NAME = TEST_CLASS_NAME + ".foo";
 56    private static final String TEST_METHOD_SIGNATURE = TEST_CLASS_NAME + ".foo()";
 57   
 58    private Mockery context;
 59   
 60    private ClassfileLoader loader;
 61    private ConstantPool constantPool;
 62   
 63  16 @Before
 64    public void setUp() throws Exception {
 65  16 context = new Mockery();
 66  16 context.setImposteriser(ClassImposteriser.INSTANCE);
 67   
 68  16 loader = context.mock(ClassfileLoader.class);
 69  16 constantPool = context.mock(ConstantPool.class);
 70    }
 71   
 72  1 @Test
 73    public void testGetPackageName() {
 74  1 final Class_info classInfo = context.mock(Class_info.class);
 75   
 76  1 context.checking(new Expectations() {{
 77  1 one (constantPool).get(1);
 78  1 will(returnValue(classInfo));
 79  1 one (classInfo).getPackageName();
 80  1 will(returnValue(TEST_PACKAGE_NAME));
 81    }});
 82   
 83  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 84   
 85  1 String actualValue = sut.getPackageName();
 86  1 assertThat("package name", actualValue, is(TEST_PACKAGE_NAME));
 87    }
 88   
 89  1 @Test
 90    public void testLocateField_localField_succeed() throws Exception {
 91  1 final Field_info expectedField = context.mock(Field_info.class, "located field");
 92   
 93  1 Collection<Field_info> fields = Collections.singletonList(expectedField);
 94   
 95  1 context.checking(new Expectations() {{
 96  1 one (expectedField).getName();
 97  1 will(returnValue(TEST_FIELD_NAME));
 98    }});
 99   
 100  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), fields, Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 101   
 102  1 Field_info actualField = (Field_info) sut.locateField(TEST_FIELD_NAME);
 103  1 assertThat("local field", actualField, is(expectedField));
 104    }
 105   
 106  1 @Test
 107    public void testLocateField_publicInheritedField_succeed() throws Exception {
 108  1 final String superclassName = "superclass";
 109  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 110  1 final Field_info expectedField = context.mock(Field_info.class, "located field");
 111   
 112  1 expectClassNameLookup(2, superclassName);
 113   
 114  1 context.checking(new Expectations() {{
 115  1 one (loader).getClassfile(superclassName);
 116  1 will(returnValue(superclass));
 117  1 one (superclass).locateField(TEST_FIELD_NAME);
 118  1 will(returnValue(expectedField));
 119  1 one (expectedField).isPublic();
 120  1 will(returnValue(true));
 121    }});
 122   
 123  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 124   
 125  1 Field_info actualField = (Field_info) sut.locateField(TEST_FIELD_NAME);
 126  1 assertThat("public field", actualField, is(expectedField));
 127    }
 128   
 129  1 @Test
 130    public void testLocateField_protectedInheritedField_succeed() throws Exception {
 131  1 final String superclassName = "superclass";
 132  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 133  1 final Field_info expectedField = context.mock(Field_info.class, "located field");
 134   
 135  1 expectClassNameLookup(2, superclassName);
 136   
 137  1 context.checking(new Expectations() {{
 138  1 one (loader).getClassfile(superclassName);
 139  1 will(returnValue(superclass));
 140  1 one (superclass).locateField(TEST_FIELD_NAME);
 141  1 will(returnValue(expectedField));
 142  1 one (expectedField).isPublic();
 143  1 will(returnValue(false));
 144  1 one (expectedField).isProtected();
 145  1 will(returnValue(true));
 146    }});
 147   
 148  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 149   
 150  1 Field_info actualField = (Field_info) sut.locateField(TEST_FIELD_NAME);
 151  1 assertThat("protected field", actualField, is(expectedField));
 152    }
 153   
 154  1 @Test
 155    public void testLocateField_packageInheritedField_succeed() throws Exception {
 156  1 final Class_info classInfo = context.mock(Class_info.class, "class info");
 157  1 final String superclassName = "superclass";
 158  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 159  1 final Field_info expectedField = context.mock(Field_info.class, "located field");
 160   
 161  1 final Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 162   
 163  1 expectClassNameLookup(2, superclassName);
 164   
 165  1 context.checking(new Expectations() {{
 166  1 one (constantPool).get(1);
 167  1 will(returnValue(classInfo));
 168  1 one (classInfo).getPackageName();
 169  1 will(returnValue(TEST_PACKAGE_NAME));
 170  1 one (loader).getClassfile(superclassName);
 171  1 will(returnValue(superclass));
 172  1 one (superclass).getPackageName();
 173  1 will(returnValue(TEST_PACKAGE_NAME));
 174  1 one (superclass).locateField(TEST_FIELD_NAME);
 175  1 will(returnValue(expectedField));
 176  1 one (expectedField).isPublic();
 177  1 will(returnValue(false));
 178  1 one (expectedField).isProtected();
 179  1 will(returnValue(false));
 180  1 one (expectedField).isPackage();
 181  1 will(returnValue(true));
 182  1 one (expectedField).getClassfile();
 183  1 will(returnValue(sut));
 184    }});
 185   
 186  1 Field_info actualField = (Field_info) sut.locateField(TEST_FIELD_NAME);
 187  1 assertThat("package field", actualField, is(expectedField));
 188    }
 189   
 190  1 @Test
 191    public void testLocateField_packageInheritedField_fail() throws Exception {
 192  1 final Class_info classInfo = context.mock(Class_info.class, "class info");
 193  1 final String superclassName = "superclass";
 194  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 195  1 final Field_info expectedField = context.mock(Field_info.class, "located field");
 196   
 197  1 final Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 198   
 199  1 expectClassNameLookup(2, superclassName);
 200   
 201  1 context.checking(new Expectations() {{
 202  1 one (constantPool).get(1);
 203  1 will(returnValue(classInfo));
 204  1 one (classInfo).getPackageName();
 205  1 will(returnValue(TEST_PACKAGE_NAME));
 206  1 one (loader).getClassfile(superclassName);
 207  1 will(returnValue(superclass));
 208  1 one (superclass).getPackageName();
 209  1 will(returnValue(""));
 210  1 one (superclass).locateField(TEST_FIELD_NAME);
 211  1 will(returnValue(expectedField));
 212  1 one (expectedField).isPublic();
 213  1 will(returnValue(false));
 214  1 one (expectedField).isProtected();
 215  1 will(returnValue(false));
 216  1 one (expectedField).isPackage();
 217  1 will(returnValue(true));
 218  1 one (expectedField).getClassfile();
 219  1 will(returnValue(sut));
 220    }});
 221   
 222  1 Field_info actualField = (Field_info) sut.locateField(TEST_FIELD_NAME);
 223  1 assertThat("package field", actualField, is(nullValue()));
 224    }
 225   
 226  1 @Test
 227    public void testLocateField_privateInheritedField_fail() throws Exception {
 228  1 final String superclassName = "superclass";
 229  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 230  1 final Field_info expectedField = context.mock(Field_info.class, "located field");
 231   
 232  1 expectClassNameLookup(2, superclassName);
 233   
 234  1 context.checking(new Expectations() {{
 235  1 one (loader).getClassfile(superclassName);
 236  1 will(returnValue(superclass));
 237  1 one (superclass).locateField(TEST_FIELD_NAME);
 238  1 will(returnValue(expectedField));
 239  1 one (expectedField).isPublic();
 240  1 will(returnValue(false));
 241  1 one (expectedField).isProtected();
 242  1 will(returnValue(false));
 243  1 one (expectedField).isPackage();
 244  1 will(returnValue(false));
 245    }});
 246   
 247  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 248   
 249  1 Field_info actualField = (Field_info) sut.locateField(TEST_FIELD_NAME);
 250  1 assertThat("local field", actualField, is(nullValue()));
 251    }
 252   
 253  1 @Test
 254    public void testLocateMethod_localMethod_succeed() throws Exception {
 255  1 final Method_info expectedMethod = context.mock(Method_info.class, "located method");
 256   
 257  1 Collection<Method_info> methods = Collections.singletonList(expectedMethod);
 258   
 259  1 context.checking(new Expectations() {{
 260  1 one (expectedMethod).getSignature();
 261  1 will(returnValue(TEST_METHOD_SIGNATURE));
 262    }});
 263   
 264  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), methods, Collections.<Attribute_info>emptyList());
 265   
 266  1 Method_info actualMethod = (Method_info) sut.locateMethod(TEST_METHOD_SIGNATURE);
 267  1 assertThat("local method", actualMethod, is(expectedMethod));
 268    }
 269   
 270  1 @Test
 271    public void testLocateMethod_publicInheritedMethod_succeed() throws Exception {
 272  1 final String superclassName = "superclass";
 273  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 274  1 final Method_info expectedMethod = context.mock(Method_info.class, "located method");
 275   
 276  1 expectClassNameLookup(2, superclassName);
 277   
 278  1 context.checking(new Expectations() {{
 279  1 one (loader).getClassfile(superclassName);
 280  1 will(returnValue(superclass));
 281  1 one (superclass).locateMethod(TEST_METHOD_SIGNATURE);
 282  1 will(returnValue(expectedMethod));
 283  1 one (expectedMethod).isPublic();
 284  1 will(returnValue(true));
 285    }});
 286   
 287  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 288   
 289  1 Method_info actualMethod = (Method_info) sut.locateMethod(TEST_METHOD_SIGNATURE);
 290  1 assertThat("public method", actualMethod, is(expectedMethod));
 291    }
 292   
 293  1 @Test
 294    public void testLocateMethod_protectedInheritedMethod_succeed() throws Exception {
 295  1 final String superclassName = "superclass";
 296  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 297  1 final Method_info expectedMethod = context.mock(Method_info.class, "located method");
 298   
 299  1 expectClassNameLookup(2, superclassName);
 300   
 301  1 context.checking(new Expectations() {{
 302  1 one (loader).getClassfile(superclassName);
 303  1 will(returnValue(superclass));
 304  1 one (superclass).locateMethod(TEST_METHOD_SIGNATURE);
 305  1 will(returnValue(expectedMethod));
 306  1 one (expectedMethod).isPublic();
 307  1 will(returnValue(false));
 308  1 one (expectedMethod).isProtected();
 309  1 will(returnValue(true));
 310    }});
 311   
 312  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 313   
 314  1 Method_info actualMethod = (Method_info) sut.locateMethod(TEST_METHOD_SIGNATURE);
 315  1 assertThat("protected method", actualMethod, is(expectedMethod));
 316    }
 317   
 318  1 @Test
 319    public void testLocateMethod_packageInheritedMethod_succeed() throws Exception {
 320  1 final Class_info classInfo = context.mock(Class_info.class, "class info");
 321  1 final String superclassName = "superclass";
 322  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 323  1 final Method_info expectedMethod = context.mock(Method_info.class, "located method");
 324   
 325  1 final Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 326   
 327  1 expectClassNameLookup(2, superclassName);
 328   
 329  1 context.checking(new Expectations() {{
 330  1 one (constantPool).get(1);
 331  1 will(returnValue(classInfo));
 332  1 one (classInfo).getPackageName();
 333  1 will(returnValue(TEST_PACKAGE_NAME));
 334  1 one (loader).getClassfile(superclassName);
 335  1 will(returnValue(superclass));
 336  1 one (superclass).getPackageName();
 337  1 will(returnValue(TEST_PACKAGE_NAME));
 338  1 one (superclass).locateMethod(TEST_METHOD_SIGNATURE);
 339  1 will(returnValue(expectedMethod));
 340  1 one (expectedMethod).isPublic();
 341  1 will(returnValue(false));
 342  1 one (expectedMethod).isProtected();
 343  1 will(returnValue(false));
 344  1 one (expectedMethod).isPackage();
 345  1 will(returnValue(true));
 346  1 one (expectedMethod).getClassfile();
 347  1 will(returnValue(sut));
 348    }});
 349   
 350  1 Method_info actualMethod = (Method_info) sut.locateMethod(TEST_METHOD_SIGNATURE);
 351  1 assertThat("package method", actualMethod, is(expectedMethod));
 352    }
 353   
 354  1 @Test
 355    public void testLocateMethod_packageInheritedMethod_fail() throws Exception {
 356  1 final Class_info classInfo = context.mock(Class_info.class, "class info");
 357  1 final String superclassName = "superclass";
 358  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 359  1 final Method_info expectedMethod = context.mock(Method_info.class, "located method");
 360   
 361  1 final Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 362   
 363  1 expectClassNameLookup(2, superclassName);
 364   
 365  1 context.checking(new Expectations() {{
 366  1 one (constantPool).get(1);
 367  1 will(returnValue(classInfo));
 368  1 one (classInfo).getPackageName();
 369  1 will(returnValue(TEST_PACKAGE_NAME));
 370  1 one (loader).getClassfile(superclassName);
 371  1 will(returnValue(superclass));
 372  1 one (superclass).getPackageName();
 373  1 will(returnValue(""));
 374  1 one (superclass).locateMethod(TEST_METHOD_SIGNATURE);
 375  1 will(returnValue(expectedMethod));
 376  1 one (expectedMethod).isPublic();
 377  1 will(returnValue(false));
 378  1 one (expectedMethod).isProtected();
 379  1 will(returnValue(false));
 380  1 one (expectedMethod).isPackage();
 381  1 will(returnValue(true));
 382  1 one (expectedMethod).getClassfile();
 383  1 will(returnValue(sut));
 384    }});
 385   
 386  1 Method_info actualMethod = (Method_info) sut.locateMethod(TEST_METHOD_SIGNATURE);
 387  1 assertThat("package method", actualMethod, is(nullValue()));
 388    }
 389   
 390  1 @Test
 391    public void testLocateMethod_privateInheritedMethod_fail() throws Exception {
 392  1 final String superclassName = "superclass";
 393  1 final Classfile superclass = context.mock(Classfile.class, "superclass");
 394  1 final Method_info expectedMethod = context.mock(Method_info.class, "located method");
 395   
 396  1 expectClassNameLookup(2, superclassName);
 397   
 398  1 context.checking(new Expectations() {{
 399  1 one (loader).getClassfile(superclassName);
 400  1 will(returnValue(superclass));
 401  1 one (superclass).locateMethod(TEST_METHOD_SIGNATURE);
 402  1 will(returnValue(expectedMethod));
 403  1 one (expectedMethod).isPublic();
 404  1 will(returnValue(false));
 405  1 one (expectedMethod).isProtected();
 406  1 will(returnValue(false));
 407  1 one (expectedMethod).isPackage();
 408  1 will(returnValue(false));
 409    }});
 410   
 411  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 412   
 413  1 Method_info actualMethod = (Method_info) sut.locateMethod(TEST_METHOD_SIGNATURE);
 414  1 assertThat("private method", actualMethod, is(nullValue()));
 415    }
 416   
 417  1 @Test
 418    public void testIsInnerClass_matchingInnerClassInfo_returnsTrue() throws Exception {
 419  1 final InnerClasses_attribute innerClasses_attribute = context.mock(InnerClasses_attribute.class);
 420  1 final InnerClass innerClass = context.mock(InnerClass.class);
 421   
 422  1 expectClassNameLookup(1, TEST_CLASS_NAME);
 423   
 424  1 context.checking(new Expectations() {{
 425  1 one (innerClasses_attribute).getInnerClasses();
 426  1 will(returnValue(Collections.<InnerClass>singleton(innerClass)));
 427  1 one (innerClass).getInnerClassInfo();
 428  1 will(returnValue(TEST_CLASS_NAME));
 429    }});
 430   
 431  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>singleton(innerClasses_attribute));
 432   
 433  1 assertThat("inner class", sut.isInnerClass(), is(true));
 434    }
 435   
 436  1 @Test
 437    public void testIsInnerClass_emptyInnerClassInfo_returnsFalse() throws Exception {
 438  1 final InnerClasses_attribute innerClasses_attribute = context.mock(InnerClasses_attribute.class);
 439  1 final InnerClass innerClass = context.mock(InnerClass.class);
 440   
 441  1 expectClassNameLookup(1, TEST_CLASS_NAME);
 442   
 443  1 context.checking(new Expectations() {{
 444  1 one (innerClasses_attribute).getInnerClasses();
 445  1 will(returnValue(Collections.<InnerClass>singleton(innerClass)));
 446  1 one (innerClass).getInnerClassInfo();
 447  1 will(returnValue(""));
 448    }});
 449   
 450  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>singleton(innerClasses_attribute));
 451   
 452  1 assertThat("inner class", sut.isInnerClass(), is(false));
 453    }
 454   
 455  1 @Test
 456    public void testIsInnerClass_noInnerClassInfo_returnsFalse() throws Exception {
 457  1 Classfile sut = new Classfile(loader, constantPool, 0x0, 1, 2, Collections.<Class_info>emptyList(), Collections.<Field_info>emptyList(), Collections.<Method_info>emptyList(), Collections.<Attribute_info>emptyList());
 458   
 459  1 assertThat("inner class", sut.isInnerClass(), is(false));
 460    }
 461   
 462  12 private void expectClassNameLookup(final int index, final String value) {
 463  12 final Class_info class_info = context.mock(Class_info.class);
 464   
 465  12 context.checking(new Expectations() {{
 466  12 one (constantPool).get(index);
 467  12 will(returnValue(class_info));
 468  12 one (class_info).getName();
 469  12 will(returnValue(value));
 470    }});
 471    }
 472    }