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:
037: import junit.framework.*;
038: import java.util.*;
039:
040: public class TestDeprecationPrinter extends TestCase {
041: private static final String NEW_CLASSPATH = "tests"
042: + File.separator + "JarJarDiff" + File.separator + "new";
043:
044: private ClassfileLoader loader;
045: private StringWriter writer;
046: private DeprecationPrinter printer;
047:
048: protected void setUp() throws Exception {
049: loader = new AggregatingClassfileLoader();
050: loader.load(NEW_CLASSPATH);
051:
052: writer = new StringWriter();
053: printer = new DeprecationPrinter(new PrintWriter(writer));
054: }
055:
056: public void testOneNonDeprecatedClass() {
057: loader.getClassfile("NewPackage.NewClass").accept(printer);
058:
059: assertEquals("No deprecation", "", writer.toString());
060: }
061:
062: public void testOneDeprecatedClass() throws IOException {
063: loader.getClassfile("ModifiedPackage.DeprecatedInterface")
064: .accept(printer);
065:
066: Collection entries = parse(writer.toString());
067:
068: assertTrue("Deprecated class", entries
069: .contains("ModifiedPackage.DeprecatedInterface"));
070: assertEquals("Deprecated class", 1, entries.size());
071: }
072:
073: public void testDeprecatedMethods() throws IOException {
074: loader.getClassfile("ModifiedPackage.ModifiedClass").accept(
075: printer);
076:
077: Collection entries = parse(writer.toString());
078:
079: assertTrue(
080: "Deprecated field",
081: entries
082: .contains("ModifiedPackage.ModifiedClass.deprecatedField"));
083: assertTrue(
084: "Deprecated constructor",
085: entries
086: .contains("ModifiedPackage.ModifiedClass.ModifiedClass(int)"));
087: assertTrue(
088: "Deprecated method",
089: entries
090: .contains("ModifiedPackage.ModifiedClass.deprecatedMethod()"));
091: assertEquals("Modified class", 3, entries.size());
092: }
093:
094: public void testListenerBehavior() throws IOException {
095: loader = new TransientClassfileLoader();
096: loader.addLoadListener(new LoadListenerVisitorAdapter(printer));
097: loader.load(NEW_CLASSPATH);
098:
099: Collection entries = parse(writer.toString());
100:
101: assertTrue("Deprecated class", entries
102: .contains("ModifiedPackage.DeprecatedClass"));
103: assertTrue("Deprecated interface", entries
104: .contains("ModifiedPackage.DeprecatedInterface"));
105: assertTrue(
106: "Deprecated field",
107: entries
108: .contains("ModifiedPackage.ModifiedClass.deprecatedField"));
109: assertTrue(
110: "Deprecated field",
111: entries
112: .contains("ModifiedPackage.ModifiedInterface.deprecatedField"));
113: assertTrue(
114: "Deprecated constructor",
115: entries
116: .contains("ModifiedPackage.DeprecatedClass.DeprecatedClass()"));
117: assertTrue(
118: "Deprecated constructor",
119: entries
120: .contains("ModifiedPackage.ModifiedClass.ModifiedClass(int)"));
121: assertTrue(
122: "Deprecated method",
123: entries
124: .contains("ModifiedPackage.ModifiedClass.deprecatedMethod()"));
125: assertTrue(
126: "Deprecated method",
127: entries
128: .contains("ModifiedPackage.ModifiedInterface.deprecatedMethod()"));
129: assertEquals("Classpath " + entries, 8, entries.size());
130: }
131:
132: private Collection parse(String text) throws IOException {
133: Collection result = new HashSet();
134:
135: BufferedReader in = new BufferedReader(new StringReader(text));
136: String line;
137: while ((line = in.readLine()) != null) {
138: result.add(line);
139: }
140: in.close();
141:
142: return result;
143: }
144: }
|