Examining JPython 
by Kirby W. Angell

Example 1:
(a)     
JPython 1.0.1 on javaJDK1.1.6_Borland
Copyright 1997-1998 Corporation for National Research Initiatives
>>>

(b)
>>> from java.math import BigInteger

(c)
>>> bi = BigInteger( "100" )
>>> bi
100

(d)
>>> bi2 = BigInteger( "200" )
>>> bi.max(bi2)
200

Example 2:
(a)
>>> from java.util import Date
>>> d = Date()
>>> d.hours
19
>>> d.hours = 10
>>> d.hours
10

(b)
>>> from java.util import Date
>>> d = Date()
>>> d.getHours()
19
>>> d.setHours(10)
>>> d.getHours()
10

Example 3:
C:\Sample2>jpython BigIntegerTest.py
19981102 22:51:21       BigIntegerTest  Test absolute value operator
19981102 22:51:22       BigIntegerTest  Test max operator
19981102 22:51:22       BigIntegerTest  Test string conversion

Example 4:
(a)
exec( "import %s" % script )
module = locals()[script]
# if this module supports our testing interface
if module.__dict__.has_key( "addTests" ):
module.addTests( ts )

(b)
def addTests( ts ):
    ts.add( BigIntegerTest( "BigIntegerTest" ) )


Listing One
01:  #
02:  # BigIntegerTest.py
03:  # Tests the standard java class java.math.BigInteger
04:  import sys
05:  from java.math import BigInteger
06:  print "Test the standard java BigInteger class."
07:  # Test string conversion
08:  bi = BigInteger( '100' )
09:  if bi.longValue() != 100:
10: print "BigInteger string conversion failed."
11: sys.exit(1)
12:  # Test max operator
13:  bi1 = BigInteger( '100' )
14:  bi2 = BigInteger( '200' )
15:  if bi1.max(bi2) != 200:
16: print "BigInteger max operator failed."
17: sys.exit(1)
18:  # Test absolute value operator
19:  bi = BigInteger( '-100' )
20:  if bi.abs() != 100:
21: print "BigInteger abs operator failed."
22: sys.exit(1)
23:  print "All tests completed."


Listing Two
01:  #
02:  # BigIntegerTest.py
03:  # Tests the standard java class java.math.BigInteger
04:  import sys
05:  from java.math import BigInteger
06:  from testcase import TestCase
07:  class BigIntegerTest(TestCase):
08: "Test the standard java BigInteger class."
09: def test_stringConversion(self):
10:        "Test string conversion"
11:     bi = BigInteger( '100' )
12:     self.assert_( bi.longValue() == 100 )
13: def test_maxOperator(self):
14:     "Test max operator"
15:     bi1 = BigInteger( '100' )
16:     bi2 = BigInteger( '200' )
17:     self.assert_( bi1.max(bi2) == 200 )
18: def test_abs(self):
19:     "Test absolute value operator"
20:     bi = BigInteger( '-100' )
21:     self.assert_( bi.abs() == 100 )
22: if __name__ == "__main__":
23:     try:
24:         c = BigIntegerTest( "BigIntegerTest" )
25:         c.runTest()
26:     except:
27:         pass


Listing Three
01:  import sys
02:  import traceback
03:  import os
04:  from time import *
05:  from string import *
06:  class TestCase:
07: "Base class for python based unit level and regression tests. \
08:  To use this class, implement the runTest, initialize, and reset  \
09:  methods.  Call the assert_ method to test for success."
10: def __init__(self, name):
11:     self.testName = name
12:     self.failed = 1
13:     self.failureReason = "<<Not Tested>>"
14: def runTest(self):
15:     "Run the test methods in this class"
16:     names = dir( self.__class__ )
17:     self.failed = 0
18:     self.failureReason = ""
19:     for name in names:
20:         # if this is a test method
21:         if name[:5] == "test_":
22:             self.initialize()
23:             try:
24:                 func = getattr( self, name )
25:                 self.writeMessage( func.__doc__ )
26:                 # Execute the test
27:                 func()
28:             finally:
29:                 self.reset()
30: def initialize(self):
31:     "Called before each test is run.  Should be overridden."
32:     pass
33: def reset(self):
34:     "Called after each test is run.  Should be overridden."
35:        pass
36: def failTest(self, reason):
37:     "Mark this test as having failed."
38:     self.failed = 1
39:     self.failureReason = reason
40: def writeMessage(self, message):
41:     "Writes a message to the standard output device."
42:     self.__writeMessage(sys.stdout, message )
43: def writeError(self, error):
44:     "Writes an error to the standard output device."
45:     self.__writeMessage( sys.stdout, "ERR " + error )
46: def writeWarning(self, warning):
47:     "Writes an warning to the standard output device."
48:     self.__writeMessage( sys.stdout, "WRN " + warning )
49: def __writeMessage(self, outFile, message):
50:     year, month, day, hour, minute, second, weekday, julian,\ 
51:       daylight = localtime(time())
52:     outFile.write( "%04d%02d%02d %02d:%02d:%02d\t" % (year,\
53:             month, day, hour, minute, second) )
54:     outFile.write( "%s\t%s\n" % (self.testName, message) )
55: def assert_(self, test):
56:     "Logs and generates an error if the test is false."
57:     if test == 0:
58:         stack = traceback.extract_stack()
59:         stackFrame = stack[len(stack)-2]
60:         file,line,method,call = stackFrame
61:         path,file = os.path.split(file)
62:         source = "%s %s %s %s" % (file, line, method, call)
63:         self.failTest( "Assertion failed: %s" % source )
64:         self.writeError( self.failureReason )
65:         raise AssertionError, source
    

1


