- 论坛徽章:
- 0
|
最近在学习contributing to eclipse,自己的java能力还是比较差,搞了整整2周才能做完第一个loop的过程,在网络上还是没有源码找了很久都没有找到。现在把我的源码先贴出来看看对大家是否有帮助吧。
plugin.xml
E:\eclipse\workspace\org.hometech.junit\src\org\hometech\junit
DoAction.java
package org.hometech.junit;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
public class DoAction implements IObjectActionDelegate {
ISelection selection;
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
public boolean equals(Object arg0) {
// TODO Auto-generated method stub
return super.equals(arg0);
}
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
}
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
// TODO Auto-generated method stub
}
public void run(IAction action) {
// TODO Auto-generated method stub
if(! (selection instanceof IStructuredSelection))
return;
IStructuredSelection structured = (IStructuredSelection)selection;
IType type = (IType) structured.getFirstElement();
MessageDialog.openInformation(null,type.getElementName(),type.getFullyQualifiedName());
ITestRunListener listener = new Listener();
JunitPlugin.getPlugin().addTestListener(listener);
JunitPlugin.getPlugin().run(type);
JunitPlugin.getPlugin().removeTestListener(listener);
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO Auto-generated method stub
this.selection = selection;
}
public static class Listener implements ITestRunListener {
private boolean passed = true;
public Listener() {
System.out.println("Listener create now");
}
public void testsStarted(int testCount) {
}
public void testsFinished() {
String message = passed ? "Pass":"Fail";
MessageDialog.openInformation(null,"Test Results",message);
}
public void testStarted(String klass,String method) {
}
public void testFailed(String klass,String method,String trace){
passed = false;
}
}
}
ITestRunListener.java
package org.hometech.junit;
public interface ITestRunListener {
void testsStarted(int testCount);
void testsFinished();
void testStarted(String klass,String method);
void testFailed(String klass,String method,String trace);
}
JunitPlugin.java
package org.hometech.junit;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.jdt.core.IType;
public class JunitPlugin extends Plugin {
private static JunitPlugin instance;
public JunitPlugin() {
super();
// TODO Auto-generated constructor stub
System.out.println("load default JunitPlugin!");
instance = this;
}
public JunitPlugin(IPluginDescriptor descriptor) {
super(descriptor);
// TODO Auto-generated constructor stub
System.out.println("load IPluginDescriptor JunitPlugin!");
instance = this;
}
public static JunitPlugin getPlugin() {
return instance;
}
public void run(IType type) {
System.out.println("run JunitPlugin!");
try {
new TestRunner().run(type);
}
catch(CoreException e) {
e.printStackTrace();
}
}
private List listeners = new ArrayList();
public void addTestListener(ITestRunListener listener) {
System.out.println("add Test Listener now");
listeners.add(listener);
}
public void removeTestListener(ITestRunListener listener) {
System.out.println("remove Test Listener now");
listeners.remove(listener);
}
public List getListeners() {
return listeners;
}
public void fireTestsStarted(int count) {
for (Iterator all = getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testsStarted(count);
}
}
public void fireTestsFinished() {
for (Iterator all = getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testsFinished();
}
}
public void fireTestStarted(String klass, String methodName) {
for (Iterator all = getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testStarted(klass, methodName);
}
}
public void fireTestFailed(String klass, String method, String trace) {
for (Iterator all = getListeners().iterator(); all.hasNext();) {
ITestRunListener each = (ITestRunListener) all.next();
each.testFailed(klass, method, trace);
}
}
}
SocketTestRunner.java
package org.hometech.junit;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
import junit.framework.TestResult;
import junit.framework.TestSuite;
public class SocketTestRunner implements TestListener {
private int port;
private Socket socket;
private PrintWriter writer;
/**
* The entry point for the test runner. The arguments are: args[0]: the port
* number to connect to args[1-n]: the name of test classes
*/
public static void main(String[] args) {
System.out.println("in socket test runner");
new SocketTestRunner().runTests(args);
}
private void runTests(String[] args) {
port = Integer.parseInt(args[0]);
openClientSocket();
try {
TestSuite suite = new TestSuite();
for (int i = 1; i
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/41189/showart_324337.html |
|