1 /*
2  * Copyright (C) 2016 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.ahat.heapdump.AhatHeap;
20 import com.android.ahat.heapdump.AhatInstance;
21 import com.android.ahat.heapdump.Reachability;
22 import com.android.ahat.heapdump.Value;
23 import java.io.IOException;
24 import org.junit.Test;
25 
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 
31 public class DiffTest {
32   @Test
diffMatchedHeap()33   public void diffMatchedHeap() throws IOException {
34     TestDump dump = TestDump.getTestDump();
35     AhatHeap a = dump.getAhatSnapshot().getHeap("app");
36     assertNotNull(a);
37     AhatHeap b = dump.getBaselineAhatSnapshot().getHeap("app");
38     assertNotNull(b);
39     assertEquals(a.getBaseline(), b);
40     assertEquals(b.getBaseline(), a);
41   }
42 
43   @Test
diffUnchanged()44   public void diffUnchanged() throws IOException {
45     TestDump dump = TestDump.getTestDump();
46 
47     AhatInstance a = dump.getDumpedAhatInstance("unchangedObject");
48     assertNotNull(a);
49 
50     AhatInstance b = dump.getBaselineDumpedAhatInstance("unchangedObject");
51     assertNotNull(b);
52     assertEquals(a, b.getBaseline());
53     assertEquals(b, a.getBaseline());
54     assertEquals(a.getSite(), b.getSite().getBaseline());
55     assertEquals(b.getSite(), a.getSite().getBaseline());
56 
57     Value va = Value.pack(a);
58     assertEquals(b, Value.getBaseline(va).asAhatInstance());
59   }
60 
61   @Test
diffAdded()62   public void diffAdded() throws IOException {
63     TestDump dump = TestDump.getTestDump();
64 
65     AhatInstance a = dump.getDumpedAhatInstance("addedObject");
66     assertNotNull(a);
67     assertNull(dump.getBaselineDumpedAhatInstance("addedObject"));
68     assertTrue(a.getBaseline().isPlaceHolder());
69   }
70 
71   @Test
diffRemoved()72   public void diffRemoved() throws IOException {
73     TestDump dump = TestDump.getTestDump();
74 
75     assertNull(dump.getDumpedAhatInstance("removedObject"));
76     AhatInstance b = dump.getBaselineDumpedAhatInstance("removedObject");
77     assertNotNull(b);
78     assertTrue(b.getBaseline().isPlaceHolder());
79   }
80 
81   @Test
diffClassRemoved()82   public void diffClassRemoved() throws IOException {
83     TestDump dump = TestDump.getTestDump("O.hprof", "L.hprof", null, Reachability.STRONG);
84     AhatHandler handler = new ObjectsHandler(dump.getAhatSnapshot());
85     TestHandler.testNoCrash(handler, "http://localhost:7100/objects?class=java.lang.Class");
86   }
87 }
88