Update of /cvsroot/nutch/playground/src/test/net/nutch/plugin
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10313/src/test/net/nutch/plugin
Added Files:
SimpleTestPlugin.java TestPluginSystem.java
ITestExtension.java HelloWorldExtension.java
Log Message:
intial commit
--- NEW FILE: SimpleTestPlugin.java ---
/* Copyright (c) 2003 The Nutch Organization. All rights reserved.
* Use subject to the conditions in http://www.nutch.org/LICENSE.txt.
*/
package net.nutch.plugin;
/**
* a simple test plugin
* @author joa23
*/
public class SimpleTestPlugin extends Plugin {
/**
* @param pDescriptor
*/
public SimpleTestPlugin(PluginDescriptor pDescriptor) {
super(pDescriptor);
}
/*
*
* @see net.nutch.plugin.Plugin#startUp()
*/
public void startUp() throws PluginRuntimeException {
System.err.println("start up Plugin: "+getDescriptor().getPluginId());
}
/* (non-Javadoc)
* @see net.nutch.plugin.Plugin#shutDown()
*/
public void shutDown() throws PluginRuntimeException {
System.err.println("shutdown Plugin: "+getDescriptor().getPluginId());
}
}
--- NEW FILE: TestPluginSystem.java ---
/*
* Copyright (c) 2003 The Nutch Organization. All rights reserved. Use subject
* to the conditions in http://www.nutch.org/LICENSE.txt.
*/
package net.nutch.plugin;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import junit.framework.TestCase;
import net.nutch.util.NutchConf;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
/**
* the main plugin architecture test. it tests the plugin model.
*
* @author joa23
*/
public class TestPluginSystem extends TestCase {
/*
* (non-Javadoc)
*
* @see java.lang.Object#finalize()
*/
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#setUp()
*/
private int fPluginCount;
protected void setUp() throws Exception {
fPluginCount = 5;
createDummyPlugins(fPluginCount);
}
/**
* Constructor for PluginConfigTest.
*
* @param arg0
*/
public TestPluginSystem(String arg0) {
super(arg0);
}
/**
* @throws IOException
*/
public void testPluginConfiguration() throws IOException {
String string = getPluginFolder();
File file = new File(string);
if (!file.exists());
file.mkdir();
assertTrue(file.exists());
}
/**
* @throws IOException
*/
public void testLoadPlugins() throws IOException {
PluginDescriptor[] descriptors =
PluginRepository.getInstance().getPluginDescriptors();
int k = descriptors.length;
assertEquals(fPluginCount, k);
for (int i = 0; i < descriptors.length; i++) {
PluginDescriptor descriptor = descriptors[i];
assertEquals(1, descriptor.getExportedLibUrls().length);
assertEquals(1, descriptor.getNotExportedLibUrls().length);
}
}
/**
*
*/
public void testGetExtensionAndAttributes() {
String xpId = " sdsdsd";
ExtensionPoint extensionPoint =
PluginRepository.getInstance().getExtensionPoint(xpId);
assertEquals(extensionPoint, null);
Extension[] extension1 =
PluginRepository
.getInstance()
.getExtensionPoint(getGetExtensionId())
.getExtentens();
assertEquals(extension1.length, fPluginCount);
for (int i = 0; i < extension1.length; i++) {
Extension extension2 = extension1[i];
String string =
extension2.getAttribute(getGetConfigElementName());
assertEquals(string, getAttributeValue());
}
}
/**
*
*/
public void testGetExtensionInstances() throws PluginRuntimeException {
Extension[] extension1 =
PluginRepository
.getInstance()
.getExtensionPoint(getGetExtensionId())
.getExtentens();
assertEquals(extension1.length, fPluginCount);
for (int i = 0; i < extension1.length; i++) {
Extension extension2 = extension1[i];
Object object = extension2.getExtensionInstance();
if (!(object instanceof HelloWorldExtension))
fail(" object is not a instance of TextExtension");
((ITestExtension) object).testGetExtension("Bla ");
String string = ((ITestExtension)
object).testGetExtension("Hello");
assertEquals("Hello World", string);
}
}
/**
*
*/
public void testGetClassLoader() {
PluginDescriptor[] descriptors =
PluginRepository.getInstance().getPluginDescriptors();
for (int i = 0; i < descriptors.length; i++) {
PluginDescriptor descriptor = descriptors[i];
descriptor.getClassLoader();
}
}
public void testGetResources() throws PluginRuntimeException, IOException {
PluginDescriptor[] descriptors =
PluginRepository.getInstance().getPluginDescriptors();
for (int i = 0; i < descriptors.length; i++) {
PluginDescriptor descriptor = descriptors[i];
String value = descriptor.getResourceString("key", Locale.UK);
assertEquals("value", value);
value =
descriptor.getResourceString("key",
Locale.TRADITIONAL_CHINESE);
assertEquals("value", value);
}
}
/**
* @return String
*/
private String getPluginFolder() {
String string = NutchConf.get("plugin.folder");
if (string == null)
fail("no plugin directory setuped..");
return string;
}
/**
* @param pCount
* @throws IOException
*/
private void createDummyPlugins(int pCount) {
String string = getPluginFolder();
try {
File folder = new File(string);
if (!folder.exists())
folder.mkdir();
delete(folder);
for (int i = 0; i < pCount; i++) {
String pluginFolder =
string + File.separator + "DummyPlugin" + i;
File file = new File(pluginFolder);
file.mkdir();
createPluginManifest(i, file.getAbsolutePath());
createResourceFile(i, file.getAbsolutePath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param pString
*/
private void createResourceFile(int i, String pFolderPath)
throws FileNotFoundException, IOException {
Properties properties = new Properties();
properties.setProperty("key", "value");
properties.store(
new FileOutputStream(
pFolderPath + File.separator + "messages" +
".properties"),
"");
}
/**
* @param path
* @throws IOException
*/
private void delete(File path) throws IOException {
File[] files = path.listFiles();
for (int i = 0; i < files.length; ++i) {
if (files[i].isDirectory())
delete(files[i]);
files[i].delete();
}
}
/**
* not 100 % complete implementation of elclipse manifest definition.
*
* @param pFile
*/
private void createPluginManifest(int i, String pFolderPath)
throws IOException {
FileWriter out =
new FileWriter(pFolderPath + File.separator + "plugin.xml");
Document document = DocumentFactory.getInstance().createDocument();
document.addComment("this is just a simple plugin for testing
issues.");
Element pluginElement =
document
.addElement("nutch-plugin")
.addAttribute("id", "net.nutch.plugin." + i)
.addAttribute(getGetConfigElementName(), "" + i)
.addAttribute("version", "1.0")
.addAttribute("provider-name", "joa23")
.addAttribute("class",
"net.nutch.plugin.SimpleTestPlugin");
pluginElement
.addElement("extension-point")
.addAttribute("id", getGetExtensionId())
.addAttribute(getGetConfigElementName(), getAttributeValue())
.addAttribute("schema", "schema/testExtensionPoint.exsd");
Element runtime = pluginElement.addElement("runtime");
Element element =
runtime.addElement("library").addAttribute(
"name",
"libs/exported.jar");
element.addElement("extport");
Element element1 =
runtime.addElement("library").addAttribute(
"name",
"libs/not_exported.jar");
Element extension =
pluginElement.addElement("extension").addAttribute(
"point",
getGetExtensionId());
extension
.addElement("extemsion-item")
.addAttribute("objectClass", "IInterfaceForExtensionPoint")
.addAttribute(getGetConfigElementName(), getAttributeValue())
.addAttribute("id", "aExtensionId.")
.addAttribute("class", "net.nutch.plugin.HelloWorldExtension");
document.write(out);
out.flush();
out.close();
}
private String getAttributeValue() {
return "simple Parser Extension";
}
private static String getGetExtensionId() {
return "aExtensioID";
}
private static String getGetConfigElementName() {
return "name";
}
}
--- NEW FILE: ITestExtension.java ---
/* Copyright (c) 2003 The Nutch Organization. All rights reserved.
* Use subject to the conditions in http://www.nutch.org/LICENSE.txt.
*/
package net.nutch.plugin;
/**
* A Simple test Extension Interface.
* @author joa23
*
*
*/
public interface ITestExtension {
public String testGetExtension(String hello);
}
--- NEW FILE: HelloWorldExtension.java ---
/* Copyright (c) 2003 The Nutch Organization. All rights reserved.
* Use subject to the conditions in http://www.nutch.org/LICENSE.txt.
*/
package net.nutch.plugin;
/**
* a simple test extensions
*
* @author joa23
*/
public class HelloWorldExtension implements ITestExtension {
/* (non-Javadoc)
* @see net.nutch.plugin.ITestExtension#testGetExtension(java.lang.String)
*/
public String testGetExtension(String hello) {
return hello + " World";
}
}
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Nutch-cvs mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nutch-cvs