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.io.*;
036: import java.util.*;
037:
038: import junit.framework.*;
039:
040: import org.apache.log4j.*;
041:
042: public class TestSymbolGatherer extends TestCase {
043: public static final String TEST_CLASS = "test";
044: public static final String TEST_FILENAME = "classes"
045: + File.separator + "test.class";
046:
047: private SymbolGatherer gatherer;
048: private ClassfileLoader loader;
049:
050: protected void setUp() throws Exception {
051: super .setUp();
052:
053: gatherer = new SymbolGatherer();
054: loader = new AggregatingClassfileLoader();
055: loader
056: .addLoadListener(new LoadListenerVisitorAdapter(
057: gatherer));
058: }
059:
060: public void testEmpty() {
061: assertEquals("Different number of symbols", 0, gatherer
062: .getCollection().size());
063: }
064:
065: public void testOnOneClass() {
066: loader.load(Collections.singleton(TEST_FILENAME));
067:
068: assertTrue("Missing test class name from "
069: + gatherer.getCollection(), gatherer.getCollection()
070: .contains("test"));
071: assertTrue("Missing test.main() method from "
072: + gatherer.getCollection(), gatherer.getCollection()
073: .contains("test.main(java.lang.String[])"));
074: assertTrue("Missing args parameter from "
075: + gatherer.getCollection(), gatherer.getCollection()
076: .contains("test.main(java.lang.String[]): args"));
077: assertTrue("Missing c local variable from "
078: + gatherer.getCollection(), gatherer.getCollection()
079: .contains("test.main(java.lang.String[]): c"));
080: assertTrue("Missing ex local variable from "
081: + gatherer.getCollection(), gatherer.getCollection()
082: .contains("test.main(java.lang.String[]): ex"));
083: assertTrue("Missing test.test() method from "
084: + gatherer.getCollection(), gatherer.getCollection()
085: .contains("test.test()"));
086: assertTrue("Missing this parameter from "
087: + gatherer.getCollection(), gatherer.getCollection()
088: .contains("test.test(): this"));
089: assertEquals("Different number of symbols in "
090: + gatherer.getCollection(), 7, gatherer.getCollection()
091: .size());
092: }
093:
094: public void testClassNamesOnly() {
095: gatherer.setCollectingClassNames(true);
096: gatherer.setCollectingFieldNames(false);
097: gatherer.setCollectingMethodNames(false);
098: gatherer.setCollectingLocalNames(false);
099:
100: loader.load(Collections.singleton(TEST_FILENAME));
101:
102: assertTrue("Missing test class name from "
103: + gatherer.getCollection(), gatherer.getCollection()
104: .contains("test"));
105: assertEquals("Different number of symbols in "
106: + gatherer.getCollection(), 1, gatherer.getCollection()
107: .size());
108: }
109:
110: public void testMethodNamesOnly() {
111: gatherer.setCollectingClassNames(false);
112: gatherer.setCollectingFieldNames(false);
113: gatherer.setCollectingMethodNames(true);
114: gatherer.setCollectingLocalNames(false);
115:
116: loader.load(Collections.singleton(TEST_FILENAME));
117:
118: assertTrue("Missing test.main() method from "
119: + gatherer.getCollection(), gatherer.getCollection()
120: .contains("test.main(java.lang.String[])"));
121: assertTrue("Missing test.test() method from "
122: + gatherer.getCollection(), gatherer.getCollection()
123: .contains("test.test()"));
124: assertEquals("Different number of symbols in "
125: + gatherer.getCollection(), 2, gatherer.getCollection()
126: .size());
127: }
128:
129: public void testLocalNamesOnly() {
130: gatherer.setCollectingClassNames(false);
131: gatherer.setCollectingFieldNames(false);
132: gatherer.setCollectingMethodNames(false);
133: gatherer.setCollectingLocalNames(true);
134:
135: loader.load(Collections.singleton(TEST_FILENAME));
136:
137: assertTrue("Missing args parameter from "
138: + gatherer.getCollection(), gatherer.getCollection()
139: .contains("test.main(java.lang.String[]): args"));
140: assertTrue("Missing c local variable from "
141: + gatherer.getCollection(), gatherer.getCollection()
142: .contains("test.main(java.lang.String[]): c"));
143: assertTrue("Missing ex local variable from "
144: + gatherer.getCollection(), gatherer.getCollection()
145: .contains("test.main(java.lang.String[]): ex"));
146: assertTrue("Missing this parameter from "
147: + gatherer.getCollection(), gatherer.getCollection()
148: .contains("test.test(): this"));
149: assertEquals("Different number of symbols in "
150: + gatherer.getCollection(), 4, gatherer.getCollection()
151: .size());
152: }
153: }
|