Exemple #1
0
 def test_casting(self):
     s = self.shd(1001)
     l = hprof.cast(s, self.lst)
     o = hprof.cast(s, self.obj)
     # casting upward
     self.assertIs(hprof.cast(s, self.shd), s)
     self.assertEqual(hprof.cast(s, self.lst), s)
     self.assertEqual(hprof.cast(s, self.obj), s)
     # casting downward
     self.assertEqual(hprof.cast(o, self.obj), s)
     self.assertEqual(hprof.cast(o, self.lst), s)
     self.assertIs(hprof.cast(o, self.shd), s)
Exemple #2
0
 def test_refs_to_class(self):
     _, string = heap._create_class(self, self.names['str'], self.obj, {},
                                    ('chars', ), (jtype.object, ))
     o = hprof.cast(string, self.obj)
     c = hprof.cast(string, self.cls)
     self.assertIs(o, string)
     self.assertIs(c, string)
     with self.assertRaises(TypeError):
         hprof.cast(string, string)
     with self.assertRaises(TypeError):
         hprof.cast(string, self.lst)
Exemple #3
0
 def test_unref(self):
     s = self.shd(1234)
     o = hprof.cast(s, self.obj)
     self.assertIs(hprof.cast(o), s)
Exemple #4
0
 def test_bad_downcast(self):
     s = self.shd(1033)
     o = hprof.cast(s, self.obj)
     with self.assertRaises(TypeError):
         hprof.cast(o, self.cls)
Exemple #5
0
 def test_bad_cast(self):
     s = self.shd(1020)
     with self.assertRaises(TypeError):
         hprof.cast(s, self.java.lang.Class)
Exemple #6
0
    def test_refs(self):
        _, extraclass = heap._create_class(self, self.names['ext'], self.shd,
                                           {}, ('shadow', ), (jtype.int, ))
        e = extraclass(0xbadf00d)
        self.obj._hprof_ifieldvals.__set__(e, (1111, ))
        self.lst._hprof_ifieldvals.__set__(e, (708, ))
        self.shd._hprof_ifieldvals.__set__(e, (2223, 33))
        extraclass._hprof_ifieldvals.__set__(e, (10, ))

        self.assertEqual(e.shadow, 10)
        self.assertEqual(e.unique, 33)
        self.assertEqual(e.next, 708)
        self.assertCountEqual(dir(e), ('shadow', 'unique', 'next'))
        self.assertEqual(str(e), 'Extra@badf00d')
        self.assertEqual(repr(e), '<Extra 0xbadf00d>')

        r = heap.Ref(e, extraclass)
        self.assertIs(r,
                      e)  # no reason to use Ref when reftype matches exactly.

        s = hprof.cast(e, self.shd)
        self.assertEqual(s, e)
        self.assertEqual(s.shadow, 2223)
        self.assertEqual(s.unique, 33)
        self.assertEqual(s.next, 708)
        self.assertCountEqual(dir(s), ('shadow', 'unique', 'next'))
        self.assertEqual(str(s), '<Ref of type Shadower to Extra 0xbadf00d>')
        self.assertEqual(repr(s), str(s))
        self.assertIsInstance(s, self.obj)
        self.assertIsInstance(s, self.lst)
        self.assertIsInstance(s, self.shd)
        self.assertIsInstance(s, extraclass)
        self.assertNotIsInstance(s, self.cls)

        l = hprof.cast(e, self.lst)
        self.assertEqual(l.shadow, 1111)
        self.assertEqual(l, e)
        with self.assertRaises(AttributeError):
            self.assertEqual(l.unique, 33)
        self.assertEqual(l.next, 708)
        self.assertCountEqual(dir(l), ('shadow', 'next'))
        self.assertEqual(str(l),
                         '<Ref of type java.util.List to Extra 0xbadf00d>')
        self.assertEqual(repr(l), str(l))
        self.assertIsInstance(l, self.obj)
        self.assertIsInstance(l, self.lst)
        self.assertIsInstance(l, self.shd)
        self.assertIsInstance(l, extraclass)
        self.assertNotIsInstance(l, self.cls)

        o = hprof.cast(e, self.obj)
        self.assertEqual(o, e)
        self.assertEqual(o.shadow, 1111)
        with self.assertRaises(AttributeError):
            self.assertEqual(o.unique, 33)
        with self.assertRaises(AttributeError):
            self.assertEqual(o.next, 708)
        self.assertCountEqual(dir(o), ('shadow', ))
        self.assertEqual(str(o),
                         '<Ref of type java.lang.Object to Extra 0xbadf00d>')
        self.assertEqual(repr(o), str(o))
        self.assertIsInstance(o, self.obj)
        self.assertIsInstance(o, self.lst)
        self.assertIsInstance(o, self.shd)
        self.assertIsInstance(o, extraclass)
        self.assertNotIsInstance(o, self.cls)