Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c5376d99596896f5798ad12a1ae405ac7100f8c (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*******************************************************************************
 * Copyright (c) 2010, 2018 SAP AG and IBM Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    SAP AG - initial API and implementation - moved from ExtractCollectionEntriesTest
 *    Andrew Johnson - lots of extra tests including all Java 7 collections
 *******************************************************************************/
package org.eclipse.mat.tests.collect;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.isOneOf;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.hamcrest.number.OrderingComparison.greaterThan;
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
import static org.hamcrest.number.OrderingComparison.lessThanOrEqualTo;

import java.io.Serializable;
import java.util.regex.Pattern;

import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.query.IContextObject;
import org.eclipse.mat.query.IResult;
import org.eclipse.mat.query.IResultTable;
import org.eclipse.mat.query.IResultTree;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.query.SnapshotQuery;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.mat.util.VoidProgressListener;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.rules.ErrorCollector;

public class ExtractCollectionEntriesBase
{
    /**
     * Hamcrest style matcher for regular expression matching.
     * Hamcrest 1.1 doesn't have this.
     * @param regex
     * @return true if the string matches the pattern
     */
    public static Matcher<java.lang.String> matchesPattern(final java.lang.String regex)
    {
        return new TypeSafeMatcher<String>() {
            Pattern pat = Pattern.compile(regex);

            public void describeTo(Description description)
            {
                description.appendText("a string matching '" + regex + "'");
            }

            @Override
            protected boolean matchesSafely(String item)
            {
                return pat.matcher(item).matches();
            }

        };
    }

    @Rule
    public ErrorCollector collector = new ErrorCollector();

    void checkListObjects(long objAddress, int numEntries, boolean checkVals, ISnapshot snapshot) throws SnapshotException
    {
        IObject obj = snapshot.getObject(snapshot.mapAddressToId(objAddress));
        String prefix = obj.getTechnicalName()+": ";
        SnapshotQuery query = SnapshotQuery.parse("extract_list_values 0x" + Long.toHexString(objAddress), snapshot); //$NON-NLS-1$
        String name = obj.getClazz().getName();
        try
        {
            IResult result = query.execute(new VoidProgressListener());
            IResultTree table = (IResultTree) result;
            if (table != null)
            {
                int rowCount = table.getElements().size();

                collector.checkThat(MessageUtil.format(
                                "Expected to extract {0} entries from list {1} [{2}], but got {3} entries in the result", //$NON-NLS-1$
                                numEntries, obj, snapshot.getSnapshotInfo().getPath(), rowCount), rowCount,
                                equalTo(numEntries));
                for (Object o : table.getElements())
                {
                    IContextObject co = table.getContext(o);
                    IClass cls = snapshot.getClassOf(co.getObjectId());
                    collector.checkThat(prefix + "List should contain Strings", cls.getName(), equalTo("java.lang.String"));
                    IObject lobj = snapshot.getObject(co.getObjectId());

                    String cv = lobj.getClassSpecificName();
                    if (cv != null)
                    {
                        if (checkVals)
                        {
                            collector.checkThat(prefix + "List entry should start with the type", cv, startsWith(name));
                        }
                        collector.checkThat(prefix + "Should end with number or aAbB", cv,
                                    matchesPattern(".*([0-9]+|[aAbB]+)$"));
                    }
                    else
                    {
                        collector.checkThat(prefix + "snapshot type is DTFJ-PHD when list item contents is null", snapshot.getSnapshotInfo().getProperty("$heapFormat"), equalTo((Serializable)"DTFJ-PHD"));
                    }
                }
            }
        }
        catch (IllegalArgumentException e)
        {
            collector.addError(e);
        }
    }

    /**
     * Checks all the entries are the special values from CreateCollectionDump
     * @param objAddress
     * @param numEntries
     * @param snapshot
     * @throws SnapshotException
     */
    protected void checkHashSetObjects(long objAddress, int numEntries, boolean checkVals, ISnapshot snapshot) throws SnapshotException
    {
        IObject obj = snapshot.getObject(snapshot.mapAddressToId(objAddress));
        String prefix = obj.getTechnicalName()+": ";
        SnapshotQuery query = SnapshotQuery.parse("hash_set_values 0x" + Long.toHexString(objAddress), snapshot); //$NON-NLS-1$
        String name = obj.getClazz().getName();

        try
        {
            IResult result = query.execute(new VoidProgressListener());
            IResultTree table = (IResultTree) result;
            if (table != null)
            {
                int rowCount = table.getElements().size();

                collector.checkThat(MessageUtil.format(
                                "Expected to extract {0} entries from hash set {1} [{2}], but got {3} entries in the result", //$NON-NLS-1$
                                numEntries, obj, snapshot.getSnapshotInfo().getPath(), rowCount), rowCount,
                                equalTo(numEntries));
                for (Object o : table.getElements())
                {
                    IContextObject co = table.getContext(o);
                    IClass cls = snapshot.getClassOf(co.getObjectId());
                    collector.checkThat(prefix + "hash set should contain Strings", cls.getName(),
                                    equalTo("java.lang.String"));
                    IObject cobj = snapshot.getObject(co.getObjectId());
                    String cv = cobj.getClassSpecificName();
                    if (cv != null)
                    {
                        if (checkVals)
                        {
                            collector.checkThat(prefix + "Hash set entry should start with the type", cv, startsWith(name));
                        }
                        collector.checkThat(prefix + "Should end with number or aAbB", cv,
                                    matchesPattern(".*([0-9]+|[aAbB]+)$"));
                    }
                    else
                    {
                        collector.checkThat(prefix+"snapshot type is DTFJ-PHD when hash set item contents is null", snapshot.getSnapshotInfo().getProperty("$heapFormat"), equalTo((Serializable)"DTFJ-PHD"));
                    }
                }
            }
        }
        catch (IllegalArgumentException e)
        {
            collector.addError(e);
        }
    }

    protected void checkCollection(long objAddress, int numEntries, ISnapshot snapshot) throws SnapshotException
    {
        checkMap(objAddress, numEntries, snapshot);
        checkHashEntries(objAddress, numEntries, snapshot, false, false);
    }

    /**
     * HashMap entries.
     * Check keys and values.
     * Map:
     * 123 : full.class.name:123
     * Set from map
     * full.class.name:123 : <other>
     * @param objAddress
     * @param numEntries
     * @param snapshot
     * @param checkValueString
     * @param isMap whether this is a normal test map, or a map from a set
     * @throws SnapshotException
     */
    protected void checkHashEntries(long objAddress, int numEntries, ISnapshot snapshot, boolean checkValueString, boolean isMap) throws SnapshotException
    {
        IObject obj = snapshot.getObject(snapshot.mapAddressToId(objAddress));
        String prefix = obj.getTechnicalName()+": ";
        SnapshotQuery query = SnapshotQuery.parse("hash_entries 0x" + Long.toHexString(objAddress), snapshot); //$NON-NLS-1$

        IResult result;
        try
        {
            result = query.execute(new VoidProgressListener());
        }
        catch (UnsupportedOperationException e)
        {
            collector.checkThat(prefix + "snapshot type is DTFJ-PHD when unsupported operation for hash entries", snapshot.getSnapshotInfo().getProperty("$heapFormat"), equalTo((Serializable)"DTFJ-PHD"));
            return;
        }
        IResultTable table = (IResultTable) result;
        int rowCount = table.getRowCount();

        collector.checkThat(MessageUtil.format(prefix+"Expected to extract {0} entries from collection {1} [{2}], but got {3} entries in the result", //$NON-NLS-1$
                        numEntries, obj, snapshot.getSnapshotInfo().getPath(), rowCount), rowCount, equalTo(numEntries));
        // Check that at least one key and value value differs
        boolean diff = rowCount == 0;
        for (int i = 0; i < rowCount; ++i)
        {
            Object row = table.getRow(i);
            Object k1 = table.getColumnValue(row, 1);
            Object v1 = table.getColumnValue(row, 2);
            if (k1 != null ? !k1.equals(v1) : v1 != null)
            {
                diff = true;
            }
            if (k1 != null)
            {
                collector.checkThat(prefix+"Key should be an String", k1, instanceOf(String.class));
                if (isMap && checkValueString && k1 instanceof String)
                {
                    collector.checkThat(prefix+"Key should be a number or aAbB", (String)k1, matchesPattern("[0-9]+|[aAbB]+"));
                }
                if (checkValueString && k1 instanceof String)
                {
                    collector.checkThat(prefix+"Key should end with a number or aAbB", (String)k1, matchesPattern(".*([0-9]+|[aAbB]+)$"));
                }
                if (!isMap && checkValueString && k1 instanceof String)
                {
                    // Some of the sets are KeySets from Maps so have numeric/aAbB not class names
                    collector.checkThat(prefix+"Key contains name of collection class or a number or aAbB", (String)k1, anyOf(containsString(obj.getClazz().getName()), matchesPattern("[0-9]+|[aAbB]+")));
                }
            }
            if (v1 != null)
            {
                collector.checkThat(prefix+"Value should be an String", v1, instanceOf(String.class));
            }
            if (isMap && checkValueString && k1 instanceof String &&  v1 instanceof String)
            {
                collector.checkThat(prefix+"Value contains key", (String)v1, containsString((String)k1));
            }
            if (isMap && checkValueString && v1 instanceof String)
            {
                collector.checkThat(prefix+"Value contains name of collection class", (String)v1, containsString(obj.getClazz().getName()));
            }

            IContextObject ctx = table.getContext(row);
            collector.checkThat(prefix+"Row object should be the map", ctx.getObjectId(), equalTo(obj.getObjectId()));
        }
        if (checkValueString)
        {
            collector.checkThat(MessageUtil.format(
                            "Expected some differing keys and values {0} from collection {1} [{2}]", obj, snapshot
                            .getSnapshotInfo().getPath()), diff, equalTo(true));
        }
    }

    /**
     * Also run the size query
     * @param objAddress
     * @param numEntries
     * @param snapshot
     * @throws SnapshotException
     */
    protected void checkCollectionSize(long objAddress, int numEntries, ISnapshot snapshot) throws SnapshotException
    {
        IObject obj = snapshot.getObject(snapshot.mapAddressToId(objAddress));
        String prefix = obj.getTechnicalName()+": ";
        SnapshotQuery query2 = SnapshotQuery.parse("collections_grouped_by_size 0x" + Long.toHexString(objAddress), snapshot); //$NON-NLS-1$
        IResult result2 = query2.execute(new VoidProgressListener());
        IResultTable table2 = (IResultTable) result2;
        int rowCount2 = table2.getRowCount();
        collector.checkThat("Rows for size "+obj, rowCount2, equalTo(1));
        if (rowCount2 == 1)
        {
            Object row = table2.getRow(0);
            int sizeBucket = (Integer)table2.getColumnValue(row, 0);
            collector.checkThat(prefix+"size "+obj, sizeBucket, equalTo(numEntries));
        }
    }

    protected void checkMap(long objAddress, int numEntries, ISnapshot snapshot) throws SnapshotException
    {
        checkCollectionSize(objAddress, numEntries, snapshot);
        checkCollectionFillRatio(objAddress, numEntries, snapshot);
        checkMapCollisionRatio(objAddress, numEntries, snapshot);
    }

    protected void checkList(long objAddress, int numEntries, boolean checkVals, ISnapshot snapshot) throws SnapshotException
    {
        checkCollectionSize(objAddress, numEntries, snapshot);
        checkListObjects(objAddress, numEntries, checkVals, snapshot);
    }

    /**
     * Run the fill ratio query
     * @param objAddress
     * @param numEntries
     * @param snapshot
     * @throws SnapshotException
     */
    protected void checkCollectionFillRatio(long objAddress, int numEntries, ISnapshot snapshot) throws SnapshotException
    {
        IObject obj = snapshot.getObject(snapshot.mapAddressToId(objAddress));
        String prefix = obj.getTechnicalName()+": ";
        SnapshotQuery query2 = SnapshotQuery.parse("collection_fill_ratio 0x" + Long.toHexString(objAddress), snapshot); //$NON-NLS-1$
        IResult result2 = query2.execute(new VoidProgressListener());
        IResultTable table2 = (IResultTable) result2;
        int rowCount2 = table2.getRowCount();
        String className = snapshot.getClassOf(snapshot.mapAddressToId(objAddress)).getName();
        if (className.equals("java.util.TreeMap") || className.equals("java.util.TreeMap$KeySet") ||
            className.equals("java.util.TreeMap$EntrySet") ||
            className.equals("java.util.concurrent.ConcurrentSkipListMap") || className.equals("java.util.concurrent.ConcurrentSkipListMap$KeySet") ||
            className.equals("java.util.concurrent.ConcurrentSkipListMap$EntrySet") ||
            className.equals("java.util.TreeSet") || className.equals("java.util.concurrent.ConcurrentSkipListSet"))
        {
            // TreeMaps and ConcurrentSkipListMap don't appear in the fill ratio report as they
            // don't have a backing array
            collector.checkThat(prefix+"Fill ratio rows", rowCount2, equalTo(0));
        }
        else
        {
            if (!className.startsWith("java.util.Collections") && !className.equals("javax.script.SimpleBindings"))
            {
                collector.checkThat(prefix+"Fill ratio rows", rowCount2, equalTo(1));
            }
            if (rowCount2 == 1)
            {
                Object row = table2.getRow(0);
                double v = (Double)table2.getColumnValue(row, 0);
                if (numEntries > 0)
                {
                    collector.checkThat(prefix+"Fill ratio value > 0.0", v, greaterThan(0.0));
                    // If there are a lot of collisions the ratio could be greater than one
                    collector.checkThat(prefix+"Fill ratio value <= 1.2", v, lessThanOrEqualTo(1.2));
                }
                else
                {
                    // 1.0 if the size == 0, capacity == 0, 0.0 if the size == 0, capacity > 0
                    collector.checkThat(prefix+"Fill ratio value == 0.0 or 1.0 ", v, isOneOf(0.0, 1.0));
                }
            }
        }
    }

    /**
     * Also run the map collision ratio query
     * @param objAddress
     * @param numEntries
     * @param snapshot
     * @throws SnapshotException
     */
    protected void checkMapCollisionRatio(long objAddress, int numEntries, ISnapshot snapshot) throws SnapshotException
    {
        IObject obj = snapshot.getObject(snapshot.mapAddressToId(objAddress));
        String prefix = obj.getTechnicalName()+": ";
        SnapshotQuery query2 = SnapshotQuery.parse("map_collision_ratio 0x" + Long.toHexString(objAddress), snapshot); //$NON-NLS-1$
        IResult result2 = query2.execute(new VoidProgressListener());
        IResultTable table2 = (IResultTable) result2;
        int rowCount2 = table2.getRowCount();
        collector.checkThat(prefix+"Rows for Map collision ratio ", rowCount2, equalTo(1));
        if (rowCount2 == 1)
        {
            Object row = table2.getRow(0);
            double v = (Double)table2.getColumnValue(row, 0);
            collector.checkThat(prefix+"Map collision value >= 0.0", v, greaterThanOrEqualTo(0.0));
            // 100% collisions shouldn't be possible, but might be if every entry in the first table maps to 2 keys
            collector.checkThat(prefix+"Map collision value <= 1.0", v, lessThanOrEqualTo(1.0));
            // No collisions possible if no entries
            if (numEntries == 0)
                collector.checkThat(prefix+"Map collision ratio if no entries", v, equalTo(0.0));
            if (numEntries == 1)
                collector.checkThat(prefix+"Map collision ratio if one entry", v, equalTo(0.0));
        }
    }

}

Back to the top