Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Development/Testing/StrawManProposal"
(→[http://en.wikipedia.org/wiki/Hollywood_Principle The Hollywood Principle]) |
|||
(15 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
** Eclipse IDE has built-in support for JUnit4 tests | ** Eclipse IDE has built-in support for JUnit4 tests | ||
** while various testing frameworks have been extended, JUnit4 is extensible '''by design''' | ** while various testing frameworks have been extended, JUnit4 is extensible '''by design''' | ||
+ | * [[../Info on JUnit4 |More information on JUnit4]] | ||
Looking at requirements from MOXy/SDO/DBWS/JPA, this is what we've got so far ... | Looking at requirements from MOXy/SDO/DBWS/JPA, this is what we've got so far ... | ||
Line 39: | Line 40: | ||
There are two things injected by the <tt>TestContextRunner</tt>: | There are two things injected by the <tt>TestContextRunner</tt>: | ||
− | + | ||
− | + | 1. Properties | |
+ | * found in the <tt>ntf.xml</tt> file in the user's home directory: <code> | ||
<pre> | <pre> | ||
− | <?xml version="1.0" encoding="UTF-8"?> | + | <?xml version="1.0" encoding="UTF-8"?> |
− | <ntf> | + | <ntf> |
− | + | <properties> | |
− | + | <property | |
− | + | name="easy_as">pie</property> | |
− | + | </properties> | |
</pre> | </pre> | ||
− | + | </code> | |
− | + | the name and location of the <tt>ntf.xml</tt> file can be altered by the Java System properties <tt>-Dntf.file</tt>=''some_other_file.name'' and <tt>-Dntf.dir</tt>=''some/directory/path'' | |
− | + | * Java System properties | |
− | + | 2. User-defined context object | |
+ | * an external factory constructs an object that implements <tt>Context</tt>, providing a simple API to look up objects by name: | ||
<font color="#000000">'''<font color="#7f0055">public</font>''' '''<font color="#7f0055">interface</font>''' Context<V> { | <font color="#000000">'''<font color="#7f0055">public</font>''' '''<font color="#7f0055">interface</font>''' Context<V> { | ||
Line 62: | Line 65: | ||
} | } | ||
</font> | </font> | ||
+ | |||
+ | The rest of the <tt>ntf.xml</tt> document describes the factories used to build the <tt>Context</tt> objects: | ||
+ | <code> | ||
+ | <pre> | ||
+ | |||
+ | <test | ||
+ | class="org.eclipse.persistence.ntf.junittests.ASetOfSimpleTests" | ||
+ | > | ||
+ | <context | ||
+ | tag="context" | ||
+ | class="org.eclipse.persistence.ntf.junittests.SimpleContext">barf</context> | ||
+ | <context | ||
+ | tag="context2" | ||
+ | class="org.eclipse.persistence.ntf.junittests.DocumentContext">c:/temp/foo.xml</context> | ||
+ | </test> | ||
+ | </pre> | ||
+ | </code> | ||
+ | |||
+ | An example set of tests: | ||
+ | |||
+ | <font color="#000000">'''<font color="#7f0055">package</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">eclipse</font><font color="#7f0055">.</font><font color="#7f0055">persistence</font><font color="#7f0055">.</font><font color="#7f0055">ntf</font><font color="#7f0055">.</font><font color="#7f0055">junittests</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | |||
+ | <font color="#3f7f59">// javase imports</font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> java</font><font color="#7f0055">.</font><font color="#7f0055">util</font><font color="#7f0055">.</font><font color="#7f0055">Properties</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">w3c</font><font color="#7f0055">.</font><font color="#7f0055">dom</font><font color="#7f0055">.</font><font color="#7f0055">Document</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | |||
+ | <font color="#3f7f59">// JUnit imports</font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Before</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">BeforeClass</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Ignore</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Test</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">runner</font><font color="#7f0055">.</font><font color="#7f0055">RunWith</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> static org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Assert</font><font color="#7f0055">.</font><font color="#7f0055">assertFalse</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> static org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Assert</font><font color="#7f0055">.</font><font color="#7f0055">assertNotNull</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> static org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Assert</font><font color="#7f0055">.</font><font color="#7f0055">assertTrue</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | |||
+ | <font color="#3f7f59">// ntf imports</font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">eclipse</font><font color="#7f0055">.</font><font color="#7f0055">persistence</font><font color="#7f0055">.</font><font color="#7f0055">ntf</font><font color="#7f0055">.</font><font color="#7f0055">TestContext</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">eclipse</font><font color="#7f0055">.</font><font color="#7f0055">persistence</font><font color="#7f0055">.</font><font color="#7f0055">ntf</font><font color="#7f0055">.</font><font color="#7f0055">TestContextRunner</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">eclipse</font><font color="#7f0055">.</font><font color="#7f0055">persistence</font><font color="#7f0055">.</font><font color="#7f0055">ntf</font><font color="#7f0055">.</font><font color="#7f0055">TestProperties</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">eclipse</font><font color="#7f0055">.</font><font color="#7f0055">persistence</font><font color="#7f0055">.</font><font color="#7f0055">ntf</font><font color="#7f0055">.</font><font color="#7f0055">Context</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">eclipse</font><font color="#7f0055">.</font><font color="#7f0055">persistence</font><font color="#7f0055">.</font><font color="#7f0055">ntf</font><font color="#7f0055">.</font><font color="#7f0055">TestGroups</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | '''<font color="#7f0055">import</font>'''<font color="#7f0055"> static org</font><font color="#7f0055">.</font><font color="#7f0055">eclipse</font><font color="#7f0055">.</font><font color="#7f0055">persistence</font><font color="#7f0055">.</font><font color="#7f0055">ntf</font><font color="#7f0055">.</font><font color="#7f0055">IgnoreAssertion</font><font color="#7f0055">.</font><font color="#7f0055">ignore</font><font color="#7f0055"><nowiki>;</nowiki></font> | ||
+ | |||
+ | @RunWith(TestContextRunner.class) | ||
+ | @TestGroups({<font color="#2a00ff">"FirstGroup"</font>, <font color="#2a00ff">"SecondGroup"</font>}) | ||
+ | '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">class</font>''' ASetOfSimpleTests { | ||
+ | |||
+ | @TestProperties '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">static</font>''' '''<font color="#7f0055">Properties</font>''' properties; | ||
+ | @TestContext(tag=<font color="#2a00ff">"context"</font>) '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">static</font>''' Context<'''<font color="#7f0055">Object</font>'''> context; | ||
+ | @TestContext(tag=<font color="#2a00ff">"context2"</font>) '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">static</font>''' Context<Document> context2; | ||
+ | |||
+ | @BeforeClass | ||
+ | '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">static</font>''' '''<font color="#7f0055">void</font>''' setUp() { | ||
+ | '''<font color="#7f0055">System</font>'''.identityHashCode('''<font color="#7f0055">System</font>'''.out); | ||
+ | } | ||
+ | |||
+ | @Before | ||
+ | '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">void</font>''' something() { | ||
+ | '''<font color="#7f0055">System</font>'''.identityHashCode('''<font color="#7f0055">System</font>'''.out); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">void</font>''' aSimpleTest() { | ||
+ | assertTrue(<font color="#2a00ff">"no key 'java.version'"</font>, properties.containsKey(<font color="#2a00ff">"java.version"</font>)); | ||
+ | assertNotNull(<font color="#2a00ff">"object @ 'some_key' is null"</font>, context.getObject(<font color="#2a00ff">"some_key"</font>)); | ||
+ | assertTrue(<font color="#2a00ff">"object @ 'controlDocument' is not a org.w3c.dom.Document"</font>, | ||
+ | context2.getObject(<font color="#2a00ff">"controlDocument"</font>) '''<font color="#7f0055">instanceof</font>''' Document); | ||
+ | } | ||
+ | |||
+ | @Test | ||
+ | '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">void</font>''' anotherSimpleTest() { | ||
+ | ignore(<font color="#2a00ff">"whatever!"</font>, '''<font color="#7f0055">true</font>''' == '''<font color="#7f0055">true</font>'''); | ||
+ | } | ||
+ | |||
+ | @Ignore | ||
+ | '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">void</font>''' yetAnotherSimpleTest() { | ||
+ | assertFalse(<font color="#2a00ff">"whatever!"</font>, properties.containsKey(<font color="#2a00ff">"java.version"</font>)); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </font> | ||
+ | |||
+ | == What would it look like in real-life? == | ||
+ | A sample of converted tests can be found at ... |
Latest revision as of 16:46, 4 December 2007
From the EclipseLink DevMeeting 071017, create a straw-man proposal for a new consolidated testing framework
New Testing Framework
- based on JUnit4 - Why?
- JUnit3 is at end-of-life
- Eclipse IDE has built-in support for JUnit4 tests
- while various testing frameworks have been extended, JUnit4 is extensible by design
- More information on JUnit4
Looking at requirements from MOXy/SDO/DBWS/JPA, this is what we've got so far ...
The Hollywood Principle
Don't call us, we'll call you The basic idea is to de-couple the tests from their resource setup requirements that currently are handled through Java inheritance (not the best for component re-use). The NTF instead injects the resource into a tagged context variable owned by the test. Setup for the context is done in a separate class with its own requirements for composition/aggregation/inheritance.
// javase imports import java.util.Properties; // JUnit imports import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; // ntf imports import org.eclipse.persistence.ntf.TestContext; import org.eclipse.persistence.ntf.TestContextRunner; import org.eclipse.persistence.ntf.TestProperties; import org.eclipse.persistence.ntf.Context; @RunWith(TestContextRunner.class) public class ASetOfSimpleTests { @TestProperties public static Properties properties; @TestContext(tag="context") public static Context<Object> context;
There are two things injected by the TestContextRunner:
1. Properties
- found in the ntf.xml file in the user's home directory:
<?xml version="1.0" encoding="UTF-8"?> <ntf> <properties> <property name="easy_as">pie</property> </properties>
the name and location of the ntf.xml file can be altered by the Java System properties -Dntf.file=some_other_file.name and -Dntf.dir=some/directory/path
- Java System properties
2. User-defined context object
- an external factory constructs an object that implements Context, providing a simple API to look up objects by name:
public interface Context<V> { public boolean containsObject(String objectName); public V getObject(String objectName); }
The rest of the ntf.xml document describes the factories used to build the Context objects:
<test class="org.eclipse.persistence.ntf.junittests.ASetOfSimpleTests" > <context tag="context" class="org.eclipse.persistence.ntf.junittests.SimpleContext">barf</context> <context tag="context2" class="org.eclipse.persistence.ntf.junittests.DocumentContext">c:/temp/foo.xml</context> </test>
An example set of tests:
package org.eclipse.persistence.ntf.junittests; // javase imports import java.util.Properties; import org.w3c.dom.Document; // JUnit imports import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; // ntf imports import org.eclipse.persistence.ntf.TestContext; import org.eclipse.persistence.ntf.TestContextRunner; import org.eclipse.persistence.ntf.TestProperties; import org.eclipse.persistence.ntf.Context; import org.eclipse.persistence.ntf.TestGroups; import static org.eclipse.persistence.ntf.IgnoreAssertion.ignore; @RunWith(TestContextRunner.class) @TestGroups({"FirstGroup", "SecondGroup"}) public class ASetOfSimpleTests { @TestProperties public static Properties properties; @TestContext(tag="context") public static Context<Object> context; @TestContext(tag="context2") public static Context<Document> context2; @BeforeClass public static void setUp() { System.identityHashCode(System.out); } @Before public void something() { System.identityHashCode(System.out); } @Test public void aSimpleTest() { assertTrue("no key 'java.version'", properties.containsKey("java.version")); assertNotNull("object @ 'some_key' is null", context.getObject("some_key")); assertTrue("object @ 'controlDocument' is not a org.w3c.dom.Document", context2.getObject("controlDocument") instanceof Document); } @Test public void anotherSimpleTest() { ignore("whatever!", true == true); } @Ignore public void yetAnotherSimpleTest() { assertFalse("whatever!", properties.containsKey("java.version")); } }
What would it look like in real-life?
A sample of converted tests can be found at ...