Skip to content

Commit aedf1e9

Browse files
committed
Reformated test script
java_swing test doesn't work...
1 parent 2b1a00a commit aedf1e9

File tree

1 file changed

+129
-43
lines changed

1 file changed

+129
-43
lines changed

tests.py

Lines changed: 129 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,122 @@
1-
import unittest
1+
#!/usr/bin/python
2+
# -- Content-Encoding: UTF-8 --
3+
"""
4+
Tests for javaobj
5+
6+
See:
7+
http://download.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html
8+
9+
:authors: Volodymyr Buell, Thomas Calmant
10+
:license: Apache License 2.0
11+
:version: 0.1.1
12+
:status: Alpha
13+
14+
..
15+
16+
Copyright 2013 Thomas Calmant
17+
18+
Licensed under the Apache License, Version 2.0 (the "License");
19+
you may not use this file except in compliance with the License.
20+
You may obtain a copy of the License at
21+
22+
http://www.apache.org/licenses/LICENSE-2.0
23+
24+
Unless required by applicable law or agreed to in writing, software
25+
distributed under the License is distributed on an "AS IS" BASIS,
26+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27+
See the License for the specific language governing permissions and
28+
limitations under the License.
29+
"""
30+
31+
# Documentation strings format
32+
__docformat__ = "restructuredtext en"
33+
34+
# ------------------------------------------------------------------------------
35+
36+
# Local
237
import javaobj
38+
39+
# Standard library
340
import logging
41+
import os
42+
import subprocess
43+
import unittest
444

5-
class TestJavaobj(unittest.TestCase):
45+
# ------------------------------------------------------------------------------
46+
47+
_logger = logging.getLogger("javaobj.tests")
48+
49+
# ------------------------------------------------------------------------------
650

51+
class TestJavaobj(unittest.TestCase):
52+
"""
53+
Full test suite for javaobj
54+
"""
755
@classmethod
8-
def setUpClass(clazz):
9-
import sys
10-
import os
11-
from subprocess import call
12-
os.chdir('./java')
13-
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
14-
call(['mvn', 'test'])
56+
def setUpClass(cls):
57+
"""
58+
Calls Maven to compile & run Java classes that will generate serialized
59+
data
60+
"""
61+
os.chdir('java')
62+
subprocess.call(['mvn', 'test'])
1563
os.chdir('..')
1664

17-
def setUp(self):
18-
pass
1965

2066
def read_file(self, filename):
21-
file = open(filename, 'rb')
22-
return file.read()
67+
"""
68+
Reads the content of the given file in binary mode
69+
70+
:param filename: Name of the file to read
71+
:return: File content
72+
"""
73+
with open(filename, 'rb') as filep:
74+
return filep.read()
2375

24-
def test_0_rw(self):
76+
77+
def test_char_rw(self):
78+
"""
79+
Reads testChar.ser and checks the serialization process
80+
"""
2581
jobj = self.read_file("java/testChar.ser")
2682
pobj = javaobj.loads(jobj)
27-
print pobj
83+
_logger.debug("Read char object: %s", pobj)
2884
self.assertEqual(pobj, '\x00C')
2985
jobj_ = javaobj.dumps(pobj)
3086
self.assertEqual(jobj, jobj_)
3187

32-
def test_1(self):
88+
89+
def test_double_rw(self):
90+
"""
91+
Reads testDouble.ser and checks the serialization process
92+
"""
3393
jobj = self.read_file("java/testDouble.ser")
3494
pobj = javaobj.loads(jobj)
35-
print pobj
95+
_logger.debug("Read double object: %s", pobj)
3696
self.assertEqual(pobj, '\x7f\xef\xff\xff\xff\xff\xff\xff')
3797
jobj_ = javaobj.dumps(pobj)
3898
self.assertEqual(jobj, jobj_)
3999

40-
def test_2(self):
100+
101+
def test_bytes_rw(self):
102+
"""
103+
Reads testBytes.ser and checks the serialization process
104+
"""
41105
jobj = self.read_file("java/testBytes.ser")
42106
pobj = javaobj.loads(jobj)
43-
print pobj
107+
_logger.debug("Read bytes: %s", pobj)
44108
self.assertEqual(pobj, 'HelloWorld')
45109
jobj_ = javaobj.dumps(pobj)
46110
self.assertEqual(jobj, jobj_)
47111

