From: <st...@us...> - 2009-03-17 14:37:40
|
Revision: 7482 http://smartfrog.svn.sourceforge.net/smartfrog/?rev=7482&view=rev Author: steve_l Date: 2009-03-17 14:37:30 +0000 (Tue, 17 Mar 2009) Log Message: ----------- SFOS-1146 Junut under Hadoop Added Paths: ----------- trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/ trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRException.java trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRUtils.java trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JunitMR.java trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/SingleTestRun.java trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/TestSummary.java Added: trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRException.java =================================================================== --- trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRException.java (rev 0) +++ trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRException.java 2009-03-17 14:37:30 UTC (rev 7482) @@ -0,0 +1,72 @@ +/* (C) Copyright 2009 Hewlett-Packard Development Company, LP + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org + +*/ +package org.smartfrog.services.hadoop.junitmr; + +/** + * Created 17-Mar-2009 14:11:27 + */ + +public class JUnitMRException extends RuntimeException { + + /** + * Constructs a new runtime exception with <code>null</code> as its detail message. The cause is not initialized, and + * may subsequently be initialized by a call to {@link #initCause}. + */ + public JUnitMRException() { + } + + /** + * Constructs a new runtime exception with the specified detail message. The cause is not initialized, and may + * subsequently be initialized by a call to {@link #initCause}. + * + * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} + * method. + */ + public JUnitMRException(String message) { + super(message); + } + + /** + * Constructs a new runtime exception with the specified detail message and cause. <p>Note that the detail message + * associated with <code>cause</code> is <i>not</i> automatically incorporated in this runtime exception's detail + * message. + * + * @param message the detail message (which is saved for later retrieval by the {@link #getMessage()} method). + * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> + * value is permitted, and indicates that the cause is nonexistent or unknown.) + * @since 1.4 + */ + public JUnitMRException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructs a new runtime exception with the specified cause and a detail message of <tt>(cause==null ? null : + * cause.toString())</tt> (which typically contains the class and detail message of <tt>cause</tt>). This constructor + * is useful for runtime exceptions that are little more than wrappers for other throwables. + * + * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> + * value is permitted, and indicates that the cause is nonexistent or unknown.) + * @since 1.4 + */ + public JUnitMRException(Throwable cause) { + super(cause); + } +} Added: trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRUtils.java =================================================================== --- trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRUtils.java (rev 0) +++ trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JUnitMRUtils.java 2009-03-17 14:37:30 UTC (rev 7482) @@ -0,0 +1,96 @@ +/* (C) Copyright 2009 Hewlett-Packard Development Company, LP + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org + +*/ +package org.smartfrog.services.hadoop.junitmr; + +import org.apache.hadoop.io.Text; +import org.smartfrog.sfcore.common.SmartFrogInitException; +import org.smartfrog.sfcore.common.SmartFrogResolutionException; + +import java.io.DataInput; +import java.io.IOException; +import java.io.DataOutput; +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; +import java.rmi.RemoteException; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Created 17-Mar-2009 13:59:23 + */ + +public class JUnitMRUtils { + /** + * {@value } + */ + private static final String SUITE_METHOD_NAME = "suite"; + + static String readText(DataInput in) throws IOException { + Text t = new Text(); + t.readFields(in); + String s = t.toString(); + return s; + } + + static void writeText(DataOutput out, String s) throws IOException { + Text t = new Text(s); + t.write(out); + } + + + /** + * get the tests from the class, either as a suite or as introspected tests. There is no verification here that a + * class is a test suite! + * + * @param clazz class with the test + * @return the test + * @throws JUnitMRException if the test suite setup failed + */ + public static Test extractTest(Class clazz) throws JUnitMRException { + //todo: verify that the class implements test or testsuite + try { + // check if there is a suite method + Method method = clazz.getMethod(SUITE_METHOD_NAME); + return (Test) method.invoke(null); + } catch (NoSuchMethodException e) { + //if not, assume that it is a testclass and do it that way + return new TestSuite(clazz); + } catch (IllegalAccessException e) { + throw new JUnitMRException("No access to the method " + SUITE_METHOD_NAME + + " in class " + clazz, e); + } catch (InvocationTargetException e) { + throw new JUnitMRException("Exception in " + SUITE_METHOD_NAME + + " in class " + clazz, e.getCause()); + } + } + + + /** + * load the test class, using the secure classloader framework. + * + * @param classname class + * @return the loaded class + */ + public static Class loadTestClass(String classname) + throws ClassNotFoundException { + return Thread.currentThread().getClass().getClassLoader().loadClass(classname); + } +} Added: trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JunitMR.java =================================================================== --- trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JunitMR.java (rev 0) +++ trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/JunitMR.java 2009-03-17 14:37:30 UTC (rev 7482) @@ -0,0 +1,94 @@ +/* (C) Copyright 2009 Hewlett-Packard Development Company, LP + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org + +*/ +package org.smartfrog.services.hadoop.junitmr; + +import org.apache.hadoop.io.Text; +import org.apache.hadoop.mapreduce.Mapper; +import org.apache.hadoop.mapreduce.Reducer; + +import java.io.IOException; + +import junit.framework.TestResult; +import junit.framework.Test; + +/** + * Created 17-Mar-2009 13:18:16 + */ + +public class JunitMR { + + + public static class JunitMap extends + Mapper<Text, Text, Text, SingleTestRun> { + + /** + * Called once for each key/value pair in the input split. Most applications should override this, but the default + * is the identity function. + */ + @Override + protected void map(Text key, Text value, + Context context) + throws IOException, InterruptedException { + super.map(key, value, context); + String test = value.toString(); + TestResult result = new TestResult(); + try { + Class testClass = JUnitMRUtils.loadTestClass(test); + Test testSuite = JUnitMRUtils.extractTest(testClass); + SingleTestRun run = new SingleTestRun(); + result.addListener(run); + testSuite.run(result); + context.write(key, run); + } catch (ClassNotFoundException e) { + throw new IOException("Could not load "+test,e); + } + + } + + } + + public static class JUnitReducer + extends Reducer<Text, SingleTestRun, Text, TestSummary> { + + public void reduce(Text key, Iterable<SingleTestRun> values, + Context context) + throws IOException, InterruptedException { + TestSummary summary = new TestSummary(); + summary.name = key.toString(); + for (SingleTestRun outcome : values) { + summary.attempts++; + if (outcome.succeeded) { + summary.successes++; + } else { + summary.failures++; + } + if (outcome.skipped) { + summary.skips++; + } + if (!outcome.text.isEmpty()) { + //do something with text here + } + } + //push out the summary + context.write(key, summary); + } + } + +} Added: trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/SingleTestRun.java =================================================================== --- trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/SingleTestRun.java (rev 0) +++ trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/SingleTestRun.java 2009-03-17 14:37:30 UTC (rev 7482) @@ -0,0 +1,99 @@ +/* (C) Copyright 2009 Hewlett-Packard Development Company, LP + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org + +*/ +package org.smartfrog.services.hadoop.junitmr; + +import org.apache.hadoop.io.Writable; +import junit.framework.TestListener; +import junit.framework.Test; +import junit.framework.AssertionFailedError; + +import java.io.IOException; +import java.io.DataOutput; +import java.io.DataInput; + +/** + * Result of a single test run, and the listener for the test run itself. + * Fields marked as transient are used during the test run, and not serialized + */ +public class SingleTestRun implements Writable, TestListener { + public boolean skipped; + public boolean succeeded; + public String text = ""; + + + public SingleTestRun() { + } + + /** + * Serialize the fields of this object to <code>out</code>. + * + * @param out <code>DataOuput</code> to serialize this object into. + * @throws IOException + */ + public void write(DataOutput out) throws IOException { + out.writeBoolean(skipped); + out.writeBoolean(succeeded); + JUnitMRUtils.writeText(out, text); + } + + /** + * Deserialize the fields of this object from <code>in</code>. + * + * <p>For efficiency, implementations should attempt to re-use storage in the existing object where possible.</p> + * + * @param in <code>DataInput</code> to deseriablize this object from. + * @throws IOException + */ + public void readFields(DataInput in) throws IOException { + skipped = in.readBoolean(); + succeeded = in.readBoolean(); + text = JUnitMRUtils.readText(in); + } + + /** + * An error occurred. + */ + public void addError(Test test, Throwable t) { + + } + + /** + * A failure occurred. + */ + public void addFailure(Test test, AssertionFailedError t) { + + } + + /** + * A test ended. + */ + public void endTest(Test test) { + + + } + + /** + * A test started. + */ + public void startTest(Test test) { + + } + +} Added: trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/TestSummary.java =================================================================== --- trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/TestSummary.java (rev 0) +++ trunk/core/components/hadoop/src/org/smartfrog/services/hadoop/junitmr/TestSummary.java 2009-03-17 14:37:30 UTC (rev 7482) @@ -0,0 +1,80 @@ +/* (C) Copyright 2009 Hewlett-Packard Development Company, LP + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org + +*/ +package org.smartfrog.services.hadoop.junitmr; + +import org.apache.hadoop.io.WritableComparable; + +import java.io.IOException; +import java.io.DataOutput; +import java.io.DataInput; + +/** + * Created 17-Mar-2009 14:00:23 +*/ +public class TestSummary implements WritableComparable<TestSummary> { + + public int attempts; + public int successes; + public int skips; + public int failures; + public String name; + + /** + * Serialize the fields of this object to <code>out</code>. + * + * @param out <code>DataOuput</code> to serialize this object into. + * @throws IOException + */ + public void write(DataOutput out) throws IOException { + out.writeInt(attempts); + out.writeInt(successes); + out.writeInt(skips); + out.writeInt(failures); + JUnitMRUtils.writeText(out, name); + } + + + /** + * Deserialize the fields of this object from <code>in</code>. + * + * <p>For efficiency, implementations should attempt to re-use storage in the existing object where possible.</p> + * + * @param in <code>DataInput</code> to deseriablize this object from. + * @throws IOException + */ + public void readFields(DataInput in) throws IOException { + attempts = in.readInt(); + successes = in.readInt(); + skips = in.readInt(); + failures = in.readInt(); + name = JUnitMRUtils.readText(in); + } + + + /** + * Compares this object with the specified object for order. Returns a negative integer, zero, or a positive + * integer as this object is less than, equal to, or greater than the specified object. + * + * we compare on name only + */ + public int compareTo(TestSummary that) { + return this.name.compareTo(that.name); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |