| java.lang.Object com.sun.midp.i3test.TestCase
All known Subclasses: javax.microedition.lcdui.TestCommandSorting, com.sun.midp.services.TestSystemServiceRequest, com.sun.midp.io.j2me.datagram.TestDatagramWouldBlock, javax.microedition.sip.TestPushUtils, com.sun.satsa.crypto.TestRC4, javax.microedition.sip.TestSipRefreshHelper, com.sun.midp.io.j2me.mms.TestMMSOpenClose, javax.microedition.lcdui.TestItemLFImplLDoInternalLayout, javax.microedition.rms.TestRecordStore, com.sun.j2me.payment.TestTransactionStore, com.sun.j2me.location.TestCriteria, com.sun.midp.security.TestPermissions, com.sun.midp.services.TestServiceRequestProtocol, javax.microedition.sip.TestSipConnectionNotifier, com.sun.midp.io.j2me.apdu.TestCad, com.sun.midp.links.TestLinkPortal, com.sun.midp.services.TestSystemServiceManager, javax.microedition.lcdui.game.TestSprite, com.sun.midp.links.TestKill, com.sun.midp.test.TestSemaphore, com.sun.midp.io.j2me.mms.TestMMSNumberOfSegments, com.sun.midp.io.TestHttpUrl, com.sun.midp.i3test.NamsTestCase, com.sun.midp.rms.TestFileRscLimit, com.sun.j2me.location.TestThreadSafety, com.sun.midp.links.TestNamedLinkPortal, com.sun.midp.io.j2me.sms.TestSMSOpenClose, com.sun.midp.appmanager.TestUserNotification, gov.nist.microedition.sip.TestSipDialogClient, gov.nist.microedition.sip.TestSipClientCancel, javax.microedition.sip.TestSipCommunication, com.sun.midp.content.ExtendedTestCase, javax.microedition.lcdui.TestStringItemNoLabelSizing, com.sun.satsa.pki.TestPKI, com.sun.midp.io.j2me.http.TestHttpHeaders, com.sun.midp.links.TestEcho, com.sun.midp.links.TestLinkMessage, gov.nist.microedition.sip.TestSipClientSetHeader, javax.microedition.lcdui.CustomItemTest, com.sun.midp.suspend.TestDummy, com.sun.j2me.location.TestLocationProvider, javax.microedition.sip.TestOriginatingInvite, javax.microedition.lcdui.DisplayManagerTest, javax.microedition.sip.TestMultiNotifiers, com.sun.j2me.location.TestLandmarkStore, com.sun.midp.i3test.TestUtfReaders, com.sun.midp.suspend.TestConnectionSuspend, com.sun.midp.chameleon.input.TestNativeInputMode, com.sun.midp.io.j2me.cbs.TestCBSOpenClose, javax.microedition.sip.TestSubscribeNotify, javax.microedition.sip.TestMessageAttachHeader, com.sun.midp.io.j2me.storage.TestFileSystem, com.sun.midp.main.TestMIDletControllerEventProducer, com.sun.midp.services.TestSystemService, com.sun.midp.io.j2me.cbs.TestCBS, javax.microedition.sip.TestSipAddress, javax.microedition.sip.SipHeaderBaseTest, com.sun.midp.links.TestLink, com.sun.midp.i3test.TestCompWildcard, com.sun.midp.content.TestRegReadWrite, com.sun.midp.links.TestLinkTransfer, javax.microedition.lcdui.TestKeyConverter, javax.microedition.lcdui.TestOne, javax.microedition.lcdui.TestTextFieldInput, javax.microedition.global.TestStringComparator, javax.microedition.lcdui.TestIsShown, javax.microedition.lcdui.Test6254765, com.sun.midp.io.j2me.socket.TestNonblockingSocket, javax.microedition.lcdui.TestDisplayPreempt, javax.microedition.sip.TestRegisterTCP, com.sun.midp.links.TestMultiple, com.sun.midp.lcdui.TestDisplayFactory, javax.microedition.lcdui.TestThree, com.sun.midp.automation.TestSwitchMidlets, javax.microedition.lcdui.TestNewSetCurrent, com.sun.midp.links.TestRing, com.sun.cardreader.TestGemplus1, com.sun.midp.test.TestReflect, javax.microedition.global.TestResourceManager, com.sun.midp.util.FailTest, com.sun.midp.io.j2me.storage.TestRandomAccessStream, com.sun.midp.io.j2me.mms.TestMMSParseAddress, com.sun.midp.events.TestHandleFatalError, com.sun.cardreader.TestSlotFactory, com.sun.midp.i3test.TestIsolateSynch, javax.microedition.lcdui.TestTwo, com.sun.midp.automation.TestAutoSuiteStorage, javax.microedition.lcdui.TestCanvasSizing, com.sun.midp.io.j2me.jcrmi.DataTest, com.sun.midp.events.TestNativeEventPool, com.sun.midp.io.j2me.sms.TestSMS, com.sun.midp.services.TestSystemServiceConnectionListener, gov.nist.microedition.sip.TestSipClientConnection, com.sun.midp.events.TestEventQueue, javax.microedition.global.TestFormatter, gov.nist.microedition.sip.TestSipClientInitNotifyReq, gov.nist.microedition.sip.TestSipDialogServer, com.sun.midp.links.TestTransfer, com.sun.midp.io.j2me.socket.TestPushRegistry, javax.microedition.lcdui.TestStringItemSizing, com.sun.j2me.location.TestLandmark, javax.microedition.lcdui.TestSizeChanged, javax.microedition.sip.TestPrack, com.sun.satsa.utils.TestIA5, javax.microedition.lcdui.TestGraphicsClipping, com.sun.midp.io.j2me.socket.TestInterruptedIO, javax.microedition.io.TestResourceLimit, com.sun.midp.io.j2me.mms.TestMMSSendReceive,
TestCase | abstract public class TestCase (Code) | | The main class for writing integrated internal interface (i3) tests. Tests
should create a subclass of TestCase and implement the runTests() method.
This method is responsible for running all of the tests. Tests consist of
a test declaration followed by one or more assertions. The declaration is
a call to the declare() method, which simply establishes a name for the set
of assertions to follow. The assertions are made through calls to the
assert*() family of methods. The framework considers it an error if any
assertions are made prior to a call to declare(), or if there is a call to
declare() with no subsequent calls to assert().
The TestCase class and its assert() methods are loosely based on the
JUnit TestCase class. JUnit uses reflection to find test methods. Since
reflection isn't present in CLDC, we need to have a runTests() method that
calls them explicitly.
JUnit, in contrast to other test frameworks, doesn't return a result
from each test. Instead, each test simply makes assertions, and only
assertions that fail are flagged. The i3 tests follow the JUnit approach.
For this reason, there is no pass() method one might expect to see
corresponding to the fail() method.
While it's not a requirement, it's conventional for the TestCase
subclass to reside in the same package as the code under test. This will
allow the tests access to package-private variables and methods.
Each of the different assert() calls has an optional
message parameter. This message is emitted only when the
assertion fails and should be written with this in mind.
For "negative tests" that require an exception to be thrown, a suggested
style is to record the fact that the correct exception was thrown in a
boolean variable. This variable can then be tested using one of the
assert() calls. This is probably preferable to placing calls to fail() in
the midst of the test method. See testThree() in the example below.
The framework will catch any Throwable thrown by the tests and will log
a message and count the occurrence as an error. This allows tests to be
written more simply, because they can rely on the framework to catch
everything. In support of this, the runTests() method has been defined with
a 'throws Throwable' clause.
A general rule is that tests should run independently of each other.
That is, tests should not leave any side effects that other tests rely on
or that could cause them to fail. Typically, tests should create fresh
objects for testing instead of reusing objects from other tests. If a test
has any external side effects, such as creating threads or opening files,
the test should be coded with a 'finally' clause that attempts to clean up
after itself. See testFour() in the example below.
Example:
import com.sun.midp.i3test;
package java.lang;
public class SampleTest extends TestCase {
void testOne() {
int i = 1 + 1;
assertEquals("integer addition failed", 2, i);
}
void testTwo() {
Object x = new Object();
assertNotNull("Object constructor returned null", x);
}
void testThree() {
StringBuffer sb;
boolean caught = false;
try {
sb = new StringBuffer(-1);
} catch (NegativeArraySizeException e) {
caught = true;
}
assertTrue("no exception thrown", caught);
}
void testFour() {
Thread thr = new Thread(...).start();
try {
// ...
} finally {
thr.interrupt();
}
}
public void runTests() {
declare("testOne");
testOne();
declare("testTwo");
testTwo();
declare("testThree");
testThree();
declare("testFour");
testFour();
}
}
|
Constructor Summary | |
public | TestCase() Constructs a TestCase. |
Method Summary | |
public void | assertEquals(Object expected, Object actual) Asserts that two objects are equal according to the equals() method. | public void | assertEquals(String message, Object expected, Object actual) Asserts that two objects are equal according to the equals() method. | public void | assertEquals(int expected, int actual) Asserts that two integer values are equal. | public void | assertEquals(String message, int expected, int actual) Asserts that two integer values are equal. | public void | assertFalse(boolean condition) Asserts that condition is false. | public void | assertFalse(String message, boolean condition) Asserts that condition is false. | public void | assertNotNull(Object object) Asserts that the object reference is not null. | public void | assertNotNull(String message, Object object) Asserts that the object reference is not null. | public void | assertNotSame(Object expected, Object actual) Asserts that two references do not point to the same object,
using the == operator. | public void | assertNotSame(String message, Object expected, Object actual) Asserts that two references do not point to the same object,
using the == operator. | public void | assertNull(Object object) Asserts that the object reference is null. | public void | assertNull(String message, Object object) Asserts that the object reference is null. | public void | assertSame(Object expected, Object actual) Asserts that two references point to the same object, using the
== operator. | public void | assertSame(String message, Object expected, Object actual) Asserts that two references point to the same object, using the
== operator. | public void | assertTrue(String message, boolean condition) Tests the assertion that the boolean condition is true.
If the condition is true, this method simply updates some statistics
and returns. | public void | assertTrue(boolean condition) Asserts that condition is true. | static void | cleanup() Cleanup after running a test. | static String | currentTestCaseName() Return the name of the current test case. | public void | declare(String testName) Declares that the set of assertions that follow this call belong to a
test named testName . | public void | fail() Signals an unconditional assertion failure. | public void | fail(String message) Signals an unconditional assertion failure. | protected SecurityToken | getSecurityToken() Gets the system's internal security token. | protected boolean | getVerbose() Reads the verbose flag. | public void | info(String s) If in verbose mode, print information. | static void | p(String s) Print the string. | static void | perror(int nerr, String errstr) Print the number of errors with the category. | static void | report() | static void | reset() Reset the counters of all errors and exceptions. | static void | runTestCase(String testCaseName) Run the named test case. | abstract public void | runTests() Runs all the tests for this TestCase. | static void | setVerbose(boolean v) Set the verbose output flag. |
currentNumAsserts | static int currentNumAsserts(Code) | | |
errorAssertWithoutTest | static int errorAssertWithoutTest(Code) | | |
errorClassNotFound | static int errorClassNotFound(Code) | | |
errorConstructorException | static int errorConstructorException(Code) | | |
errorNoTests | static int errorNoTests(Code) | | |
errorNotTestCase | static int errorNotTestCase(Code) | | |
errorTestRunThrows | static int errorTestRunThrows(Code) | | |
errorTestWithoutAssert | static int errorTestWithoutAssert(Code) | | |
tokenEnabled | static boolean tokenEnabled(Code) | | |
totalAsserts | static int totalAsserts(Code) | | |
totalCases | static int totalCases(Code) | | |
totalFailures | static int totalFailures(Code) | | |
totalTests | static int totalTests(Code) | | |
verbose | static boolean verbose(Code) | | |
TestCase | public TestCase()(Code) | | Constructs a TestCase. Since TestCase is an abstract class, this
constructor is called only at the time a TestCase subclass is being
constructed.
|
assertEquals | public void assertEquals(Object expected, Object actual)(Code) | | Asserts that two objects are equal according to the equals() method.
Parameters: expected - an object containing the expected value Parameters: actual - an object containing the actual value |
assertEquals | public void assertEquals(String message, Object expected, Object actual)(Code) | | Asserts that two objects are equal according to the equals() method.
Two null references are considered to be equal.
Parameters: message - the message to be emitted if the assertion fails Parameters: expected - an object containing the expected value Parameters: actual - an object containing the actual value |
assertEquals | public void assertEquals(int expected, int actual)(Code) | | Asserts that two integer values are equal.
Parameters: expected - the expected value Parameters: actual - the actual value |
assertEquals | public void assertEquals(String message, int expected, int actual)(Code) | | Asserts that two integer values are equal.
Parameters: message - the message to be emitted if the assertion fails Parameters: expected - the expected value Parameters: actual - the actual value |
assertFalse | public void assertFalse(boolean condition)(Code) | | Asserts that condition is false.
Parameters: condition - the condition asserted to be false |
assertFalse | public void assertFalse(String message, boolean condition)(Code) | | Asserts that condition is false.
Parameters: message - the message to be emitted if the assertion fails Parameters: condition - the condition asserted to be false |
assertNotNull | public void assertNotNull(Object object)(Code) | | Asserts that the object reference is not null.
Parameters: object - the reference to be tested |
assertNotNull | public void assertNotNull(String message, Object object)(Code) | | Asserts that the object reference is not null.
Parameters: message - the message to be emitted if the assertion fails Parameters: object - the reference to be tested |
assertNotSame | public void assertNotSame(Object expected, Object actual)(Code) | | Asserts that two references do not point to the same object,
using the == operator.
Parameters: expected - a reference to the expected object Parameters: actual - a reference to the actual object |
assertNotSame | public void assertNotSame(String message, Object expected, Object actual)(Code) | | Asserts that two references do not point to the same object,
using the == operator.
Parameters: message - the message to be emitted if the assertion fails Parameters: expected - a reference to the expected object Parameters: actual - a reference to the actual object |
assertNull | public void assertNull(Object object)(Code) | | Asserts that the object reference is null.
Parameters: object - the reference to be tested |
assertNull | public void assertNull(String message, Object object)(Code) | | Asserts that the object reference is null.
Parameters: message - the message to be emitted if the assertion fails Parameters: object - the reference to be tested |
assertSame | public void assertSame(Object expected, Object actual)(Code) | | Asserts that two references point to the same object, using the
== operator.
Parameters: expected - a reference to the expected object Parameters: actual - a reference to the actual object |
assertSame | public void assertSame(String message, Object expected, Object actual)(Code) | | Asserts that two references point to the same object, using the
== operator.
Parameters: message - the message to be emitted if the assertion fails Parameters: expected - a reference to the expected object Parameters: actual - a reference to the actual object |
assertTrue | public void assertTrue(String message, boolean condition)(Code) | | Tests the assertion that the boolean condition is true.
If the condition is true, this method simply updates some statistics
and returns. If the condition is false, the failure is noted and the
message is emitted, along with an indication of the current TestCase
and the name of the test currently being run. The
message string should be phrased in a way that makes sense
when the test fails. See the example in the class documentation.
Parameters: message - the message to be emitted if the assertion fails Parameters: condition - the condition asserted to be true |
assertTrue | public void assertTrue(boolean condition)(Code) | | Asserts that condition is true.
Parameters: condition - the condition to be tested |
cleanup | static void cleanup()(Code) | | Cleanup after running a test.
|
currentTestCaseName | static String currentTestCaseName()(Code) | | Return the name of the current test case.
the name of the class for the current test case. |
declare | public void declare(String testName)(Code) | | Declares that the set of assertions that follow this call belong to a
test named testName . The framework considers it an error
if no calls to any of the assert() methods follow a call to declare().
Parameters: testName - the name of the test |
fail | public void fail()(Code) | | Signals an unconditional assertion failure.
|
fail | public void fail(String message)(Code) | | Signals an unconditional assertion failure.
Parameters: message - the message to be emitted, explaining the failure |
getSecurityToken | protected SecurityToken getSecurityToken()(Code) | | Gets the system's internal security token. This is useful for testing
sensitive interfaces that require a security token parameter. This
returns a valid security token only when called within the context of a
call to runTests() inside a TestCase. At other times it will throw a
SecurityException.
the internal security token throws: SecurityException - if not called from within runTests() |
getVerbose | protected boolean getVerbose()(Code) | | Reads the verbose flag.
true if in verbose mode |
info | public void info(String s)(Code) | | If in verbose mode, print information.
|
p | static void p(String s)(Code) | | Print the string.
Parameters: s - the string to print on a new line. |
perror | static void perror(int nerr, String errstr)(Code) | | Print the number of errors with the category.
Parameters: nerr - the number of errors in the category Parameters: errstr - the description of the errors |
report | static void report()(Code) | | |
reset | static void reset()(Code) | | Reset the counters of all errors and exceptions.
|
runTestCase | static void runTestCase(String testCaseName)(Code) | | Run the named test case.
Parameters: testCaseName - the class name of the test case |
runTests | abstract public void runTests() throws Throwable(Code) | | Runs all the tests for this TestCase. This is an abstract method that
must be implemented by the subclass. The implementation of this method
should run all of the tests provided in this TestCase. This method can
also be used to initialize and tear down any state that might need to
be shared among all the tests.
A suggested organization is to have runTests() alternate calling
declare() and an internal method that implements the test. One could
write the declare() method as part of the internal test method, but
consolidating all calls in runTests() makes it a little easier to
ensure that each test has its own call to declare(). See the example
in the class documentation.
|
setVerbose | static void setVerbose(boolean v)(Code) | | Set the verbose output flag.
Parameters: v - true to enable verbose output |
|
|