1 5 package net.nutch.plugin; 6 import java.io.File ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.FileWriter ; 10 import java.io.IOException ; 11 import java.util.LinkedList ; 12 import java.util.Locale ; 13 import java.util.Properties ; 14 import java.net.URL ; 15 import junit.framework.TestCase; 16 import net.nutch.util.NutchConf; 17 import org.dom4j.Document; 18 import org.dom4j.DocumentFactory; 19 import org.dom4j.Element; 20 25 public class TestPluginSystem extends TestCase { 26 private int fPluginCount; 27 private LinkedList fFolders = new LinkedList (); 28 29 protected void setUp() throws Exception { 30 fPluginCount = 5; 31 createDummyPlugins(fPluginCount); 32 } 33 38 protected void tearDown() throws Exception { 39 for (int i = 0; i < fFolders.size(); i++) { 40 File folder = (File ) fFolders.get(i); 41 delete(folder); 42 folder.delete(); 43 } 44 45 } 46 49 public void testPluginConfiguration() throws IOException { 50 String string = getPluginFolder(); 51 File file = new File (string); 52 if (!file.exists()) { 53 file.mkdir(); 54 } 55 assertTrue(file.exists()); 56 } 57 60 public void testLoadPlugins() throws IOException { 61 PluginDescriptor[] descriptors = PluginRepository.getInstance() 62 .getPluginDescriptors(); 63 int k = descriptors.length; 64 assertTrue(fPluginCount <= k); 65 for (int i = 0; i < descriptors.length; i++) { 66 PluginDescriptor descriptor = descriptors[i]; 67 if (!descriptor.getPluginId().startsWith("getPluginFolder()")) { 68 continue; 69 } 70 assertEquals(1, descriptor.getExportedLibUrls().length); 71 assertEquals(1, descriptor.getNotExportedLibUrls().length); 72 } 73 } 74 77 public void testGetExtensionAndAttributes() { 78 String xpId = " sdsdsd"; 79 ExtensionPoint extensionPoint = PluginRepository.getInstance() 80 .getExtensionPoint(xpId); 81 assertEquals(extensionPoint, null); 82 Extension[] extension1 = PluginRepository.getInstance() 83 .getExtensionPoint(getGetExtensionId()).getExtentens(); 84 assertEquals(extension1.length, fPluginCount); 85 for (int i = 0; i < extension1.length; i++) { 86 Extension extension2 = extension1[i]; 87 String string = extension2.getAttribute(getGetConfigElementName()); 88 assertEquals(string, getAttributeValue()); 89 } 90 } 91 94 public void testGetExtensionInstances() throws PluginRuntimeException { 95 Extension[] extensions = PluginRepository.getInstance() 96 .getExtensionPoint(getGetExtensionId()).getExtentens(); 97 assertEquals(extensions.length, fPluginCount); 98 for (int i = 0; i < extensions.length; i++) { 99 Extension extension = extensions[i]; 100 Object object = extension.getExtensionInstance(); 101 if (!(object instanceof HelloWorldExtension)) 102 fail(" object is not a instance of HelloWorldExtension"); 103 ((ITestExtension) object).testGetExtension("Bla "); 104 String string = ((ITestExtension) object).testGetExtension("Hello"); 105 assertEquals("Hello World", string); 106 } 107 } 108 112 public void testGetClassLoader() { 113 PluginDescriptor[] descriptors = PluginRepository.getInstance() 114 .getPluginDescriptors(); 115 for (int i = 0; i < descriptors.length; i++) { 116 PluginDescriptor descriptor = descriptors[i]; 117 assertNotNull(descriptor.getClassLoader()); 118 } 119 } 120 124 public void testGetResources() throws PluginRuntimeException, IOException { 125 PluginDescriptor[] descriptors = PluginRepository.getInstance() 126 .getPluginDescriptors(); 127 for (int i = 0; i < descriptors.length; i++) { 128 PluginDescriptor descriptor = descriptors[i]; 129 if (!descriptor.getPluginId().startsWith("getPluginFolder()")) { 130 continue; 131 } 132 String value = descriptor.getResourceString("key", Locale.UK); 133 assertEquals("value", value); 134 value = descriptor.getResourceString("key", 135 Locale.TRADITIONAL_CHINESE); 136 assertEquals("value", value); 137 138 } 139 } 140 143 private String getPluginFolder() { 144 String [] strings = NutchConf.getStrings("plugin.folders"); 145 if (strings == null || strings.length == 0) 146 fail("no plugin directory setuped.."); 147 148 String name = strings[0]; 149 return PluginManifestParser.getPluginFolder(name).toString(); 150 } 151 152 157 private void createDummyPlugins(int pCount) { 158 String string = getPluginFolder(); 159 try { 160 File folder = new File (string); 161 folder.mkdir(); 162 for (int i = 0; i < pCount; i++) { 163 String pluginFolder = string + File.separator + "DummyPlugin" 164 + i; 165 File file = new File (pluginFolder); 166 file.mkdir(); 167 fFolders.add(file); 168 createPluginManifest(i, file.getAbsolutePath()); 169 createResourceFile(i, file.getAbsolutePath()); 170 } 171 } catch (IOException e) { 172 e.printStackTrace(); 173 } 174 } 175 183 private void createResourceFile(int i, String pFolderPath) 184 throws FileNotFoundException , IOException { 185 Properties properties = new Properties (); 186 properties.setProperty("key", "value"); 187 properties.store(new FileOutputStream (pFolderPath + File.separator 188 + "messages" + ".properties"), ""); 189 } 190 196 private void delete(File path) throws IOException { 197 File [] files = path.listFiles(); 198 for (int i = 0; i < files.length; ++i) { 199 if (files[i].isDirectory()) 200 delete(files[i]); 201 files[i].delete(); 202 } 203 } 204 211 private void createPluginManifest(int i, String pFolderPath) 212 throws IOException { 213 FileWriter out = new FileWriter (pFolderPath + File.separator 214 + "plugin.xml"); 215 Document document = DocumentFactory.getInstance().createDocument(); 216 document.addComment("this is just a simple plugin for testing issues."); 217 Element pluginElement = document.addElement("nutch-plugin") 218 .addAttribute("id", "net.nutch.plugin." + i).addAttribute( 219 getGetConfigElementName(), "" + i).addAttribute( 220 "version", "1.0") 221 .addAttribute("provider-name", "joa23").addAttribute("class", 222 "net.nutch.plugin.SimpleTestPlugin"); 223 pluginElement.addElement("extension-point").addAttribute("id", 224 getGetExtensionId()).addAttribute(getGetConfigElementName(), 225 getAttributeValue()).addAttribute("schema", 226 "schema/testExtensionPoint.exsd"); 227 Element runtime = pluginElement.addElement("runtime"); 228 Element element = runtime.addElement("library").addAttribute("name", 229 "libs/exported.jar"); 230 element.addElement("extport"); 231 Element element1 = runtime.addElement("library").addAttribute("name", 232 "libs/not_exported.jar"); 233 Element extension = pluginElement.addElement("extension").addAttribute( 234 "point", getGetExtensionId()); 235 extension.addElement("extemsion-item").addAttribute("objectClass", 236 "IInterfaceForExtensionPoint").addAttribute( 237 getGetConfigElementName(), getAttributeValue()).addAttribute( 238 "id", "aExtensionId.").addAttribute("class", 239 "net.nutch.plugin.HelloWorldExtension"); 240 document.write(out); 241 out.flush(); 242 out.close(); 243 } 244 private String getAttributeValue() { 245 return "simple Parser Extension"; 246 } 247 private static String getGetExtensionId() { 248 return "aExtensioID"; 249 } 250 private static String getGetConfigElementName() { 251 return "name"; 252 } 253 } 254 | Popular Tags |