PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/org/apache/nutch/plugin/TestPluginSystem.java

https://github.com/ufwebadmin/nutch
Java | 282 lines | 183 code | 25 blank | 74 comment | 17 complexity | 93152c7fd79eb8b200459f5a0aae3bc6 MD5 | raw file
  1. /*
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.nutch.plugin;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.FileOutputStream;
  22. import java.io.FileWriter;
  23. import java.io.IOException;
  24. import java.util.LinkedList;
  25. import java.util.Locale;
  26. import java.util.Properties;
  27. import junit.framework.TestCase;
  28. import org.apache.hadoop.conf.Configuration;
  29. import org.apache.nutch.util.NutchConfiguration;
  30. /**
  31. * Unit tests for the plugin system
  32. *
  33. * @author joa23
  34. */
  35. public class TestPluginSystem extends TestCase {
  36. private int fPluginCount;
  37. private LinkedList fFolders = new LinkedList();
  38. private Configuration conf ;
  39. private PluginRepository repository;
  40. protected void setUp() throws Exception {
  41. this.conf = NutchConfiguration.create();
  42. conf.set("plugin.includes", ".*");
  43. // String string = this.conf.get("plugin.includes", "");
  44. // conf.set("plugin.includes", string + "|Dummy*");
  45. fPluginCount = 5;
  46. createDummyPlugins(fPluginCount);
  47. this.repository = PluginRepository.get(conf);
  48. }
  49. /*
  50. * (non-Javadoc)
  51. *
  52. * @see junit.framework.TestCase#tearDown()
  53. */
  54. protected void tearDown() throws Exception {
  55. for (int i = 0; i < fFolders.size(); i++) {
  56. File folder = (File) fFolders.get(i);
  57. delete(folder);
  58. folder.delete();
  59. }
  60. }
  61. /**
  62. */
  63. public void testPluginConfiguration() {
  64. String string = getPluginFolder();
  65. File file = new File(string);
  66. if (!file.exists()) {
  67. file.mkdir();
  68. }
  69. assertTrue(file.exists());
  70. }
  71. /**
  72. */
  73. public void testLoadPlugins() {
  74. PluginDescriptor[] descriptors = repository
  75. .getPluginDescriptors();
  76. int k = descriptors.length;
  77. assertTrue(fPluginCount <= k);
  78. for (int i = 0; i < descriptors.length; i++) {
  79. PluginDescriptor descriptor = descriptors[i];
  80. if (!descriptor.getPluginId().startsWith("getPluginFolder()")) {
  81. continue;
  82. }
  83. assertEquals(1, descriptor.getExportedLibUrls().length);
  84. assertEquals(1, descriptor.getNotExportedLibUrls().length);
  85. }
  86. }
  87. /**
  88. *
  89. */
  90. public void testGetExtensionAndAttributes() {
  91. String xpId = " sdsdsd";
  92. ExtensionPoint extensionPoint =repository
  93. .getExtensionPoint(xpId);
  94. assertEquals(extensionPoint, null);
  95. Extension[] extension1 = repository
  96. .getExtensionPoint(getGetExtensionId()).getExtensions();
  97. assertEquals(extension1.length, fPluginCount);
  98. for (int i = 0; i < extension1.length; i++) {
  99. Extension extension2 = extension1[i];
  100. String string = extension2.getAttribute(getGetConfigElementName());
  101. assertEquals(string, getParameterValue());
  102. }
  103. }
  104. /**
  105. * @throws PluginRuntimeException
  106. */
  107. public void testGetExtensionInstances() throws PluginRuntimeException {
  108. Extension[] extensions = repository
  109. .getExtensionPoint(getGetExtensionId()).getExtensions();
  110. assertEquals(extensions.length, fPluginCount);
  111. for (int i = 0; i < extensions.length; i++) {
  112. Extension extension = extensions[i];
  113. Object object = extension.getExtensionInstance();
  114. if (!(object instanceof HelloWorldExtension))
  115. fail(" object is not a instance of HelloWorldExtension");
  116. ((ITestExtension) object).testGetExtension("Bla ");
  117. String string = ((ITestExtension) object).testGetExtension("Hello");
  118. assertEquals("Hello World", string);
  119. }
  120. }
  121. /**
  122. *
  123. *
  124. */
  125. public void testGetClassLoader() {
  126. PluginDescriptor[] descriptors = repository
  127. .getPluginDescriptors();
  128. for (int i = 0; i < descriptors.length; i++) {
  129. PluginDescriptor descriptor = descriptors[i];
  130. assertNotNull(descriptor.getClassLoader());
  131. }
  132. }
  133. /**
  134. * @throws IOException
  135. */
  136. public void testGetResources() throws IOException {
  137. PluginDescriptor[] descriptors = repository
  138. .getPluginDescriptors();
  139. for (int i = 0; i < descriptors.length; i++) {
  140. PluginDescriptor descriptor = descriptors[i];
  141. if (!descriptor.getPluginId().startsWith("getPluginFolder()")) {
  142. continue;
  143. }
  144. String value = descriptor.getResourceString("key", Locale.UK);
  145. assertEquals("value", value);
  146. value = descriptor.getResourceString("key",
  147. Locale.TRADITIONAL_CHINESE);
  148. assertEquals("value", value);
  149. }
  150. }
  151. /**
  152. * @return a PluginFolderPath
  153. */
  154. private String getPluginFolder() {
  155. String[] strings = conf.getStrings("plugin.folders");
  156. if (strings == null || strings.length == 0)
  157. fail("no plugin directory setuped..");
  158. String name = strings[0];
  159. return new PluginManifestParser(conf, this.repository).getPluginFolder(name).toString();
  160. }
  161. /**
  162. * Creates some Dummy Plugins
  163. *
  164. * @param pCount
  165. */
  166. private void createDummyPlugins(int pCount) {
  167. String string = getPluginFolder();
  168. try {
  169. File folder = new File(string);
  170. folder.mkdir();
  171. for (int i = 0; i < pCount; i++) {
  172. String pluginFolder = string + File.separator + "DummyPlugin"
  173. + i;
  174. File file = new File(pluginFolder);
  175. file.mkdir();
  176. fFolders.add(file);
  177. createPluginManifest(i, file.getAbsolutePath());
  178. createResourceFile(file.getAbsolutePath());
  179. }
  180. } catch (IOException e) {
  181. e.printStackTrace();
  182. }
  183. }
  184. /**
  185. * Creates an ResourceFile
  186. *
  187. * @param pFolderPath
  188. * @throws FileNotFoundException
  189. * @throws IOException
  190. */
  191. private void createResourceFile(String pFolderPath)
  192. throws FileNotFoundException, IOException {
  193. Properties properties = new Properties();
  194. properties.setProperty("key", "value");
  195. properties.store(new FileOutputStream(pFolderPath + File.separator
  196. + "messages" + ".properties"), "");
  197. }
  198. /**
  199. * Deletes files in path
  200. *
  201. * @param path
  202. * @throws IOException
  203. */
  204. private void delete(File path) throws IOException {
  205. File[] files = path.listFiles();
  206. for (int i = 0; i < files.length; ++i) {
  207. if (files[i].isDirectory())
  208. delete(files[i]);
  209. files[i].delete();
  210. }
  211. }
  212. /**
  213. * Creates an Plugin Manifest File
  214. *
  215. * @param i
  216. * @param pFolderPath
  217. * @throws IOException
  218. */
  219. private void createPluginManifest(int i, String pFolderPath)
  220. throws IOException {
  221. FileWriter out = new FileWriter(pFolderPath + File.separator
  222. + "plugin.xml");
  223. String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  224. + "<!--this is just a simple plugin for testing issues.-->"
  225. + "<plugin id=\"org.apache.nutch.plugin."
  226. + i
  227. + "\" name=\""
  228. + i
  229. + "\" version=\"1.0\" provider-name=\"joa23\" "
  230. + "class=\"org.apache.nutch.plugin.SimpleTestPlugin\">"
  231. + "<extension-point id=\"aExtensioID\" "
  232. + "name=\"simple Parser Extension\" "
  233. + "schema=\"schema/testExtensionPoint.exsd\"/>"
  234. + "<runtime><library name=\"libs/exported.jar\"><extport/></library>"
  235. + "<library name=\"libs/not_exported.jar\"/></runtime>"
  236. + "<extension point=\"aExtensioID\">"
  237. + "<implementation name=\"simple Parser Extension\" "
  238. + "id=\"aExtensionId.\" class=\"org.apache.nutch.plugin.HelloWorldExtension\">"
  239. + "<parameter name=\"dummy-name\" value=\"a simple param value\"/>"
  240. + "</implementation></extension></plugin>";
  241. out.write(xml);
  242. out.flush();
  243. out.close();
  244. }
  245. private String getParameterValue() {
  246. return "a simple param value";
  247. }
  248. private static String getGetExtensionId() {
  249. return "aExtensioID";
  250. }
  251. private static String getGetConfigElementName() {
  252. return "dummy-name";
  253. }
  254. public static void main(String[] args) throws IOException {
  255. new TestPluginSystem().createPluginManifest(1, "/");
  256. }
  257. }