KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > plugin > TestPluginSystem


1 /*
2  * Copyright (c) 2003 The Nutch Organization. All rights reserved. Use subject
3  * to the conditions in http://www.nutch.org/LICENSE.txt.
4  */

5 package net.nutch.plugin;
6 import java.io.File JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.FileWriter JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.LinkedList JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.net.URL JavaDoc;
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 /**
21  * Unit tests for the plugin system
22  *
23  * @author joa23
24  */

25 public class TestPluginSystem extends TestCase {
26   private int fPluginCount;
27   private LinkedList JavaDoc fFolders = new LinkedList JavaDoc();
28   
29   protected void setUp() throws Exception JavaDoc {
30     fPluginCount = 5;
31     createDummyPlugins(fPluginCount);
32   }
33   /*
34    * (non-Javadoc)
35    *
36    * @see junit.framework.TestCase#tearDown()
37    */

38   protected void tearDown() throws Exception JavaDoc {
39       for (int i = 0; i < fFolders.size(); i++) {
40         File JavaDoc folder = (File JavaDoc) fFolders.get(i);
41         delete(folder);
42     folder.delete();
43       }
44     
45   }
46   /**
47    * @throws IOException
48    */

49   public void testPluginConfiguration() throws IOException JavaDoc {
50     String JavaDoc string = getPluginFolder();
51     File JavaDoc file = new File JavaDoc(string);
52     if (!file.exists()) {
53       file.mkdir();
54     }
55     assertTrue(file.exists());
56   }
57   /**
58    * @throws IOException
59    */

60   public void testLoadPlugins() throws IOException JavaDoc {
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   /**
75    *
76    */

77   public void testGetExtensionAndAttributes() {
78     String JavaDoc 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 JavaDoc string = extension2.getAttribute(getGetConfigElementName());
88       assertEquals(string, getAttributeValue());
89     }
90   }
91   /**
92    * @throws PluginRuntimeException
93    */

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 JavaDoc object = extension.getExtensionInstance();
101       if (!(object instanceof HelloWorldExtension))
102         fail(" object is not a instance of HelloWorldExtension");
103       ((ITestExtension) object).testGetExtension("Bla ");
104       String JavaDoc string = ((ITestExtension) object).testGetExtension("Hello");
105       assertEquals("Hello World", string);
106     }
107   }
108   /**
109    *
110    *
111    */

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   /**
121    * @throws PluginRuntimeException
122    * @throws IOException
123    */

124   public void testGetResources() throws PluginRuntimeException, IOException JavaDoc {
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 JavaDoc 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   /**
141    * @return a PluginFolderPath
142    */

143   private String JavaDoc getPluginFolder() {
144     String JavaDoc[] strings = NutchConf.getStrings("plugin.folders");
145     if (strings == null || strings.length == 0)
146       fail("no plugin directory setuped..");
147
148     String JavaDoc name = strings[0];
149     return PluginManifestParser.getPluginFolder(name).toString();
150   }
151
152   /**
153    * Creates some Dummy Plugins
154    *
155    * @param pCount
156    */

157   private void createDummyPlugins(int pCount) {
158     String JavaDoc string = getPluginFolder();
159     try {
160       File JavaDoc folder = new File JavaDoc(string);
161       folder.mkdir();
162       for (int i = 0; i < pCount; i++) {
163         String JavaDoc pluginFolder = string + File.separator + "DummyPlugin"
164           + i;
165         File JavaDoc file = new File JavaDoc(pluginFolder);
166         file.mkdir();
167         fFolders.add(file);
168         createPluginManifest(i, file.getAbsolutePath());
169         createResourceFile(i, file.getAbsolutePath());
170       }
171     } catch (IOException JavaDoc e) {
172       e.printStackTrace();
173     }
174   }
175   /**
176    * Creates an ResourceFile
177    *
178    * @param i
179    * @param pFolderPath
180    * @throws FileNotFoundException
181    * @throws IOException
182    */

183   private void createResourceFile(int i, String JavaDoc pFolderPath)
184     throws FileNotFoundException JavaDoc, IOException JavaDoc {
185     Properties JavaDoc properties = new Properties JavaDoc();
186     properties.setProperty("key", "value");
187     properties.store(new FileOutputStream JavaDoc(pFolderPath + File.separator
188                                           + "messages" + ".properties"), "");
189   }
190   /**
191    * Deletes files in path
192    *
193    * @param path
194    * @throws IOException
195    */

196   private void delete(File JavaDoc path) throws IOException JavaDoc {
197     File JavaDoc[] 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   /**
205    * Creates an Plugin Manifest File
206    *
207    * @param i
208    * @param pFolderPath
209    * @throws IOException
210    */

211   private void createPluginManifest(int i, String JavaDoc pFolderPath)
212     throws IOException JavaDoc {
213     FileWriter JavaDoc out = new FileWriter JavaDoc(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 JavaDoc getAttributeValue() {
245     return "simple Parser Extension";
246   }
247   private static String JavaDoc getGetExtensionId() {
248     return "aExtensioID";
249   }
250   private static String JavaDoc getGetConfigElementName() {
251     return "name";
252   }
253 }
254
Popular Tags