1 32 33 package com.jeantessier.dependencyfinder.cli; 34 35 import java.io.*; 36 import java.util.*; 37 38 import junit.framework.*; 39 40 import com.jeantessier.classreader.*; 41 42 public class TestClassMatcher extends TestCase { 43 public static final String TEST_CLASS = "test"; 44 public static final String TEST_FILENAME = "classes" + File.separator + "test.class"; 45 public static final String TEST_DIR = "tests" + File.separator + "JarJarDiff"; 46 47 public void testMatchNone() { 48 ClassMatcher matcher = new ClassMatcher(Collections.EMPTY_LIST, Collections.EMPTY_LIST); 49 50 ClassfileLoader loader = new TransientClassfileLoader(); 51 loader.addLoadListener(matcher); 52 loader.load(Collections.singleton(TEST_FILENAME)); 53 54 assertEquals("Number of results", 0, matcher.getResults().size()); 55 } 56 57 public void testMatchClassfile() { 58 ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST); 59 60 ClassfileLoader loader = new TransientClassfileLoader(); 61 loader.addLoadListener(matcher); 62 loader.load(Collections.singleton(TEST_FILENAME)); 63 64 assertEquals("Number of results", 1, matcher.getResults().size()); 65 assertEquals("key", TEST_CLASS, matcher.getResults().keySet().iterator().next()); 66 Iterator i = matcher.getResults().values().iterator(); 67 while (i.hasNext()) { 68 List results = (List) i.next(); 69 assertEquals("number results", 1, results.size()); 70 assertEquals("value", TEST_FILENAME, results.get(0)); 71 } 72 } 73 74 public void testOneLevelJar() { 75 String filename = TEST_DIR + File.separator + "onelevel.jar"; 76 assertTrue(filename + " missing", new File(filename).exists()); 77 78 ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST); 79 80 ClassfileLoader loader = new TransientClassfileLoader(); 81 loader.addLoadListener(matcher); 82 loader.load(Collections.singleton(filename)); 83 84 assertEquals("Number of results", 17, matcher.getResults().size()); 85 Iterator i = matcher.getResults().values().iterator(); 86 while (i.hasNext()) { 87 List results = (List) i.next(); 88 assertEquals("number results", 1, results.size()); 89 assertEquals("value", filename, results.get(0)); 90 } 91 } 92 93 public void testTwoLevelJar() { 94 String filename = TEST_DIR + File.separator + "twolevel.jar"; 95 assertTrue(filename + " missing", new File(filename).exists()); 96 97 ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST); 98 99 ClassfileLoader loader = new TransientClassfileLoader(); 100 loader.addLoadListener(matcher); 101 loader.load(Collections.singleton(filename)); 102 103 assertEquals("Number of results", 17, matcher.getResults().size()); 104 Iterator i = matcher.getResults().values().iterator(); 105 while (i.hasNext()) { 106 List results = (List) i.next(); 107 assertEquals("number results", 1, results.size()); 108 assertEquals("value", "onelevel.zip", results.get(0)); 109 } 110 } 111 112 public void testMatchDirectory() { 113 String dirname = TEST_DIR + File.separator + "new"; 114 assertTrue(dirname + " missing", new File(dirname).exists()); 115 116 ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST); 117 118 ClassfileLoader loader = new TransientClassfileLoader(); 119 loader.addLoadListener(matcher); 120 loader.load(Collections.singleton(dirname)); 121 122 assertEquals("Number of results", 17, matcher.getResults().size()); 123 Iterator i = matcher.getResults().values().iterator(); 124 while (i.hasNext()) { 125 List results = (List) i.next(); 126 assertEquals("number results", 1, results.size()); 127 assertEquals("value", dirname, results.get(0)); 128 } 129 } 130 131 public void testIncludes() { 132 String dirname = TEST_DIR + File.separator + "new"; 133 assertTrue(dirname + " missing", new File(dirname).exists()); 134 135 ClassMatcher matcher = new ClassMatcher(Collections.singletonList("/modified/i"), Collections.EMPTY_LIST); 136 137 ClassfileLoader loader = new TransientClassfileLoader(); 138 loader.addLoadListener(matcher); 139 loader.load(Collections.singleton(dirname)); 140 141 assertEquals("Number of results", 12, matcher.getResults().size()); 142 Iterator i = matcher.getResults().values().iterator(); 143 while (i.hasNext()) { 144 List results = (List) i.next(); 145 assertEquals("number results", 1, results.size()); 146 assertEquals("value", dirname, results.get(0)); 147 } 148 } 149 150 public void testExcludes() { 151 String dirname = TEST_DIR + File.separator + "new"; 152 assertTrue(dirname + " missing", new File(dirname).exists()); 153 154 ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.singletonList("/modified/i")); 155 156 ClassfileLoader loader = new TransientClassfileLoader(); 157 loader.addLoadListener(matcher); 158 loader.load(Collections.singleton(dirname)); 159 160 assertEquals("Number of results", 5, matcher.getResults().size()); 161 Iterator i = matcher.getResults().values().iterator(); 162 while (i.hasNext()) { 163 List results = (List) i.next(); 164 assertEquals("number results", 1, results.size()); 165 assertEquals("value", dirname, results.get(0)); 166 } 167 } 168 169 public void testMultiples() { 170 String filename1 = TEST_DIR + File.separator + "onelevel.zip"; 171 String filename2 = TEST_DIR + File.separator + "onelevel.jar"; 172 assertTrue(filename1 + " missing", new File(filename1).exists()); 173 assertTrue(filename2 + " missing", new File(filename2).exists()); 174 175 Collection filenames = new ArrayList(); 176 filenames.add(filename1); 177 filenames.add(filename2); 178 179 ClassMatcher matcher = new ClassMatcher(Collections.singletonList("//"), Collections.EMPTY_LIST); 180 181 ClassfileLoader loader = new TransientClassfileLoader(); 182 loader.addLoadListener(matcher); 183 loader.load(filenames); 184 185 assertEquals("Number of results", 17, matcher.getResults().size()); 186 Iterator i = matcher.getResults().values().iterator(); 187 while (i.hasNext()) { 188 List results = (List) i.next(); 189 assertEquals("number results", 2, results.size()); 190 assertEquals("value", filename1, results.get(0)); 191 assertEquals("value", filename2, results.get(1)); 192 } 193 } 194 } 195 | Popular Tags |