001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.util.*;
036: import java.io.*;
037:
038: import junit.framework.*;
039:
040: public class TestVisitorBase extends TestCase {
041: public static final String TEST_CLASS = "test";
042: public static final String TEST_FILENAME = "classes"
043: + File.separator + "test.class";
044:
045: private Classfile classfile;
046: private MockVisitor visitor;
047:
048: protected void setUp() throws Exception {
049: ClassfileLoader loader = new AggregatingClassfileLoader();
050: loader.load(Collections.singleton(TEST_FILENAME));
051: classfile = loader.getClassfile(TEST_CLASS);
052:
053: visitor = new MockVisitor();
054: }
055:
056: public void testVisitCode_attribute() {
057: Code_attribute code = classfile.getMethod(
058: "main(java.lang.String[])").getCode();
059:
060: code.accept(visitor);
061:
062: assertEquals("visited ConstantValue_attributes", 0, visitor
063: .getVisitedConstantValue_attributes().size());
064: assertEquals("visited Code_attributes", 1, visitor
065: .getVisitedCode_attributes().size());
066: assertEquals("visited Exceptions_attributes", 0, visitor
067: .getVisitedExceptions_attributes().size());
068: assertEquals("visited InnerClasses_attributes", 0, visitor
069: .getVisitedInnerClasses_attributes().size());
070: assertEquals("visited Synthetic_attributes", 0, visitor
071: .getVisitedSynthetic_attributes().size());
072: assertEquals("visited SourceFile_attributes", 0, visitor
073: .getVisitedSourceFile_attributes().size());
074: assertEquals("visited LineNumberTable_attributes", 1, visitor
075: .getVisitedLineNumberTable_attributes().size());
076: assertEquals("visited LocalVariableTable_attributes", 1,
077: visitor.getVisitedLocalVariableTable_attributes()
078: .size());
079: assertEquals("visited Deprecated_attributes", 0, visitor
080: .getVisitedDeprecated_attributes().size());
081: assertEquals("visited Custom_attributes", 0, visitor
082: .getVisitedCustom_attributes().size());
083: assertEquals("visited Instructions", 11, visitor
084: .getVisitedInstructions().size());
085: assertEquals("visited ExceptionHandlers", 1, visitor
086: .getVisitedExceptionHandlers().size());
087: assertEquals("visited InnerClasses", 0, visitor
088: .getVisitedInnerClasses().size());
089: assertEquals("visited LineNumbers", 5, visitor
090: .getVisitedLineNumbers().size());
091: assertEquals("visited LocalVariables", 3, visitor
092: .getVisitedLocalVariables().size());
093: }
094:
095: public void testVisitInstruction() {
096: Iterator i = classfile.getMethod("main(java.lang.String[])")
097: .getCode().iterator();
098: Instruction instruction = (Instruction) i.next();
099:
100: instruction.accept(visitor);
101:
102: assertEquals("visited Instructions", 1, visitor
103: .getVisitedInstructions().size());
104: }
105: }
|