• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ahat;
18 
19 import com.android.tools.perflib.heap.Instance;
20 import java.io.IOException;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import org.junit.Test;
25 
26 public class InstanceUtilsTest {
27   @Test
asStringBasic()28   public void asStringBasic() throws IOException {
29     TestDump dump = TestDump.getTestDump();
30     Instance str = (Instance)dump.getDumpedThing("basicString");
31     assertEquals("hello, world", InstanceUtils.asString(str));
32   }
33 
34   @Test
asStringCharArray()35   public void asStringCharArray() throws IOException {
36     TestDump dump = TestDump.getTestDump();
37     Instance str = (Instance)dump.getDumpedThing("charArray");
38     assertEquals("char thing", InstanceUtils.asString(str));
39   }
40 
41   @Test
asStringTruncated()42   public void asStringTruncated() throws IOException {
43     TestDump dump = TestDump.getTestDump();
44     Instance str = (Instance)dump.getDumpedThing("basicString");
45     assertEquals("hello", InstanceUtils.asString(str, 5));
46   }
47 
48   @Test
asStringCharArrayTruncated()49   public void asStringCharArrayTruncated() throws IOException {
50     TestDump dump = TestDump.getTestDump();
51     Instance str = (Instance)dump.getDumpedThing("charArray");
52     assertEquals("char ", InstanceUtils.asString(str, 5));
53   }
54 
55   @Test
asStringExactMax()56   public void asStringExactMax() throws IOException {
57     TestDump dump = TestDump.getTestDump();
58     Instance str = (Instance)dump.getDumpedThing("basicString");
59     assertEquals("hello, world", InstanceUtils.asString(str, 12));
60   }
61 
62   @Test
asStringCharArrayExactMax()63   public void asStringCharArrayExactMax() throws IOException {
64     TestDump dump = TestDump.getTestDump();
65     Instance str = (Instance)dump.getDumpedThing("charArray");
66     assertEquals("char thing", InstanceUtils.asString(str, 10));
67   }
68 
69   @Test
asStringNotTruncated()70   public void asStringNotTruncated() throws IOException {
71     TestDump dump = TestDump.getTestDump();
72     Instance str = (Instance)dump.getDumpedThing("basicString");
73     assertEquals("hello, world", InstanceUtils.asString(str, 50));
74   }
75 
76   @Test
asStringCharArrayNotTruncated()77   public void asStringCharArrayNotTruncated() throws IOException {
78     TestDump dump = TestDump.getTestDump();
79     Instance str = (Instance)dump.getDumpedThing("charArray");
80     assertEquals("char thing", InstanceUtils.asString(str, 50));
81   }
82 
83   @Test
asStringNegativeMax()84   public void asStringNegativeMax() throws IOException {
85     TestDump dump = TestDump.getTestDump();
86     Instance str = (Instance)dump.getDumpedThing("basicString");
87     assertEquals("hello, world", InstanceUtils.asString(str, -3));
88   }
89 
90   @Test
asStringCharArrayNegativeMax()91   public void asStringCharArrayNegativeMax() throws IOException {
92     TestDump dump = TestDump.getTestDump();
93     Instance str = (Instance)dump.getDumpedThing("charArray");
94     assertEquals("char thing", InstanceUtils.asString(str, -3));
95   }
96 
97   @Test
asStringNull()98   public void asStringNull() throws IOException {
99     TestDump dump = TestDump.getTestDump();
100     Instance obj = (Instance)dump.getDumpedThing("nullString");
101     assertNull(InstanceUtils.asString(obj));
102   }
103 
104   @Test
asStringNotString()105   public void asStringNotString() throws IOException {
106     TestDump dump = TestDump.getTestDump();
107     Instance obj = (Instance)dump.getDumpedThing("anObject");
108     assertNotNull(obj);
109     assertNull(InstanceUtils.asString(obj));
110   }
111 
112   @Test
basicReference()113   public void basicReference() throws IOException {
114     TestDump dump = TestDump.getTestDump();
115 
116     Instance pref = (Instance)dump.getDumpedThing("aPhantomReference");
117     Instance wref = (Instance)dump.getDumpedThing("aWeakReference");
118     Instance referent = (Instance)dump.getDumpedThing("anObject");
119     assertNotNull(pref);
120     assertNotNull(wref);
121     assertNotNull(referent);
122     assertEquals(referent, InstanceUtils.getReferent(pref));
123     assertEquals(referent, InstanceUtils.getReferent(wref));
124     assertNull(InstanceUtils.getReferent(referent));
125   }
126 }
127