Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e428b9652313b0a47e53a3ed9e35ccd8f1fc16a2 (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
/*******************************************************************************
 * Copyright (c) 2011, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.tests.viewer.model;


import org.eclipse.jface.viewers.TreePath;
import org.junit.Assert;

/**
 * Utility for comparing TreePath objects in unit tests.  This wrapper prints the tree
 * paths in exception showing contexts of the paths.
 *
 * @since 3.7
 */
public class TreePathWrapper {
    private final TreePath fPath;

    public TreePathWrapper(TreePath path) {
        fPath = path;
    }

    @Override
	public int hashCode() {
        return fPath.hashCode();
    }

    @Override
	public boolean equals(Object obj) {
        return obj instanceof TreePathWrapper &&
               fPath.equals( ((TreePathWrapper)obj).fPath );
    }

    @Override
	public String toString() {
        if (fPath.getSegmentCount() == 0) {
            return "TreePath:EMPTY"; //$NON-NLS-1$
        }

        StringBuffer buf = new StringBuffer("TreePath:["); //$NON-NLS-1$

        for (int i = 0; i < fPath.getSegmentCount(); i++) {
            if (i != 0) {
                buf.append(", ");                     //$NON-NLS-1$
            }
            buf.append(fPath.getSegment(i));
        }
        buf.append(']');
        return buf.toString();
    }

    /**
     * Asserts that the two given tree paths are the same.  In case of failure, the
     * generated exception will contain a printout of the tree paths' contents.
     */
    public static void assertEqual(TreePath expected, TreePath actual) {
        Assert.assertEquals(
            expected != null ? new TreePathWrapper(expected) : null,
            actual != null ? new TreePathWrapper(actual) : null);
    }
}

Back to the top