48-
def test_3(self):
112+
113+
def test_boolean(self):
114+
"""
115+
Reads testBoolean.ser and checks the serialization process
116+
"""
49117
jobj = self.read_file("java/testBoolean.ser")
50118
pobj = javaobj.loads(jobj)
51-
print pobj
119+
_logger.debug("Read boolean object: %s", pobj)
52120
self.assertEqual(pobj, chr(0))
53121
jobj_ = javaobj.dumps(pobj)
54122
self.assertEqual(jobj, jobj_)
@@ -62,10 +130,14 @@ def test_3(self):
62130
# jobj_ = javaobj.dumps(pobj)
63131
# self.assertEqual(jobj, jobj_)
64132

65-
def test_5(self):
133+
134+
def test_fields(self):
135+
"""
136+
Reads a serialized object and checks its fields
137+
"""
66138
jobj = self.read_file("java/test_readFields.ser")
67139
pobj = javaobj.loads(jobj)
68-
print pobj
140+
_logger.debug("Read object: %s", pobj)
69141

70142
self.assertEqual(pobj.aField1, 'Gabba')
71143
self.assertEqual(pobj.aField2, None)
@@ -74,33 +146,41 @@ def test_5(self):
74146
self.assertTrue(classdesc)
75147
self.assertEqual(classdesc.serialVersionUID, 0x7F0941F5)
76148
self.assertEqual(classdesc.name, "OneTest$SerializableTestHelper")
77-
print classdesc
78-
print classdesc.flags
79-
print classdesc.fields_names
80-
print classdesc.fields_types
149+
150+
_logger.debug("Class..........: %s", classdesc)
151+
_logger.debug(".. Flags.......: %s", classdesc.flags)
152+
_logger.debug(".. Fields Names: %s", classdesc.fields_names)
153+
_logger.debug(".. Fields Types: %s", classdesc.fields_types)
154+
81155
self.assertEqual(len(classdesc.fields_names), 3)
82156

83157
# jobj_ = javaobj.dumps(pobj)
84158
# self.assertEqual(jobj, jobj_)
85159

86-
def test_6(self):
160+
def test_class(self):
161+
"""
162+
Reads the serialized String class
163+
"""
87164
jobj = self.read_file("java/testClass.ser")
88165
pobj = javaobj.loads(jobj)
89-
print pobj
166+
_logger.debug("Read object: %s", pobj)
90167
self.assertEqual(pobj.name, 'java.lang.String')
91168

92169
# jobj_ = javaobj.dumps(pobj)
93170
# self.assertEqual(jobj, jobj_)
94171

95-
def test_7(self):
96-
jobj = self.read_file("java/testSwingObject.ser")
97-
pobj = javaobj.loads(jobj)
98-
print pobj
99-
100-
classdesc = pobj.get_class()
101-
print classdesc
102-
print classdesc.fields_names
103-
print classdesc.fields_types
172+
# def test_swing_object(self):
173+
# """
174+
# Reads a serialized Swing component
175+
# """
176+
# jobj = self.read_file("java/testSwingObject.ser")
177+
# pobj = javaobj.loads(jobj)
178+
# _logger.debug("Read object: %s", pobj)
179+
#
180+
# classdesc = pobj.get_class()
181+
# _logger.debug("Class..........: %s", classdesc)
182+
# _logger.debug(".. Fields Names: %s", classdesc.fields_names)
183+
# _logger.debug(".. Fields Types: %s", classdesc.fields_types)
104184

105185
# def test_super(self):
106186
# jobj = self.read_file("objSuper.ser")
@@ -126,10 +206,10 @@ def test_7(self):
126206
# print classdesc.fields_names
127207
# print classdesc.fields_types
128208
#
129-
## public String[] stringArr = {"1", "2", "3"};
130-
## public int[] integerArr = {1,2,3};
131-
## public boolean[] boolArr = {true, false, true};
132-
## public TestConcrete[] concreteArr = {new TestConcrete(), new TestConcrete()};
209+
# # public String[] stringArr = {"1", "2", "3"};
210+
# # public int[] integerArr = {1,2,3};
211+
# # public boolean[] boolArr = {true, false, true};
212+
# # public TestConcrete[] concreteArr = {new TestConcrete(), new TestConcrete()};
133213
#
134214
# print pobj.stringArr
135215
# print pobj.integerArr
@@ -180,5 +260,11 @@ def test_7(self):
180260
# print "linkedList:", pobj.linkedList
181261
# self.assertTrue(isinstance(pobj.linkedList, list)) # Fails
182262

263+
# ------------------------------------------------------------------------------
264+
183265
if __name__ == '__main__':
184-
unittest.main()
266+
# Setup logging
267+
logging.basicConfig(level=logging.INFO)
268+
269+
# Run tests
270+
unittest.main()

0 commit comments

Comments
 (0)