Best junit code snippet using junit.framework.Interface Test
Source:BoundedStackInterface_JML_Test.java  
1// This file was generated by jmlunit on Mon Mar 16 13:17:07 EDT 2009.2package org.jmlspecs.samples.stacks;3/** Automatically-generated test driver for JML and JUnit based4 * testing of BoundedStackInterface. The superclass of this class should be edited5 * to supply test data. However it's best not to edit this class6 * directly; instead use the command7 * <pre>8 *  jmlunit BoundedStackInterface.java9 * </pre>10 * to regenerate this class whenever BoundedStackInterface.java changes.11 */12public class BoundedStackInterface_JML_Test13     extends BoundedStackInterface_JML_TestData14{15    /** Initialize this class. */16    public BoundedStackInterface_JML_Test(java.lang.String name) {17        super(name);18    }19    /** Run the tests. */20    public static void main(java.lang.String[] args) {21        org.jmlspecs.jmlunit.JMLTestRunner.run(suite());22        // You can also use a JUnit test runner such as:23        // junit.textui.TestRunner.run(suite());24    }25    /** Test to see if the code for interface BoundedStackInterface26     * has been compiled with runtime assertion checking (i.e., by jmlc).27     * Code that is not compiled with jmlc would not make an effective test,28     * since no assertion checking would be done. */29    public void test$IsRACCompiled() {30        junit.framework.Assert.assertTrue("code for interface BoundedStackInterface"31                + " was not compiled with jmlc"32                + " so no assertions will be checked!",33            org.jmlspecs.jmlrac.runtime.JMLChecker.isRACCompiled(BoundedStackInterface.class)34            );35    }36    /** Return the test suite for this test class.  This will have37    * added to it at least test$IsRACCompiled(), and any test methods38    * written explicitly by the user in the superclass.  It will also39    * have added test suites for each testing each method and40    * constructor.41    */42    //@ ensures \result != null;43    public static junit.framework.Test suite() {44        BoundedStackInterface_JML_Test testobj45            = new BoundedStackInterface_JML_Test("BoundedStackInterface_JML_Test");46        junit.framework.TestSuite testsuite = testobj.overallTestSuite();47        // Add instances of Test found by the reflection mechanism.48        testsuite.addTestSuite(BoundedStackInterface_JML_Test.class);49        testobj.addTestSuiteForEachMethod(testsuite);50        return testsuite;51    }52    /** A JUnit test object that can run a single test method.  This53     * is defined as a nested class solely for convenience; it can't54     * be defined once and for all because it must subclass its55     * enclosing class.56     */57    protected static abstract class OneTest extends BoundedStackInterface_JML_Test {58        /** Initialize this test object. */59        public OneTest(String name) {60            super(name);61        }62        /** The result object that holds information about testing. */63        protected junit.framework.TestResult result;64        //@ also65        //@ requires result != null;66        public void run(junit.framework.TestResult result) {67            this.result = result;68            super.run(result);69        }70        /* Run a single test and decide whether the test was71         * successful, meaningless, or a failure.  This is the72         * Template Method pattern abstraction of the inner loop in a73         * JML/JUnit test. */74        public void runTest() throws java.lang.Throwable {75            try {76                // The call being tested!77                doCall();78            }79            catch (org.jmlspecs.jmlrac.runtime.JMLEntryPreconditionError e) {80                // meaningless test input81                addMeaningless();82            } catch (org.jmlspecs.jmlrac.runtime.JMLAssertionError e) {83                // test failure84                int l = org.jmlspecs.jmlrac.runtime.JMLChecker.getLevel();85                org.jmlspecs.jmlrac.runtime.JMLChecker.setLevel86                    (org.jmlspecs.jmlrac.runtime.JMLOption.NONE);87                try {88                    java.lang.String failmsg = this.failMessage(e);89                    junit.framework.AssertionFailedError err90                        = new junit.framework.AssertionFailedError(failmsg);91                    err.setStackTrace(new java.lang.StackTraceElement[]{});92                    err.initCause(e);93                    result.addFailure(this, err);94                } finally {95                    org.jmlspecs.jmlrac.runtime.JMLChecker.setLevel(l);96                }97            } catch (java.lang.Throwable e) {98                // test success99            }100        }101        /** Call the method to be tested with the appropriate arguments. */102        protected abstract void doCall() throws java.lang.Throwable;103        /** Format the error message for a test failure, based on the104         * method's arguments. */105        protected abstract java.lang.String failMessage106            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e);107        /** Inform listeners that a meaningless test was run. */108        private void addMeaningless() {109            if (result instanceof org.jmlspecs.jmlunit.JMLTestResult) {110                ((org.jmlspecs.jmlunit.JMLTestResult)result)111                    .addMeaningless(this);112            }113        }114    }115    /** Create the tests that are to be run for testing the class116     * BoundedStackInterface.  The framework will then run them.117     * @param overallTestSuite$ The suite accumulating all of the tests118     * for this driver class.119     */120    //@ requires overallTestSuite$ != null;121    public void addTestSuiteForEachMethod122        (junit.framework.TestSuite overallTestSuite$)123    {124        try {125            this.addTestSuiteFor$TestPop(overallTestSuite$);126        } catch (java.lang.Throwable ex) {127            overallTestSuite$.addTest128                (new org.jmlspecs.jmlunit.strategies.ConstructorFailed(ex));129        }130        try {131            this.addTestSuiteFor$TestPush(overallTestSuite$);132        } catch (java.lang.Throwable ex) {133            overallTestSuite$.addTest134                (new org.jmlspecs.jmlunit.strategies.ConstructorFailed(ex));135        }136        try {137            this.addTestSuiteFor$TestTop(overallTestSuite$);138        } catch (java.lang.Throwable ex) {139            overallTestSuite$.addTest140                (new org.jmlspecs.jmlunit.strategies.ConstructorFailed(ex));141        }142        try {143            this.addTestSuiteFor$TestGetSizeLimit(overallTestSuite$);144        } catch (java.lang.Throwable ex) {145            overallTestSuite$.addTest146                (new org.jmlspecs.jmlunit.strategies.ConstructorFailed(ex));147        }148        try {149            this.addTestSuiteFor$TestIsEmpty(overallTestSuite$);150        } catch (java.lang.Throwable ex) {151            overallTestSuite$.addTest152                (new org.jmlspecs.jmlunit.strategies.ConstructorFailed(ex));153        }154        try {155            this.addTestSuiteFor$TestIsFull(overallTestSuite$);156        } catch (java.lang.Throwable ex) {157            overallTestSuite$.addTest158                (new org.jmlspecs.jmlunit.strategies.ConstructorFailed(ex));159        }160        try {161            this.addTestSuiteFor$TestClone(overallTestSuite$);162        } catch (java.lang.Throwable ex) {163            overallTestSuite$.addTest164                (new org.jmlspecs.jmlunit.strategies.ConstructorFailed(ex));165        }166    }167    /** Add tests for the pop method168     * to the overall test suite. */169    private void addTestSuiteFor$TestPop170        (junit.framework.TestSuite overallTestSuite$)171    {172        junit.framework.TestSuite methodTests$173            = this.emptyTestSuiteFor("pop");174        try {175            org.jmlspecs.jmlunit.strategies.IndefiniteIterator176                receivers$iter177                = new org.jmlspecs.jmlunit.strategies.NonNullIteratorDecorator178                    (this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter("pop", 0));179            this.check_has_data180                (receivers$iter,181                 "new NonNullIteratorDecorator(this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter(\"pop\", 0))");182            while (!receivers$iter.atEnd()) {183                final org.jmlspecs.samples.stacks.BoundedStackInterface receiver$184                    = (org.jmlspecs.samples.stacks.BoundedStackInterface) receivers$iter.get();185                methodTests$.addTest186                    (new TestPop(receiver$));187                receivers$iter.advance();188            }189        } catch (org.jmlspecs.jmlunit.strategies.TestSuiteFullException e$) {190            // methodTests$ doesn't want more tests191        }192        overallTestSuite$.addTest(methodTests$);193    }194    /** Test for the pop method. */195    protected static class TestPop extends OneTest {196        /** The receiver */197        private org.jmlspecs.samples.stacks.BoundedStackInterface receiver$;198        /** Initialize this instance. */199        public TestPop(org.jmlspecs.samples.stacks.BoundedStackInterface receiver$) {200            super("pop");201            this.receiver$ = receiver$;202        }203        protected void doCall() throws java.lang.Throwable {204            receiver$.pop();205        }206        protected java.lang.String failMessage207            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e$)208        {209            java.lang.String msg = "\n\tMethod 'pop' applied to";210            msg += "\n\tReceiver: " + this.receiver$;211            return msg;212        }213    }214    /** Add tests for the push method215     * to the overall test suite. */216    private void addTestSuiteFor$TestPush217        (junit.framework.TestSuite overallTestSuite$)218    {219        junit.framework.TestSuite methodTests$220            = this.emptyTestSuiteFor("push");221        try {222            org.jmlspecs.jmlunit.strategies.IndefiniteIterator223                receivers$iter224                = new org.jmlspecs.jmlunit.strategies.NonNullIteratorDecorator225                    (this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter("push", 1));226            this.check_has_data227                (receivers$iter,228                 "new NonNullIteratorDecorator(this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter(\"push\", 1))");229            while (!receivers$iter.atEnd()) {230                org.jmlspecs.jmlunit.strategies.IndefiniteIterator231                    vjava_lang_Object$1$iter232                    = this.vjava_lang_ObjectIter("push", 0);233                this.check_has_data234                    (vjava_lang_Object$1$iter,235                     "this.vjava_lang_ObjectIter(\"push\", 0)");236                while (!vjava_lang_Object$1$iter.atEnd()) {237                    final org.jmlspecs.samples.stacks.BoundedStackInterface receiver$238                        = (org.jmlspecs.samples.stacks.BoundedStackInterface) receivers$iter.get();239                    final java.lang.Object x240                        = (java.lang.Object) vjava_lang_Object$1$iter.get();241                    methodTests$.addTest242                        (new TestPush(receiver$, x));243                    vjava_lang_Object$1$iter.advance();244                }245                receivers$iter.advance();246            }247        } catch (org.jmlspecs.jmlunit.strategies.TestSuiteFullException e$) {248            // methodTests$ doesn't want more tests249        }250        overallTestSuite$.addTest(methodTests$);251    }252    /** Test for the push method. */253    protected static class TestPush extends OneTest {254        /** The receiver */255        private org.jmlspecs.samples.stacks.BoundedStackInterface receiver$;256        /** Argument x */257        private java.lang.Object x;258        /** Initialize this instance. */259        public TestPush(org.jmlspecs.samples.stacks.BoundedStackInterface receiver$, java.lang.Object x) {260            super("push"+ ":" + (x==null? "null" :"{java.lang.Object}"));261            this.receiver$ = receiver$;262            this.x = x;263        }264        protected void doCall() throws java.lang.Throwable {265            receiver$.push(x);266        }267        protected java.lang.String failMessage268            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e$)269        {270            java.lang.String msg = "\n\tMethod 'push' applied to";271            msg += "\n\tReceiver: " + this.receiver$;272            msg += "\n\tArgument x: " + this.x;273            return msg;274        }275    }276    /** Add tests for the top method277     * to the overall test suite. */278    private void addTestSuiteFor$TestTop279        (junit.framework.TestSuite overallTestSuite$)280    {281        junit.framework.TestSuite methodTests$282            = this.emptyTestSuiteFor("top");283        try {284            org.jmlspecs.jmlunit.strategies.IndefiniteIterator285                receivers$iter286                = new org.jmlspecs.jmlunit.strategies.NonNullIteratorDecorator287                    (this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter("top", 0));288            this.check_has_data289                (receivers$iter,290                 "new NonNullIteratorDecorator(this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter(\"top\", 0))");291            while (!receivers$iter.atEnd()) {292                final org.jmlspecs.samples.stacks.BoundedStackInterface receiver$293                    = (org.jmlspecs.samples.stacks.BoundedStackInterface) receivers$iter.get();294                methodTests$.addTest295                    (new TestTop(receiver$));296                receivers$iter.advance();297            }298        } catch (org.jmlspecs.jmlunit.strategies.TestSuiteFullException e$) {299            // methodTests$ doesn't want more tests300        }301        overallTestSuite$.addTest(methodTests$);302    }303    /** Test for the top method. */304    protected static class TestTop extends OneTest {305        /** The receiver */306        private org.jmlspecs.samples.stacks.BoundedStackInterface receiver$;307        /** Initialize this instance. */308        public TestTop(org.jmlspecs.samples.stacks.BoundedStackInterface receiver$) {309            super("top");310            this.receiver$ = receiver$;311        }312        protected void doCall() throws java.lang.Throwable {313            receiver$.top();314        }315        protected java.lang.String failMessage316            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e$)317        {318            java.lang.String msg = "\n\tMethod 'top' applied to";319            msg += "\n\tReceiver: " + this.receiver$;320            return msg;321        }322    }323    /** Add tests for the getSizeLimit method324     * to the overall test suite. */325    private void addTestSuiteFor$TestGetSizeLimit326        (junit.framework.TestSuite overallTestSuite$)327    {328        junit.framework.TestSuite methodTests$329            = this.emptyTestSuiteFor("getSizeLimit");330        try {331            org.jmlspecs.jmlunit.strategies.IndefiniteIterator332                receivers$iter333                = new org.jmlspecs.jmlunit.strategies.NonNullIteratorDecorator334                    (this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter("getSizeLimit", 0));335            this.check_has_data336                (receivers$iter,337                 "new NonNullIteratorDecorator(this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter(\"getSizeLimit\", 0))");338            while (!receivers$iter.atEnd()) {339                final org.jmlspecs.samples.stacks.BoundedStackInterface receiver$340                    = (org.jmlspecs.samples.stacks.BoundedStackInterface) receivers$iter.get();341                methodTests$.addTest342                    (new TestGetSizeLimit(receiver$));343                receivers$iter.advance();344            }345        } catch (org.jmlspecs.jmlunit.strategies.TestSuiteFullException e$) {346            // methodTests$ doesn't want more tests347        }348        overallTestSuite$.addTest(methodTests$);349    }350    /** Test for the getSizeLimit method. */351    protected static class TestGetSizeLimit extends OneTest {352        /** The receiver */353        private org.jmlspecs.samples.stacks.BoundedStackInterface receiver$;354        /** Initialize this instance. */355        public TestGetSizeLimit(org.jmlspecs.samples.stacks.BoundedStackInterface receiver$) {356            super("getSizeLimit");357            this.receiver$ = receiver$;358        }359        protected void doCall() throws java.lang.Throwable {360            receiver$.getSizeLimit();361        }362        protected java.lang.String failMessage363            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e$)364        {365            java.lang.String msg = "\n\tMethod 'getSizeLimit' applied to";366            msg += "\n\tReceiver: " + this.receiver$;367            return msg;368        }369    }370    /** Add tests for the isEmpty method371     * to the overall test suite. */372    private void addTestSuiteFor$TestIsEmpty373        (junit.framework.TestSuite overallTestSuite$)374    {375        junit.framework.TestSuite methodTests$376            = this.emptyTestSuiteFor("isEmpty");377        try {378            org.jmlspecs.jmlunit.strategies.IndefiniteIterator379                receivers$iter380                = new org.jmlspecs.jmlunit.strategies.NonNullIteratorDecorator381                    (this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter("isEmpty", 0));382            this.check_has_data383                (receivers$iter,384                 "new NonNullIteratorDecorator(this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter(\"isEmpty\", 0))");385            while (!receivers$iter.atEnd()) {386                final org.jmlspecs.samples.stacks.BoundedStackInterface receiver$387                    = (org.jmlspecs.samples.stacks.BoundedStackInterface) receivers$iter.get();388                methodTests$.addTest389                    (new TestIsEmpty(receiver$));390                receivers$iter.advance();391            }392        } catch (org.jmlspecs.jmlunit.strategies.TestSuiteFullException e$) {393            // methodTests$ doesn't want more tests394        }395        overallTestSuite$.addTest(methodTests$);396    }397    /** Test for the isEmpty method. */398    protected static class TestIsEmpty extends OneTest {399        /** The receiver */400        private org.jmlspecs.samples.stacks.BoundedStackInterface receiver$;401        /** Initialize this instance. */402        public TestIsEmpty(org.jmlspecs.samples.stacks.BoundedStackInterface receiver$) {403            super("isEmpty");404            this.receiver$ = receiver$;405        }406        protected void doCall() throws java.lang.Throwable {407            receiver$.isEmpty();408        }409        protected java.lang.String failMessage410            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e$)411        {412            java.lang.String msg = "\n\tMethod 'isEmpty' applied to";413            msg += "\n\tReceiver: " + this.receiver$;414            return msg;415        }416    }417    /** Add tests for the isFull method418     * to the overall test suite. */419    private void addTestSuiteFor$TestIsFull420        (junit.framework.TestSuite overallTestSuite$)421    {422        junit.framework.TestSuite methodTests$423            = this.emptyTestSuiteFor("isFull");424        try {425            org.jmlspecs.jmlunit.strategies.IndefiniteIterator426                receivers$iter427                = new org.jmlspecs.jmlunit.strategies.NonNullIteratorDecorator428                    (this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter("isFull", 0));429            this.check_has_data430                (receivers$iter,431                 "new NonNullIteratorDecorator(this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter(\"isFull\", 0))");432            while (!receivers$iter.atEnd()) {433                final org.jmlspecs.samples.stacks.BoundedStackInterface receiver$434                    = (org.jmlspecs.samples.stacks.BoundedStackInterface) receivers$iter.get();435                methodTests$.addTest436                    (new TestIsFull(receiver$));437                receivers$iter.advance();438            }439        } catch (org.jmlspecs.jmlunit.strategies.TestSuiteFullException e$) {440            // methodTests$ doesn't want more tests441        }442        overallTestSuite$.addTest(methodTests$);443    }444    /** Test for the isFull method. */445    protected static class TestIsFull extends OneTest {446        /** The receiver */447        private org.jmlspecs.samples.stacks.BoundedStackInterface receiver$;448        /** Initialize this instance. */449        public TestIsFull(org.jmlspecs.samples.stacks.BoundedStackInterface receiver$) {450            super("isFull");451            this.receiver$ = receiver$;452        }453        protected void doCall() throws java.lang.Throwable {454            receiver$.isFull();455        }456        protected java.lang.String failMessage457            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e$)458        {459            java.lang.String msg = "\n\tMethod 'isFull' applied to";460            msg += "\n\tReceiver: " + this.receiver$;461            return msg;462        }463    }464    /** Add tests for the clone method465     * to the overall test suite. */466    private void addTestSuiteFor$TestClone467        (junit.framework.TestSuite overallTestSuite$)468    {469        junit.framework.TestSuite methodTests$470            = this.emptyTestSuiteFor("clone");471        try {472            org.jmlspecs.jmlunit.strategies.IndefiniteIterator473                receivers$iter474                = new org.jmlspecs.jmlunit.strategies.NonNullIteratorDecorator475                    (this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter("clone", 0));476            this.check_has_data477                (receivers$iter,478                 "new NonNullIteratorDecorator(this.vorg_jmlspecs_samples_stacks_BoundedStackInterfaceIter(\"clone\", 0))");479            while (!receivers$iter.atEnd()) {480                final org.jmlspecs.samples.stacks.BoundedStackInterface receiver$481                    = (org.jmlspecs.samples.stacks.BoundedStackInterface) receivers$iter.get();482                methodTests$.addTest483                    (new TestClone(receiver$));484                receivers$iter.advance();485            }486        } catch (org.jmlspecs.jmlunit.strategies.TestSuiteFullException e$) {487            // methodTests$ doesn't want more tests488        }489        overallTestSuite$.addTest(methodTests$);490    }491    /** Test for the clone method. */492    protected static class TestClone extends OneTest {493        /** The receiver */494        private org.jmlspecs.samples.stacks.BoundedStackInterface receiver$;495        /** Initialize this instance. */496        public TestClone(org.jmlspecs.samples.stacks.BoundedStackInterface receiver$) {497            super("clone");498            this.receiver$ = receiver$;499        }500        protected void doCall() throws java.lang.Throwable {501            receiver$.clone();502        }503        protected java.lang.String failMessage504            (org.jmlspecs.jmlrac.runtime.JMLAssertionError e$)505        {506            java.lang.String msg = "\n\tMethod 'clone' applied to";507            msg += "\n\tReceiver: " + this.receiver$;508            return msg;509        }510    }511    /** Check that the iterator is non-null and not empty. */512    private void513    check_has_data(org.jmlspecs.jmlunit.strategies.IndefiniteIterator iter,514                   String call)515    {516        if (iter == null) {517            junit.framework.Assert.fail(call + " returned null");518        }519        if (iter.atEnd()) {520            junit.framework.Assert.fail(call + " returned an empty iterator");521        }522    }523    /** Converts a char to a printable String for display */524    public static String charToString(char c) {525        if (c == '\n') {526            return "NL";527        } else if (c == '\r') {528            return "CR";529        } else if (c == '\t') {530            return "TAB";531        } else if (Character.isISOControl(c)) {532            int i = (int)c;533            return "\\u"534                    + Character.forDigit((i/2048)%16,16)535                    + Character.forDigit((i/256)%16,16)536                    + Character.forDigit((i/16)%16,16)537                    + Character.forDigit((i)%16,16);538        }539        return Character.toString(c);540    }541}...Source:6529.java  
1/*******************************************************************************2 * Copyright (c) 2000, 2010 IBM Corporation and others.3 * All rights reserved. This program and the accompanying materials4 * are made available under the terms of the Eclipse Public License v1.05 * which accompanies this distribution, and is available at6 * http://www.eclipse.org/legal/epl-v10.html7 *8 * Contributors:9 *     IBM Corporation - initial API and implementation10 *******************************************************************************/11package org.eclipse.jdt.junit.tests;12import java.util.HashSet;13import java.util.Iterator;14import junit.framework.TestCase;15import org.eclipse.jdt.junit.JUnitCore;16import org.eclipse.jdt.testplugin.JavaProjectHelper;17import org.eclipse.jdt.testplugin.StringAsserts;18import org.eclipse.core.runtime.CoreException;19import org.eclipse.jdt.core.IClasspathEntry;20import org.eclipse.jdt.core.IJavaElement;21import org.eclipse.jdt.core.IJavaProject;22import org.eclipse.jdt.core.IPackageFragment;23import org.eclipse.jdt.core.IPackageFragmentRoot;24import org.eclipse.jdt.core.IType;25import org.eclipse.jdt.core.JavaCore;26import org.eclipse.jdt.internal.junit.launcher.ITestFinder;27import org.eclipse.jdt.internal.junit.launcher.ITestKind;28import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;29public class JUnit3TestFinderTest extends TestCase {30    private IJavaProject fProject;31    private IPackageFragmentRoot fRoot;32    @Override33    protected void setUp() throws Exception {34        super.setUp();35        fProject = JavaProjectHelper.createJavaProject("TestProject", "bin");36        JavaProjectHelper.addRTJar(fProject);37        IClasspathEntry cpe = JavaCore.newContainerEntry(JUnitCore.JUNIT3_CONTAINER_PATH);38        JavaProjectHelper.addToClasspath(fProject, cpe);39        fRoot = JavaProjectHelper.addSourceContainer(fProject, "src");40    }41    @Override42    protected void tearDown() throws Exception {43        JavaProjectHelper.delete(fProject);44        super.tearDown();45    }46    public void testTestCase() throws Exception {47        IPackageFragment p = fRoot.createPackageFragment("p", true, null);48        StringBuffer buf = new StringBuffer();49        buf.append("package p;\n");50        buf.append("import junit.framework.TestCase;\n");51        buf.append("\n");52        buf.append("public class MyTest extends TestCase {\n");53        buf.append("        public void testFoo() {\n");54        buf.append("        }\n");55        buf.append("}\n");56        IType validTest1 = p.createCompilationUnit("MyTest.java", buf.toString(), false, null).findPrimaryType();57        assertTestFound(validTest1, new String[] { "p.MyTest" });58        assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.MyTest" });59        buf = new StringBuffer();60        buf.append("package p;\n");61        buf.append("import junit.framework.TestCase;\n");62        buf.append("\n");63        buf.append("public class MySuperTest extends MyTest {\n");64        buf.append("        public void testFoo() {\n");65        buf.append("        }\n");66        buf.append("}\n");67        IType validTest2 = p.createCompilationUnit("MySuperTest.java", buf.toString(), false, null).findPrimaryType();68        assertTestFound(validTest2, new String[] { "p.MySuperTest" });69        assertTestFound(validTest2.getCompilationUnit(), new String[] { "p.MySuperTest" });70        buf = new StringBuffer();71        buf.append("package p;\n");72        buf.append("import junit.framework.TestCase;\n");73        buf.append("\n");74        buf.append("class InvisibleTest extends TestCase {\n");75        buf.append("        public void testFoo() {\n");76        buf.append("        }\n");77        buf.append("}\n");78        IType validTest3 = p.createCompilationUnit("InvisibleTest.java", buf.toString(), false, null).findPrimaryType();79        // accept invisible top level types80        assertTestFound(validTest3, new String[] { "p.InvisibleTest" });81        assertTestFound(validTest3.getCompilationUnit(), new String[] { "p.InvisibleTest" });82        buf = new StringBuffer();83        buf.append("package p;\n");84        buf.append("import junit.framework.TestCase;\n");85        buf.append("\n");86        buf.append("public class Outer {\n");87        buf.append("    public static class InnerTest extends TestCase {\n");88        buf.append("        public void testFoo() {\n");89        buf.append("        }\n");90        buf.append("    }\n");91        buf.append("}\n");92        IType validTest4 = p.createCompilationUnit("Outer.java", buf.toString(), false, null).getType("Outer").getType("InnerTest");93        assertTestFound(validTest4, new String[] { "p.Outer.InnerTest" });94        assertTestFound(validTest4.getCompilationUnit(), new String[] { "p.Outer.InnerTest" });95        buf = new StringBuffer();96        buf.append("package p;\n");97        buf.append("import junit.framework.TestCase;\n");98        buf.append("\n");99        buf.append("public class Outer2 {\n");100        buf.append("    public class NonStaticInnerTest extends TestCase {\n");101        buf.append("        public void testFoo() {\n");102        buf.append("        }\n");103        buf.append("    }\n");104        buf.append("    static class NonVisibleInnerTest extends TestCase {\n");105        buf.append("        public void testFoo() {\n");106        buf.append("            class LocalTest extends TestCase {\n");107        buf.append("                public void testFoo() {\n");108        buf.append("                }\n");109        buf.append("            }\n");110        buf.append("        }\n");111        buf.append("    }\n");112        buf.append("}\n");113        IType[] invalidTests = p.createCompilationUnit("Outer2.java", buf.toString(), false, null).getAllTypes();114        for (int i = 0; i < invalidTests.length; i++) {115            assertTestFound(invalidTests[i], new String[] {});116        }117        assertTestFound(invalidTests[0].getCompilationUnit(), new String[] {});118        buf = new StringBuffer();119        buf.append("package p;\n");120        buf.append("import junit.framework.TestCase;\n");121        buf.append("\n");122        buf.append("public abstract class AbstractTest extends TestCase {\n");123        buf.append("        public void testFoo() {\n");124        buf.append("        }\n");125        buf.append("}\n");126        IType invalidTest1 = p.createCompilationUnit("AbstractTest.java", buf.toString(), false, null).findPrimaryType();127        assertTestFound(invalidTest1, new String[] {});128        assertTestFound(invalidTest1.getCompilationUnit(), new String[] {});129        buf = new StringBuffer();130        buf.append("package p;\n");131        buf.append("import java.util.Vector;\n");132        buf.append("\n");133        buf.append("public class NoTest extends Vector {\n");134        buf.append("        public void testFoo() {\n");135        buf.append("        }\n");136        buf.append("}\n");137        IType invalidTest3 = p.createCompilationUnit("NoTest.java", buf.toString(), false, null).findPrimaryType();138        assertTestFound(invalidTest3, new String[] {});139        assertTestFound(invalidTest3.getCompilationUnit(), new String[] {});140        String[] validTests = { "p.MyTest", "p.MySuperTest", "p.InvisibleTest", "p.Outer.InnerTest" };141        assertTestFound(p, validTests);142        assertTestFound(fRoot, validTests);143        assertTestFound(fProject, validTests);144    }145    public void testSuite() throws Exception {146        IPackageFragment p = fRoot.createPackageFragment("p", true, null);147        StringBuffer buf = new StringBuffer();148        buf.append("package p;\n");149        buf.append("import junit.framework.Test;\n");150        buf.append("\n");151        buf.append("public class SuiteClass {\n");152        buf.append("    public static Test suite() {\n");153        buf.append("        return null;\n");154        buf.append("    }\n");155        buf.append("}\n");156        IType validTest1 = p.createCompilationUnit("SuiteClass.java", buf.toString(), false, null).getType("SuiteClass");157        assertTestFound(validTest1, new String[] { "p.SuiteClass" });158        assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.SuiteClass" });159        buf = new StringBuffer();160        buf.append("package p;\n");161        buf.append("import junit.framework.Test;\n");162        buf.append("\n");163        buf.append("public abstract class AbstractSuiteClass {\n");164        buf.append("    public static Test suite() {\n");165        buf.append("        return null;\n");166        buf.append("    }\n");167        buf.append("}\n");168        IType validTest2 = p.createCompilationUnit("AbstractSuiteClass.java", buf.toString(), false, null).getType("AbstractSuiteClass");169        assertTestFound(validTest2, new String[] { "p.AbstractSuiteClass" });170        assertTestFound(validTest2.getCompilationUnit(), new String[] { "p.AbstractSuiteClass" });171        buf = new StringBuffer();172        buf.append("package p;\n");173        buf.append("import junit.framework.Test;\n");174        buf.append("\n");175        buf.append("class InvisibleSuiteClass {\n");176        buf.append("    public static Test suite() {\n");177        buf.append("        return null;\n");178        buf.append("    }\n");179        buf.append("}\n");180        IType validTest3 = p.createCompilationUnit("InvisibleSuiteClass.java", buf.toString(), false, null).getType("InvisibleSuiteClass");181        assertTestFound(validTest3, new String[] { "p.InvisibleSuiteClass" });182        assertTestFound(validTest3.getCompilationUnit(), new String[] { "p.InvisibleSuiteClass" });183        buf = new StringBuffer();184        buf.append("package p;\n");185        buf.append("import junit.framework.Test;\n");186        buf.append("\n");187        buf.append("public class SuiteOuter {\n");188        buf.append("    public static class InnerSuite {\n");189        buf.append("        public static Test suite() {\n");190        buf.append("            return null;\n");191        buf.append("        }\n");192        buf.append("    }\n");193        buf.append("}\n");194        IType validTest4 = p.createCompilationUnit("SuiteOuter.java", buf.toString(), false, null).getType("SuiteOuter").getType("InnerSuite");195        assertTestFound(validTest4, new String[] { "p.SuiteOuter.InnerSuite" });196        assertTestFound(validTest4.getCompilationUnit(), new String[] { "p.SuiteOuter.InnerSuite" });197        buf = new StringBuffer();198        buf.append("package p;\n");199        buf.append("import junit.framework.TestCase;\n");200        buf.append("\n");201        buf.append("public class Outer2 {\n");202        buf.append("    public class NonStaticInnerSuiteTest extends TestCase {\n");203        buf.append("        public static Test suite() {\n");204        buf.append("            return null;\n");205        buf.append("        }\n");206        buf.append("    }\n");207        buf.append("    static class NonVisibleInnerTest extends TestCase {\n");208        buf.append("        public static Test suite() {\n");209        buf.append("            class LocalTest extends TestCase {\n");210        buf.append("                public static Test suite() {\n");211        buf.append("                }\n");212        buf.append("            }\n");213        buf.append("        }\n");214        buf.append("    }\n");215        buf.append("}\n");216        IType[] invalidTests = p.createCompilationUnit("Outer2.java", buf.toString(), false, null).getAllTypes();217        for (int i = 0; i < invalidTests.length; i++) {218            assertTestFound(invalidTests[i], new String[] {});219        }220        assertTestFound(invalidTests[0].getCompilationUnit(), new String[] {});221        buf = new StringBuffer();222        buf.append("package p;\n");223        buf.append("import junit.framework.Test;\n");224        buf.append("\n");225        buf.append("public class NonStaticSuite {\n");226        buf.append("    public Test suite() {\n");227        buf.append("        return null;\n");228        buf.append("    }\n");229        buf.append("}\n");230        IType invalidTest1 = p.createCompilationUnit("NonStaticSuite.java", buf.toString(), false, null).getType("NonStaticSuite");231        assertTestFound(invalidTest1, new String[] {});232        assertTestFound(invalidTest1.getCompilationUnit(), new String[] {});233        buf = new StringBuffer();234        buf.append("package p;\n");235        buf.append("import junit.framework.Test;\n");236        buf.append("\n");237        buf.append("public class NonVisibleSuite {\n");238        buf.append("    private static Test suite() {\n");239        buf.append("        return null;\n");240        buf.append("    }\n");241        buf.append("}\n");242        IType invalidTest2 = p.createCompilationUnit("NonVisibleSuite.java", buf.toString(), false, null).getType("NonVisibleSuite");243        assertTestFound(invalidTest2, new String[] {});244        assertTestFound(invalidTest2.getCompilationUnit(), new String[] {});245        buf = new StringBuffer();246        buf.append("package p;\n");247        buf.append("import junit.framework.Test;\n");248        buf.append("\n");249        buf.append("public class ParameterSuite {\n");250        buf.append("    public static Test suite(int i) {\n");251        buf.append("        return null;\n");252        buf.append("    }\n");253        buf.append("}\n");254        IType invalidTest3 = p.createCompilationUnit("ParameterSuite.java", buf.toString(), false, null).getType("ParameterSuite");255        assertTestFound(invalidTest3, new String[] {});256        assertTestFound(invalidTest3.getCompilationUnit(), new String[] {});257        String[] validTests = { "p.SuiteClass", "p.AbstractSuiteClass", "p.InvisibleSuiteClass", "p.SuiteOuter.InnerSuite" };258        assertTestFound(p, validTests);259        assertTestFound(fRoot, validTests);260        assertTestFound(fProject, validTests);261    }262    public void testTestInterface() throws Exception {263        IPackageFragment p = fRoot.createPackageFragment("p", true, null);264        StringBuffer buf = new StringBuffer();265        buf.append("package p;\n");266        buf.append("import junit.framework.Test;\n");267        buf.append("import junit.framework.TestResult;\n");268        buf.append("\n");269        buf.append("public class MyITest implements Test {\n");270        buf.append("    public int countTestCases() {\n");271        buf.append("        return 1;\n");272        buf.append("    }\n");273        buf.append("    public void run(TestResult result) {\n");274        buf.append("    }\n");275        buf.append("}\n");276        IType validTest1 = p.createCompilationUnit("MyITest.java", buf.toString(), false, null).findPrimaryType();277        assertTestFound(validTest1, new String[] { "p.MyITest" });278        assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.MyITest" });279        buf = new StringBuffer();280        buf.append("package p;\n");281        buf.append("\n");282        buf.append("public class MySuperITest extends MyITest {\n");283        buf.append("        public void testFoo() {\n");284        buf.append("        }\n");285        buf.append("}\n");286        IType validTest2 = p.createCompilationUnit("MySuperITest.java", buf.toString(), false, null).findPrimaryType();287        assertTestFound(validTest2, new String[] { "p.MySuperITest" });288        assertTestFound(validTest2.getCompilationUnit(), new String[] { "p.MySuperITest" });289        buf = new StringBuffer();290        buf.append("package p;\n");291        buf.append("import junit.framework.Test;\n");292        buf.append("\n");293        buf.append("public interface MyITestSuperInterface extends Test {\n");294        buf.append("}\n");295        IType invalidTest1 = p.createCompilationUnit("MyITestSuperInterface.java", buf.toString(), false, null).findPrimaryType();296        assertTestFound(invalidTest1, new String[] {});297        assertTestFound(invalidTest1.getCompilationUnit(), new String[] {});298        buf = new StringBuffer();299        buf.append("package p;\n");300        buf.append("import junit.framework.TestResult;\n");301        buf.append("\n");302        buf.append("public class MyITestSuperInterfaceImpl implements MyITestSuperInterface {\n");303        buf.append("    public int countTestCases() {\n");304        buf.append("        return 1;\n");305        buf.append("    }\n");306        buf.append("    public void run(TestResult result) {\n");307        buf.append("    }\n");308        buf.append("}\n");309        IType validTest3 = p.createCompilationUnit("MyITestSuperInterfaceImpl.java", buf.toString(), false, null).findPrimaryType();310        assertTestFound(validTest3, new String[] { "p.MyITestSuperInterfaceImpl" });311        assertTestFound(validTest3.getCompilationUnit(), new String[] { "p.MyITestSuperInterfaceImpl" });312        String[] validTests = { "p.MyITest", "p.MySuperITest", "p.MyITestSuperInterfaceImpl" };313        assertTestFound(p, validTests);314        assertTestFound(fRoot, validTests);315        assertTestFound(fProject, validTests);316    }317    private void assertTestFound(IJavaElement container, String[] expectedValidTests) throws CoreException {318        ITestKind testKind = TestKindRegistry.getContainerTestKind(container);319        assertEquals(TestKindRegistry.JUNIT3_TEST_KIND_ID, testKind.getId());320        ITestFinder finder = testKind.getFinder();321        if (container instanceof IType) {322            IType type = (IType) container;323            boolean isValidTest = expectedValidTests.length == 1 && type.getFullyQualifiedName('.').equals(expectedValidTests[0]);324            assertEquals(type.getFullyQualifiedName('.'), isValidTest, finder.isTest(type));325        }326        HashSet<IType> set = new HashSet();327        finder.findTestsInContainer(container, set, null);328        HashSet<String> namesFound = new HashSet();329        for (Iterator<IType> iterator = set.iterator(); iterator.hasNext(); ) {330            namesFound.add(iterator.next().getFullyQualifiedName('.'));331        }332        String[] actuals = namesFound.toArray(new String[namesFound.size()]);333        StringAsserts.assertEqualStringsIgnoreOrder(actuals, expectedValidTests);334    }335}...Source:JUnit3TestFinderTest.java  
1/*******************************************************************************2 * Copyright (c) 2000, 2010 IBM Corporation and others.3 * All rights reserved. This program and the accompanying materials4 * are made available under the terms of the Eclipse Public License v1.05 * which accompanies this distribution, and is available at6 * http://www.eclipse.org/legal/epl-v10.html7 *8 * Contributors:9 *     IBM Corporation - initial API and implementation10 *******************************************************************************/11package org.eclipse.jdt.junit.tests;12import java.util.HashSet;13import java.util.Iterator;14import junit.framework.TestCase;15import org.eclipse.jdt.junit.JUnitCore;16import org.eclipse.jdt.testplugin.JavaProjectHelper;17import org.eclipse.jdt.testplugin.StringAsserts;18import org.eclipse.core.runtime.CoreException;19import org.eclipse.jdt.core.IClasspathEntry;20import org.eclipse.jdt.core.IJavaElement;21import org.eclipse.jdt.core.IJavaProject;22import org.eclipse.jdt.core.IPackageFragment;23import org.eclipse.jdt.core.IPackageFragmentRoot;24import org.eclipse.jdt.core.IType;25import org.eclipse.jdt.core.JavaCore;26import org.eclipse.jdt.internal.junit.launcher.ITestFinder;27import org.eclipse.jdt.internal.junit.launcher.ITestKind;28import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;29public class JUnit3TestFinderTest extends TestCase {30	private IJavaProject fProject;31	private IPackageFragmentRoot fRoot;32	protected void setUp() throws Exception {33		super.setUp();34		fProject= JavaProjectHelper.createJavaProject("TestProject", "bin");35		JavaProjectHelper.addRTJar(fProject);36		IClasspathEntry cpe= JavaCore.newContainerEntry(JUnitCore.JUNIT3_CONTAINER_PATH);37		JavaProjectHelper.addToClasspath(fProject, cpe);38		fRoot= JavaProjectHelper.addSourceContainer(fProject, "src");39	}40	protected void tearDown() throws Exception {41		JavaProjectHelper.delete(fProject);42		super.tearDown();43	}44	public void testTestCase() throws Exception {45		IPackageFragment p= fRoot.createPackageFragment("p", true, null);46		StringBuffer buf= new StringBuffer();47		buf.append("package p;\n");48		buf.append("import junit.framework.TestCase;\n");49		buf.append("\n");50		buf.append("public class MyTest extends TestCase {\n");51		buf.append("        public void testFoo() {\n");52		buf.append("        }\n");53		buf.append("}\n");54		IType validTest1= p.createCompilationUnit("MyTest.java", buf.toString(), false, null).findPrimaryType();55		assertTestFound(validTest1, new String[] { "p.MyTest" });56		assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.MyTest" });57		buf= new StringBuffer();58		buf.append("package p;\n");59		buf.append("import junit.framework.TestCase;\n");60		buf.append("\n");61		buf.append("public class MySuperTest extends MyTest {\n");62		buf.append("        public void testFoo() {\n");63		buf.append("        }\n");64		buf.append("}\n");65		IType validTest2= p.createCompilationUnit("MySuperTest.java", buf.toString(), false, null).findPrimaryType();66		assertTestFound(validTest2, new String[] { "p.MySuperTest" });67		assertTestFound(validTest2.getCompilationUnit(), new String[] { "p.MySuperTest" });68		buf= new StringBuffer();69		buf.append("package p;\n");70		buf.append("import junit.framework.TestCase;\n");71		buf.append("\n");72		buf.append("class InvisibleTest extends TestCase {\n");73		buf.append("        public void testFoo() {\n");74		buf.append("        }\n");75		buf.append("}\n");76		IType validTest3= p.createCompilationUnit("InvisibleTest.java", buf.toString(), false, null).findPrimaryType();77		// accept invisible top level types78		assertTestFound(validTest3, new String[] { "p.InvisibleTest" });79		assertTestFound(validTest3.getCompilationUnit(), new String[] { "p.InvisibleTest" });80		buf= new StringBuffer();81		buf.append("package p;\n");82		buf.append("import junit.framework.TestCase;\n");83		buf.append("\n");84		buf.append("public class Outer {\n");85		buf.append("    public static class InnerTest extends TestCase {\n");86		buf.append("        public void testFoo() {\n");87		buf.append("        }\n");88		buf.append("    }\n");89		buf.append("}\n");90		IType validTest4= p.createCompilationUnit("Outer.java", buf.toString(), false, null).getType("Outer").getType("InnerTest");91		assertTestFound(validTest4, new String[] { "p.Outer.InnerTest" });92		assertTestFound(validTest4.getCompilationUnit(), new String[] { "p.Outer.InnerTest" });93		buf= new StringBuffer();94		buf.append("package p;\n");95		buf.append("import junit.framework.TestCase;\n");96		buf.append("\n");97		buf.append("public class Outer2 {\n");98		buf.append("    public class NonStaticInnerTest extends TestCase {\n");99		buf.append("        public void testFoo() {\n");100		buf.append("        }\n");101		buf.append("    }\n");102		buf.append("    static class NonVisibleInnerTest extends TestCase {\n");103		buf.append("        public void testFoo() {\n");104		buf.append("            class LocalTest extends TestCase {\n");105		buf.append("                public void testFoo() {\n");106		buf.append("                }\n");107		buf.append("            }\n");108		buf.append("        }\n");109		buf.append("    }\n");110		buf.append("}\n");111		IType[] invalidTests= p.createCompilationUnit("Outer2.java", buf.toString(), false, null).getAllTypes();112		for (int i= 0; i < invalidTests.length; i++) {113			assertTestFound(invalidTests[i], new String[] {});114		}115		assertTestFound(invalidTests[0].getCompilationUnit(), new String[] {});116		buf= new StringBuffer();117		buf.append("package p;\n");118		buf.append("import junit.framework.TestCase;\n");119		buf.append("\n");120		buf.append("public abstract class AbstractTest extends TestCase {\n");121		buf.append("        public void testFoo() {\n");122		buf.append("        }\n");123		buf.append("}\n");124		IType invalidTest1= p.createCompilationUnit("AbstractTest.java", buf.toString(), false, null).findPrimaryType();125		assertTestFound(invalidTest1, new String[] {});126		assertTestFound(invalidTest1.getCompilationUnit(), new String[] {});127		buf= new StringBuffer();128		buf.append("package p;\n");129		buf.append("import java.util.Vector;\n");130		buf.append("\n");131		buf.append("public class NoTest extends Vector {\n");132		buf.append("        public void testFoo() {\n");133		buf.append("        }\n");134		buf.append("}\n");135		IType invalidTest3= p.createCompilationUnit("NoTest.java", buf.toString(), false, null).findPrimaryType();136		assertTestFound(invalidTest3, new String[] {});137		assertTestFound(invalidTest3.getCompilationUnit(), new String[] {});138		String[] validTests= { "p.MyTest", "p.MySuperTest", "p.InvisibleTest", "p.Outer.InnerTest" };139		assertTestFound(p, validTests);140		assertTestFound(fRoot, validTests);141		assertTestFound(fProject, validTests);142	}143	public void testSuite() throws Exception {144		IPackageFragment p= fRoot.createPackageFragment("p", true, null);145		StringBuffer buf= new StringBuffer();146		buf.append("package p;\n");147		buf.append("import junit.framework.Test;\n");148		buf.append("\n");149		buf.append("public class SuiteClass {\n");150		buf.append("    public static Test suite() {\n");151		buf.append("        return null;\n");152		buf.append("    }\n");153		buf.append("}\n");154		IType validTest1= p.createCompilationUnit("SuiteClass.java", buf.toString(), false, null).getType("SuiteClass");155		assertTestFound(validTest1, new String[] { "p.SuiteClass" });156		assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.SuiteClass" });157		buf= new StringBuffer();158		buf.append("package p;\n");159		buf.append("import junit.framework.Test;\n");160		buf.append("\n");161		buf.append("public abstract class AbstractSuiteClass {\n");162		buf.append("    public static Test suite() {\n");163		buf.append("        return null;\n");164		buf.append("    }\n");165		buf.append("}\n");166		IType validTest2= p.createCompilationUnit("AbstractSuiteClass.java", buf.toString(), false, null).getType("AbstractSuiteClass");167		assertTestFound(validTest2, new String[] { "p.AbstractSuiteClass" });168		assertTestFound(validTest2.getCompilationUnit(), new String[] { "p.AbstractSuiteClass" });169		buf= new StringBuffer();170		buf.append("package p;\n");171		buf.append("import junit.framework.Test;\n");172		buf.append("\n");173		buf.append("class InvisibleSuiteClass {\n");174		buf.append("    public static Test suite() {\n");175		buf.append("        return null;\n");176		buf.append("    }\n");177		buf.append("}\n");178		IType validTest3= p.createCompilationUnit("InvisibleSuiteClass.java", buf.toString(), false, null).getType("InvisibleSuiteClass");179		assertTestFound(validTest3, new String[] { "p.InvisibleSuiteClass" });180		assertTestFound(validTest3.getCompilationUnit(), new String[] { "p.InvisibleSuiteClass" });181		buf= new StringBuffer();182		buf.append("package p;\n");183		buf.append("import junit.framework.Test;\n");184		buf.append("\n");185		buf.append("public class SuiteOuter {\n");186		buf.append("    public static class InnerSuite {\n");187		buf.append("        public static Test suite() {\n");188		buf.append("            return null;\n");189		buf.append("        }\n");190		buf.append("    }\n");191		buf.append("}\n");192		IType validTest4= p.createCompilationUnit("SuiteOuter.java", buf.toString(), false, null).getType("SuiteOuter").getType("InnerSuite");193		assertTestFound(validTest4, new String[] { "p.SuiteOuter.InnerSuite" });194		assertTestFound(validTest4.getCompilationUnit(), new String[] { "p.SuiteOuter.InnerSuite" });195		buf= new StringBuffer();196		buf.append("package p;\n");197		buf.append("import junit.framework.TestCase;\n");198		buf.append("\n");199		buf.append("public class Outer2 {\n");200		buf.append("    public class NonStaticInnerSuiteTest extends TestCase {\n");201		buf.append("        public static Test suite() {\n");202		buf.append("            return null;\n");203		buf.append("        }\n");204		buf.append("    }\n");205		buf.append("    static class NonVisibleInnerTest extends TestCase {\n");206		buf.append("        public static Test suite() {\n");207		buf.append("            class LocalTest extends TestCase {\n");208		buf.append("                public static Test suite() {\n");209		buf.append("                }\n");210		buf.append("            }\n");211		buf.append("        }\n");212		buf.append("    }\n");213		buf.append("}\n");214		IType[] invalidTests= p.createCompilationUnit("Outer2.java", buf.toString(), false, null).getAllTypes();215		for (int i= 0; i < invalidTests.length; i++) {216			assertTestFound(invalidTests[i], new String[] {});217		}218		assertTestFound(invalidTests[0].getCompilationUnit(), new String[] {});219		buf= new StringBuffer();220		buf.append("package p;\n");221		buf.append("import junit.framework.Test;\n");222		buf.append("\n");223		buf.append("public class NonStaticSuite {\n");224		buf.append("    public Test suite() {\n");225		buf.append("        return null;\n");226		buf.append("    }\n");227		buf.append("}\n");228		IType invalidTest1= p.createCompilationUnit("NonStaticSuite.java", buf.toString(), false, null).getType("NonStaticSuite");229		assertTestFound(invalidTest1, new String[] {});230		assertTestFound(invalidTest1.getCompilationUnit(), new String[] {});231		buf= new StringBuffer();232		buf.append("package p;\n");233		buf.append("import junit.framework.Test;\n");234		buf.append("\n");235		buf.append("public class NonVisibleSuite {\n");236		buf.append("    private static Test suite() {\n");237		buf.append("        return null;\n");238		buf.append("    }\n");239		buf.append("}\n");240		IType invalidTest2= p.createCompilationUnit("NonVisibleSuite.java", buf.toString(), false, null).getType("NonVisibleSuite");241		assertTestFound(invalidTest2, new String[] {});242		assertTestFound(invalidTest2.getCompilationUnit(), new String[] {});243		buf= new StringBuffer();244		buf.append("package p;\n");245		buf.append("import junit.framework.Test;\n");246		buf.append("\n");247		buf.append("public class ParameterSuite {\n");248		buf.append("    public static Test suite(int i) {\n");249		buf.append("        return null;\n");250		buf.append("    }\n");251		buf.append("}\n");252		IType invalidTest3= p.createCompilationUnit("ParameterSuite.java", buf.toString(), false, null).getType("ParameterSuite");253		assertTestFound(invalidTest3, new String[] {});254		assertTestFound(invalidTest3.getCompilationUnit(), new String[] {});255		String[] validTests= { "p.SuiteClass", "p.AbstractSuiteClass", "p.InvisibleSuiteClass", "p.SuiteOuter.InnerSuite" };256		assertTestFound(p, validTests);257		assertTestFound(fRoot, validTests);258		assertTestFound(fProject, validTests);259	}260	public void testTestInterface() throws Exception {261		IPackageFragment p= fRoot.createPackageFragment("p", true, null);262		StringBuffer buf= new StringBuffer();263		buf.append("package p;\n");264		buf.append("import junit.framework.Test;\n");265		buf.append("import junit.framework.TestResult;\n");266		buf.append("\n");267		buf.append("public class MyITest implements Test {\n");268		buf.append("    public int countTestCases() {\n");269		buf.append("        return 1;\n");270		buf.append("    }\n");271		buf.append("    public void run(TestResult result) {\n");272		buf.append("    }\n");273		buf.append("}\n");274		IType validTest1= p.createCompilationUnit("MyITest.java", buf.toString(), false, null).findPrimaryType();275		assertTestFound(validTest1, new String[] { "p.MyITest" });276		assertTestFound(validTest1.getCompilationUnit(), new String[] { "p.MyITest" });277		buf= new StringBuffer();278		buf.append("package p;\n");279		buf.append("\n");280		buf.append("public class MySuperITest extends MyITest {\n");281		buf.append("        public void testFoo() {\n");282		buf.append("        }\n");283		buf.append("}\n");284		IType validTest2= p.createCompilationUnit("MySuperITest.java", buf.toString(), false, null).findPrimaryType();285		assertTestFound(validTest2, new String[] { "p.MySuperITest" });286		assertTestFound(validTest2.getCompilationUnit(), new String[] { "p.MySuperITest" });287		buf= new StringBuffer();288		buf.append("package p;\n");289		buf.append("import junit.framework.Test;\n");290		buf.append("\n");291		buf.append("public interface MyITestSuperInterface extends Test {\n");292		buf.append("}\n");293		IType invalidTest1= p.createCompilationUnit("MyITestSuperInterface.java", buf.toString(), false, null).findPrimaryType();294		assertTestFound(invalidTest1, new String[] {});295		assertTestFound(invalidTest1.getCompilationUnit(), new String[] {});296		buf= new StringBuffer();297		buf.append("package p;\n");298		buf.append("import junit.framework.TestResult;\n");299		buf.append("\n");300		buf.append("public class MyITestSuperInterfaceImpl implements MyITestSuperInterface {\n");301		buf.append("    public int countTestCases() {\n");302		buf.append("        return 1;\n");303		buf.append("    }\n");304		buf.append("    public void run(TestResult result) {\n");305		buf.append("    }\n");306		buf.append("}\n");307		IType validTest3= p.createCompilationUnit("MyITestSuperInterfaceImpl.java", buf.toString(), false, null).findPrimaryType();308		assertTestFound(validTest3, new String[] { "p.MyITestSuperInterfaceImpl" });309		assertTestFound(validTest3.getCompilationUnit(), new String[] { "p.MyITestSuperInterfaceImpl" });310		String[] validTests= { "p.MyITest", "p.MySuperITest", "p.MyITestSuperInterfaceImpl" };311		assertTestFound(p, validTests);312		assertTestFound(fRoot, validTests);313		assertTestFound(fProject, validTests);314	}315	private void assertTestFound(IJavaElement container, String[] expectedValidTests) throws CoreException {316		ITestKind testKind= TestKindRegistry.getContainerTestKind(container);317		assertEquals(TestKindRegistry.JUNIT3_TEST_KIND_ID, testKind.getId());318		ITestFinder finder= testKind.getFinder();319		if (container instanceof IType) {320			IType type= (IType) container;321			boolean isValidTest= expectedValidTests.length == 1 && type.getFullyQualifiedName('.').equals(expectedValidTests[0]);322			assertEquals(type.getFullyQualifiedName('.'), isValidTest, finder.isTest(type));323		}324		HashSet set= new HashSet();325		finder.findTestsInContainer(container, set, null);326		HashSet namesFound= new HashSet();327		for (Iterator iterator= set.iterator(); iterator.hasNext();) {328			namesFound.add(((IType) iterator.next()).getFullyQualifiedName('.'));329		}330		String[] actuals= (String[]) namesFound.toArray(new String[namesFound.size()]);331		StringAsserts.assertEqualStringsIgnoreOrder(actuals, expectedValidTests);332	}333}...Source:AdaptiveServiceTestCase.java  
1/**2 * AdaptiveServiceTestCase.java3 *4 * This file was auto-generated from WSDL5 * by the Apache Axis WSDL2Java emitter.6 */7package test.wsdl.adaptive;8import java.util.Arrays;9public class AdaptiveServiceTestCase extends junit.framework.TestCase {10    public AdaptiveServiceTestCase(java.lang.String name) {11        super(name);12    }13    public void testAdaptiveWSDL() throws Exception {14        javax.xml.rpc.ServiceFactory serviceFactory = javax.xml.rpc.ServiceFactory.newInstance();15        java.net.URL url = new java.net.URL(new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptiveAddress() + "?WSDL");16        javax.xml.rpc.Service service = serviceFactory.createService(url, new test.wsdl.adaptive.AdaptiveServiceLocator().getServiceName());17        assertTrue(service != null);18    }19    public void test1AdaptiveGetServiceDescription() throws Exception {20        test.wsdl.adaptive.AdaptiveInterfaceBindingStub binding;21        try {22            binding = (test.wsdl.adaptive.AdaptiveInterfaceBindingStub)23                          new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptive();24        }25        catch (javax.xml.rpc.ServiceException jre) {26            if(jre.getLinkedCause()!=null)27                jre.getLinkedCause().printStackTrace();28            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);29        }30        assertNotNull("binding is null", binding);31        // Time out after a minute32        binding.setTimeout(60000);33        // Test operation34        java.lang.String value = null;35        value = binding.getServiceDescription();36        // TBD - validate results37    }38    public void test2AdaptiveRankResources() throws Exception {39        test.wsdl.adaptive.AdaptiveInterfaceBindingStub binding;40        try {41            binding = (test.wsdl.adaptive.AdaptiveInterfaceBindingStub)42                          new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptive();43        }44        catch (javax.xml.rpc.ServiceException jre) {45            if(jre.getLinkedCause()!=null)46                jre.getLinkedCause().printStackTrace();47            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);48        }49        assertNotNull("binding is null", binding);50        // Time out after a minute51        binding.setTimeout(60000);52        // Test operation53        test.wsdl.adaptive.types.ResourceInfo[] value = null;54        value = binding.rankResources(new test.wsdl.adaptive.types.ResourceInfo[0], new test.wsdl.adaptive.types.ApplicationInfo());55        // TBD - validate results56        assertTrue(value.length > 0);57        assertEquals(value[0].getId(), "Adaptive #1");58        java.lang.Object[] collection = value[0].getProperties().getCollection();59        assertTrue(collection.length > 0);60        assertTrue(Arrays.equals(collection, new String[]{"A","B","C"}));61    }62    public void test3AdaptiveEstimateTransferTime() throws Exception {63        test.wsdl.adaptive.AdaptiveInterfaceBindingStub binding;64        try {65            binding = (test.wsdl.adaptive.AdaptiveInterfaceBindingStub)66                          new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptive();67        }68        catch (javax.xml.rpc.ServiceException jre) {69            if(jre.getLinkedCause()!=null)70                jre.getLinkedCause().printStackTrace();71            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);72        }73        assertNotNull("binding is null", binding);74        // Time out after a minute75        binding.setTimeout(60000);76        // Test operation77        int[] value = null;78        value = binding.estimateTransferTime(true, new test.wsdl.adaptive.types.ResourceInfo(), new test.wsdl.adaptive.types.ResourceInfo[0], 0, java.util.Calendar.getInstance());79        // TBD - validate results80    }81    public void test4AdaptiveLogDataTransfer() throws Exception {82        test.wsdl.adaptive.AdaptiveInterfaceBindingStub binding;83        try {84            binding = (test.wsdl.adaptive.AdaptiveInterfaceBindingStub)85                          new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptive();86        }87        catch (javax.xml.rpc.ServiceException jre) {88            if(jre.getLinkedCause()!=null)89                jre.getLinkedCause().printStackTrace();90            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);91        }92        assertNotNull("binding is null", binding);93        // Time out after a minute94        binding.setTimeout(60000);95        // Test operation96        binding.logDataTransfer(new test.wsdl.adaptive.types.ResourceInfo(), new test.wsdl.adaptive.types.ResourceInfo(), 0, java.util.Calendar.getInstance(), java.util.Calendar.getInstance());97        // TBD - validate results98    }99    public void test5AdaptiveEstimateUsage() throws Exception {100        test.wsdl.adaptive.AdaptiveInterfaceBindingStub binding;101        try {102            binding = (test.wsdl.adaptive.AdaptiveInterfaceBindingStub)103                          new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptive();104        }105        catch (javax.xml.rpc.ServiceException jre) {106            if(jre.getLinkedCause()!=null)107                jre.getLinkedCause().printStackTrace();108            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);109        }110        assertNotNull("binding is null", binding);111        // Time out after a minute112        binding.setTimeout(60000);113        // Test operation114        java.lang.String value = null;115        value = binding.estimateUsage(true, new test.wsdl.adaptive.types.ResourceInfo(), new java.lang.String(), 0, java.util.Calendar.getInstance(), java.util.Calendar.getInstance());116        // TBD - validate results117    }118    public void test6AdaptiveEstimateMultipleUsage() throws Exception {119        test.wsdl.adaptive.AdaptiveInterfaceBindingStub binding;120        try {121            binding = (test.wsdl.adaptive.AdaptiveInterfaceBindingStub)122                          new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptive();123        }124        catch (javax.xml.rpc.ServiceException jre) {125            if(jre.getLinkedCause()!=null)126                jre.getLinkedCause().printStackTrace();127            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);128        }129        assertNotNull("binding is null", binding);130        // Time out after a minute131        binding.setTimeout(60000);132        // Test operation133        java.lang.String[][] value = null;134        value = binding.estimateMultipleUsage(true, new test.wsdl.adaptive.types.ResourceInfo[0], new java.lang.String[0], 0, java.util.Calendar.getInstance(), java.util.Calendar.getInstance());135        // TBD - validate results136    }137    public void test7AdaptiveEstimateNetworkGraph() throws Exception {138        test.wsdl.adaptive.AdaptiveInterfaceBindingStub binding;139        try {140            binding = (test.wsdl.adaptive.AdaptiveInterfaceBindingStub)141                          new test.wsdl.adaptive.AdaptiveServiceLocator().getAdaptive();142        }143        catch (javax.xml.rpc.ServiceException jre) {144            if(jre.getLinkedCause()!=null)145                jre.getLinkedCause().printStackTrace();146            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);147        }148        assertNotNull("binding is null", binding);149        // Time out after a minute150        binding.setTimeout(60000);151        // Test operation152        java.lang.String[][] value = null;153        value = binding.estimateNetworkGraph(true, new test.wsdl.adaptive.types.ResourceInfo[0], 0, java.util.Calendar.getInstance(), java.util.Calendar.getInstance());154        // TBD - validate results155    }156}...Source:WrappedInOutTestCase.java  
1/**2 * WrappedInOutTestCase.java3 *4 * This file was auto-generated from WSDL5 * by the Apache Axis WSDL2Java emitter.6 */7package test.wsdl.wrapped_inout;8public class WrappedInOutTestCase extends junit.framework.TestCase {9    public WrappedInOutTestCase(java.lang.String name) {10        super(name);11    }12    public void test1WrappedInOutEchoString() throws Exception {13        test.wsdl.wrapped_inout.WrappedInOutInterface binding;14        try {15            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();16        }17        catch (javax.xml.rpc.ServiceException jre) {18            if(jre.getLinkedCause()!=null)19                jre.getLinkedCause().printStackTrace();20            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);21        }22        assertTrue("binding is null", binding != null);23        java.lang.String expected = new String("This is a test");24        java.lang.String value = null;25        value = binding.echoString(expected);26        assertEquals(value, expected);27    }28    public void test1WrappedInOutEchoEmptyString() throws Exception {29        test.wsdl.wrapped_inout.WrappedInOutInterface binding;30        try {31            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();32        }33        catch (javax.xml.rpc.ServiceException jre) {34            if(jre.getLinkedCause()!=null)35                jre.getLinkedCause().printStackTrace();36            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);37        }38        assertTrue("binding is null", binding != null);39        java.lang.String expected = new String("");40        java.lang.String value = null;41        value = binding.echoString(expected);42        assertEquals(value, expected);43    }44    public void test2WrappedInOutEchoStringIO() throws Exception {45        test.wsdl.wrapped_inout.WrappedInOutInterface binding;46        try {47            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();48        }49        catch (javax.xml.rpc.ServiceException jre) {50            if(jre.getLinkedCause()!=null)51                jre.getLinkedCause().printStackTrace();52            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);53        }54        assertTrue("binding is null", binding != null);55        java.lang.String expected = new String("This is a test");56        java.lang.String value = null;57        value = binding.echoStringIO(expected);58        assertEquals(value, expected);59    }60    public void test3WrappedInOutEchoStringIOret() throws Exception {61        test.wsdl.wrapped_inout.WrappedInOutInterface binding;62        try {63            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();64        }65        catch (javax.xml.rpc.ServiceException jre) {66            if(jre.getLinkedCause()!=null)67                jre.getLinkedCause().printStackTrace();68            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);69        }70        assertTrue("binding is null", binding != null);71        java.lang.String value = null;72        javax.xml.rpc.holders.StringHolder ioarg = new javax.xml.rpc.holders.StringHolder("in1");73        value = binding.echoStringIOret(ioarg);74        assertEquals(value, "return");75        assertNotNull(ioarg);76        assertEquals(ioarg.value, "out1");77    }78    public void test4WrappedInOutEchoStringInIO() throws Exception {79        test.wsdl.wrapped_inout.WrappedInOutInterface binding;80        try {81            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();82        }83        catch (javax.xml.rpc.ServiceException jre) {84            if(jre.getLinkedCause()!=null)85                jre.getLinkedCause().printStackTrace();86            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);87        }88        assertTrue("binding is null", binding != null);89        javax.xml.rpc.holders.StringHolder holder = new javax.xml.rpc.holders.StringHolder("in2");90        binding.echoStringInIO("in1", holder);91        assertNotNull(holder);92        assertEquals(holder.value, "in1");93    }94    public void test5WrappedInOutEchoStringBig() throws Exception {95        test.wsdl.wrapped_inout.WrappedInOutInterface binding;96        try {97            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();98        }99        catch (javax.xml.rpc.ServiceException jre) {100            if(jre.getLinkedCause()!=null)101                jre.getLinkedCause().printStackTrace();102            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);103        }104        assertTrue("binding is null", binding != null);105        java.lang.String value = null;106        javax.xml.rpc.holders.StringHolder ioarg1 = new javax.xml.rpc.holders.StringHolder("ioarg1");107        javax.xml.rpc.holders.StringHolder ioarg2 = new javax.xml.rpc.holders.StringHolder("ioarg2");108        value = binding.echoStringBig("firstin1", ioarg1, ioarg2);109        assertNotNull(value);110        assertEquals(value, "firstin1");111        assertEquals(ioarg1.value, "out1");112        assertEquals(ioarg2.value, "out2");113    }114    public void test6WrappedInOutHelloInOut() throws Exception {115        test.wsdl.wrapped_inout.WrappedInOutInterface binding;116        try {117            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();118        }119        catch (javax.xml.rpc.ServiceException jre) {120            if(jre.getLinkedCause()!=null)121                jre.getLinkedCause().printStackTrace();122            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);123        }124        assertTrue("binding is null", binding != null);125        javax.xml.rpc.holders.StringHolder ioarg1 = new javax.xml.rpc.holders.StringHolder("ioarg1");126        javax.xml.rpc.holders.StringHolder ioarg2 = new javax.xml.rpc.holders.StringHolder("ioarg2");127        javax.xml.rpc.holders.StringHolder ioarg3 = new javax.xml.rpc.holders.StringHolder("ioarg3");128        javax.xml.rpc.holders.StringHolder ioarg4 = new javax.xml.rpc.holders.StringHolder("ioarg4");129        binding.helloInOut(ioarg1, ioarg2, ioarg3, ioarg4, "onlyInput");130        assertEquals(ioarg1.value, "out1");131        assertEquals(ioarg2.value, "out2");132        assertEquals(ioarg3.value, "out3");133        assertEquals(ioarg4.value, "out4");134    }135    136    public void test7WrappedInOutEchoPhone() throws Exception {137        test.wsdl.wrapped_inout.WrappedInOutInterface binding;138        try {139            binding = new test.wsdl.wrapped_inout.WrappedInOutLocator().getWrappedInOut();140        }141        catch (javax.xml.rpc.ServiceException jre) {142            if(jre.getLinkedCause()!=null)143                jre.getLinkedCause().printStackTrace();144            throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);145        }146        assertTrue("binding is null", binding != null);147        test.wsdl.wrapped_inout.Phone value = null;148        test.wsdl.wrapped_inout.Phone in = new test.wsdl.wrapped_inout.Phone();149        in.setAreaCode("503");150        in.setPrefix("281");151        in.setNumber("0816");152        value = binding.echoPhone(in);153        assertNotNull(value);154        assertEquals(value, in);155    }156}...Source:InterfaceSetTest.java  
1/*2 * Copyright (C) 2018 The Android Open Source Project3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 *      http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package android.net.util;17import static junit.framework.Assert.assertEquals;18import static junit.framework.Assert.assertFalse;19import static junit.framework.Assert.assertTrue;20import androidx.test.filters.SmallTest;21import androidx.test.runner.AndroidJUnit4;22import org.junit.Test;23import org.junit.runner.RunWith;24@RunWith(AndroidJUnit4.class)25@SmallTest26public class InterfaceSetTest {27    @Test28    public void testNullNamesIgnored() {29        final InterfaceSet set = new InterfaceSet(null, "if1", null, "if2", null);30        assertEquals(2, set.ifnames.size());31        assertTrue(set.ifnames.contains("if1"));32        assertTrue(set.ifnames.contains("if2"));33    }34    @Test35    public void testToString() {36        final InterfaceSet set = new InterfaceSet("if1", "if2");37        final String setString = set.toString();38        assertTrue(setString.equals("[if1,if2]") || setString.equals("[if2,if1]"));39    }40    @Test41    public void testToString_Empty() {42        final InterfaceSet set = new InterfaceSet(null, null);43        assertEquals("[]", set.toString());44    }45    @Test46    public void testEquals() {47        assertEquals(new InterfaceSet(null, "if1", "if2"), new InterfaceSet("if2", "if1"));48        assertEquals(new InterfaceSet(null, null), new InterfaceSet());49        assertFalse(new InterfaceSet("if1", "if3").equals(new InterfaceSet("if1", "if2")));50        assertFalse(new InterfaceSet("if1", "if2").equals(new InterfaceSet("if1")));51        assertFalse(new InterfaceSet().equals(null));52    }53}...Source:2990.java  
1/*******************************************************************************2 * Copyright (c) 2005 Contributors.3 * All rights reserved. 4 * This program and the accompanying materials are made available 5 * under the terms of the Eclipse Public License v1.0 6 * which accompanies this distribution and is available at 7 * http://eclipse.org/legal/epl-v10.html 8 * 9 * Contributors:10 *   Alexandre Vasseur         initial implementation11 *******************************************************************************/12package ataspectj;13import junit.framework.TestCase;14import org.aspectj.lang.annotation.Aspect;15import org.aspectj.lang.annotation.Before;16import org.aspectj.lang.annotation.DeclareParents;17import java.util.Arrays;18/**19 * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>20 */21public class DeclareParentsInterfaceTest extends TestCase {22    static class Target {23        void target() {24            log("hello");25        }26    }27    static interface Marker {}28    @Aspect29    static class TestAspect {30        @DeclareParents("ataspectj.DeclareParentsInterfaceTest.Target")31        Marker introduce;32        @Before("execution(* ataspectj.DeclareParentsInterfaceTest.Marker+.target())")33        public void before() {34            log("aop");35        }36    }37    static StringBuffer s_log = new StringBuffer();38    static void log(String s) {39        s_log.append(s).append(" ");40    }41    public void testDecPInt() {42        Class[] intfs = Target.class.getInterfaces();43        assertTrue("Was not introduced", Arrays.asList(intfs).contains(Marker.class));44    }45    public void testDecPIntAdvised() {46        s_log = new StringBuffer();47        new Target().target();48        assertEquals("aop hello ", s_log.toString());49    }50    public static void main(String[] args) {51        TestHelper.runAndThrowOnFailure(suite());52    }53    public static junit.framework.Test suite() {54        return new junit.framework.TestSuite(DeclareParentsInterfaceTest.class);55    }56}...Source:TestListener.java  
1package junit.framework;2public interface TestListener3{4public abstract  void addError(junit.framework.Test test, java.lang.Throwable t);5public abstract  void addFailure(junit.framework.Test test, junit.framework.AssertionFailedError t);6public abstract  void endTest(junit.framework.Test test);7public abstract  void startTest(junit.framework.Test test);8}...Interface Test
Using AI Code Generation
1import junit.framework.*;2public class TestJunit extends TestCase {3   protected int value1, value2;4   protected void setUp(){5      value1 = 3;6      value2 = 3;7   }8   public void testAdd(){9      double result = value1 + value2;10      assertTrue(result == 6);11   }12}13OK (1 test)Interface Test
Using AI Code Generation
1import junit.framework.*;2public class TestJunit extends TestCase {3   protected int value1, value2;4   protected void setUp(){5      value1 = 3;6      value2 = 3;7   }8   public void testAdd(){9      double result = value1 + value2;10      assertTrue(result == 6);11   }12}13C:\Users\hp>java -cp "C:\junit\junit-4.12.jar;C:\junit\hamcrest-core-1.3.jar;." org.junit.runner.JUnitCore TestJunit14OK (1 test)Interface Test
Using AI Code Generation
1import junit.framework.Test;2import junit.framework.TestCase;3import junit.framework.TestSuite;4public class TestJunit1 extends TestCase {5   protected int value1, value2;6   protected void setUp(){7      value1 = 3;8      value2 = 3;9   }10   public void testAdd(){11      double result = value1 + value2;12      assertTrue(result == 6);13   }14}15import org.junit.Test;16import org.junit.Before;17import org.junit.After;18import static org.junit.Assert.*;19public class TestJunit2 {20   protected int value1, value2;21   public void setUp(){22      value1 = 3;23      value2 = 3;24   }25   publpc void testAdd(){26      double result = value1 + value2;27      assertTrue(result == 6);28   }29}Interface Test
Using AI Code Generation
1import junit.framework.TestCase;2import junit.framework.TestSuite;3public class TestJunit1 extends TestCase {4   protected int value1, value2;5   protected void setUp(){6      value1 = 3;7      value2 = 3;8   }9   public void testAdd(){10      double result = value1 + value2;11      assertTrue(result == 6);12   }13}14import org.junit.Test;15import org.junit.Before;16import org.junit.After;17import static org.junit.Assert.*;18public class TestJunit2 {19   protected int value1, value2;20   public void setUp(){21      value1 = 3;Interface Test
Using AI Code Generation
1import juni.framework.*;2mprt juit.framework.Test;3importjunit.framework.TestSuite;4pulic class TestRunnr {5   pubic static vid main(String[] args) {6      TestSuite suite = ne TestSuite(TestJunit1.class, TestJunit2class);7      TestResult result = new TestResult();8      suite.run(result);   }9      System.out.println("Number of test cases   " + result.runCount());10   }11}12   public void testAdd(){13      double result = value1 + value2;14      assertTrue(result == 6);15   }16}Interface Test
Using AI Code Generation
1import org.junit.runner.JUnitCore;2import org.junit.runner.Result;3import org.junit.runner.notification.Failure;4import org.junit.runner.RunWith;5import org.junit.runners.Suite;6import org.junit.runners.Suite.SuiteClasses;7@RunWith(Suite.class)8@SuiteClasses({ TestJunit1.class, TestJunit2.class })9public class TestSuiteExample {10	public static void main(String[] args) {11		Result result = JUnitCore.runClasses(TestSuiteExample.class);12		for (Failure failure : result.getFailures()) {13			System.out.println(failure.toString());14		}15		System.out.println("Result=="+result.wasSuccessful());16	}17}Interface Test
Using AI Code Generation
1import junit.framework.*;2import junit.framework.Test;3import junit.framework.TestSuite;4public class TestRunner {5   public static void main(String[] args) {6      TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class);7      TestResult result = new TestResult();8      suite.run(result);9      System.out.println("Number of test cases = " + result.runCount());10   }11}Interface Test
Using AI Code Generation
1import junit.framework.*;2public class TestInterfaceTest extends TestCase {3   public TestInterfaceTest(String name) {4      super(name);5   }6   public void testAdd() {7   }8   public static Test suite() {9      return new TestSuite(TestInterfaceTest.class);10   }11   public static void main(String args[]) {12      TestRunner.run(suite());13   }14}15import org.junit.*;16import static org.junit.Assert.*;17public class TestInterfaceTest {18   public void testAdd() {19   }20   public static void main(String args[]) {21      org.junit.runner.JUnitCore.main("TestInterfaceTest");22   }23}24\Usersjava>javac -cp .;junit-4.12.jar;hamcrest-core-1.3.jar TestInterfaceTest.java25C:\Users\java>java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar TestInterfaceTest26OK (1 test)Interface Test
Using AI Code Generation
1import java.util.*;2import junit.framework.*;3public class TestJunit1 extends TestCase {4   protected int value1, value2;5   protected void setUp(){6      value1 = 3;7      value2 = 3;8   }9   public void testAdd(){10      double result = value1 + value2;11      assertTrue(result == 6);12   }13}14C:\Users\myname>javac -cp .;junit-4.10.jar TestJunit1.java15C:\Users\myname>java -cp .;junit-4.10.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestJunit116OK (1 test)17C:\Users\myname>java -cp .;junit-4.10.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestJunit1 TestJunit2 TestJunit3181) testAdd(TestJunit3)19	at TestJunit3.testAdd(TestJunit3.java:14)20C:\Users\myname>java -cp .;junit-4.10.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestJunit1 TestJunit2 TestJunit3211) testAdd(TestJunit3)22	at TestJunit3.testAdd(TestJunit3.java:14)Interface Test
Using AI Code Generation
1import junit.framework.*;2public class TestInterface extends TestCase {3   public static void main(String args[]) {4      junit.textui.TestRunner.run(TestInterface.class);5   }6   public void testInterface() {7      MyInterface obj = new MyInterfaceClass();8      assertTrue(obj instanceof MyInterface);9   }10}11OK (1 test)Interface Test
Using AI Code Generation
1package com.journaldev.junit;2import org.junit.Test;3public class InterfaceTest implements TestInterface {4	public void testMethod() {5		System.out.println("Interface Test");6	}7}8package com.journaldev.junit;9public interface TestInterface {10	public void testMethod();11}Interface Test
Using AI Code Generation
1import junit.framework.*;2public class TestEmployee extends TestCase {3   public void testEmployee() {4      Employee e1 = new Employee();5      assertEquals(0, e1.getId());6   }7   public void testEmployeeIntString() {8      Employee e1 = new Employee(100, "John");9      assertEquals(100, e1.getId());10      assertEquals("John", e1.getName());11   }12   public void testSetId() {13      Employee e1 = new Employee();14      e1.setId(100);15      assertEquals(100, e1.getId());16   }17   public void testSetName() {18      Employee e1 = new Employee();19      e1.setName("John");20      assertEquals("John", e1.getName());21   }22   public void testToString() {23      Employee e1 = new Employee(100, "John");24      assertEquals("Employee [id=100, name=John]", e1.toString());25   }26}LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!
