1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#!/usr/bin/python
# Copyright (C) 2010 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.
import tempfile, unittest, subprocess, sys, os, os.path, shutil
import apport, apport.fileutils
class _T(unittest.TestCase):
def setUp(self):
self.srcdir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))
self.orig_report_dir = apport.fileutils.report_dir
apport.fileutils.report_dir = tempfile.mkdtemp()
os.environ['APPORT_REPORT_DIR'] = apport.fileutils.report_dir
os.environ['APPORT_JAVA_EXCEPTION_HANDLER'] = os.path.join(self.srcdir,
'data', 'java_uncaught_exception')
self.apport_jar_path = os.path.join(self.srcdir, 'java', 'apport.jar')
def tearDown(self):
shutil.rmtree(apport.fileutils.report_dir)
apport.fileutils.report_dir = self.orig_report_dir
def test_crash_class(self):
'''Crash in a .class file'''
p = subprocess.Popen(['java', '-classpath',
self.apport_jar_path + ':' + os.path.join(self.srcdir, 'java'), 'crash'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate()
self.assertNotEqual(p.returncode, 0, 'crash must exit with nonzero code')
self.assert_("Can't catch this" in err, 'crash handler must print original exception:\n' + err)
self._check_crash_report('java/crash.class')
def test_crash_jar(self):
'''Crash in a .jar file'''
p = subprocess.Popen(['java', '-classpath',
self.apport_jar_path + ':' + os.path.join(self.srcdir, 'java', 'crash.jar'), 'crash'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate()
self.assertNotEqual(p.returncode, 0, 'crash must exit with nonzero code')
self.assert_("Can't catch this" in err, 'crash handler must print original exception:\n' + err)
self._check_crash_report('java/crash.jar!/crash.class')
def _check_crash_report(self, main_file):
'''Check that we have one crash report, and verify its contents'''
reports = apport.fileutils.get_new_reports()
self.assertEqual(len(reports), 1, 'did not create a crash report')
r = apport.Report()
r.load(open(reports[0]))
self.assertEqual(r['ProblemType'], 'Crash')
self.assert_(r['ProcCmdline'].startswith('java -classpath'))
self.assert_(r['StackTrace'].startswith(
"java.lang.RuntimeException: Can't catch this"))
if '.jar!' in main_file:
self.assertEqual(r['MainClassUrl'], 'jar:file:%s/%s' % (self.srcdir, main_file))
else:
self.assertEqual(r['MainClassUrl'], 'file:%s/%s' % (self.srcdir, main_file))
self.assert_('DistroRelease' in r)
self.assert_('ProcCwd' in r)
#
# main
#
try:
subprocess.check_call(['java', '-version'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except OSError:
apport.warning('Java not available, skipping')
sys.exit(0)
tl = unittest.TestLoader()
tests_all = unittest.TestSuite(tl.loadTestsFromName('__main__'))
unittest.TextTestRunner(verbosity=2).run(tests_all)
|