1 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 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 { 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 text) throws IOException { 100 Collection result = new HashSet(); 101 102 BufferedReader in = new BufferedReader(new StringReader(text)); 103 String line; 104 while ((line = in.readLine()) != null) { 105 result.add(line); 106 } 107 in.close(); 108 109 return result; 110 } 111 } 112 | Popular Tags |