KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > TestDeprecationPrinter


1 /*
2  * Copyright (c) 2001-2005, 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;
34
35 import java.io.*;
36
37 import junit.framework.*;
38 import java.util.*;
39
40 public class TestDeprecationPrinter extends TestCase {
41     private static final String JavaDoc NEW_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "new";
42
43     private ClassfileLoader loader;
44     private StringWriter writer;
45     private DeprecationPrinter printer;
46     
47     protected void setUp() throws Exception JavaDoc {
48         loader = new AggregatingClassfileLoader();
49         loader.load(NEW_CLASSPATH);
50
51         writer = new StringWriter();
52         printer = new DeprecationPrinter(new PrintWriter(writer));
53     }
54     
55     public void testOneNonDeprecatedClass() {
56         loader.getClassfile("NewPackage.NewClass").accept(printer);
57
58         assertEquals("No deprecation", "", writer.toString());
59     }
60     
61     public void testOneDeprecatedClass() throws IOException {
62         loader.getClassfile("ModifiedPackage.DeprecatedInterface").accept(printer);
63
64         Collection entries = parse(writer.toString());
65         
66         assertTrue("Deprecated class", entries.contains("ModifiedPackage.DeprecatedInterface"));
67         assertEquals("Deprecated class", 1, entries.size());
68     }
69     
70     public void testDeprecatedMethods() throws IOException {
71         loader.getClassfile("ModifiedPackage.ModifiedClass").accept(printer);
72
73         Collection entries = parse(writer.toString());
74         
75         assertTrue("Deprecated field", entries.contains("ModifiedPackage.ModifiedClass.deprecatedField"));
76         assertTrue("Deprecated constructor", entries.contains("ModifiedPackage.ModifiedClass.ModifiedClass(int)"));
77         assertTrue("Deprecated method", entries.contains("ModifiedPackage.ModifiedClass.deprecatedMethod()"));
78         assertEquals("Modified class", 3, entries.size());
79     }
80     
81     public void testListenerBehavior() throws IOException {
82         loader = new TransientClassfileLoader();
83         loader.addLoadListener(new LoadListenerVisitorAdapter(printer));
84         loader.load(NEW_CLASSPATH);
85
86         Collection entries = parse(writer.toString());
87         
88         assertTrue("Deprecated class", entries.contains("ModifiedPackage.DeprecatedClass"));
89         assertTrue("Deprecated interface", entries.contains("ModifiedPackage.DeprecatedInterface"));
90         assertTrue("Deprecated field", entries.contains("ModifiedPackage.ModifiedClass.deprecatedField"));
91         assertTrue("Deprecated field", entries.contains("ModifiedPackage.ModifiedInterface.deprecatedField"));
92         assertTrue("Deprecated constructor", entries.contains("ModifiedPackage.DeprecatedClass.DeprecatedClass()"));
93         assertTrue("Deprecated constructor", entries.contains("ModifiedPackage.ModifiedClass.ModifiedClass(int)"));
94         assertTrue("Deprecated method", entries.contains("ModifiedPackage.ModifiedClass.deprecatedMethod()"));
95         assertTrue("Deprecated method", entries.contains("ModifiedPackage.ModifiedInterface.deprecatedMethod()"));
96         assertEquals("Classpath " + entries, 8, entries.size());
97     }
98
99     private Collection parse(String JavaDoc text) throws IOException {
100         Collection result = new HashSet();
101         
102         BufferedReader in = new BufferedReader(new StringReader(text));
103         String JavaDoc line;
104         while ((line = in.readLine()) != null) {
105             result.add(line);
106         }
107         in.close();
108
109         return result;
110     }
111 }
112
Popular Tags