View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.model;
21  
22  import org.junit.Assert;
23  import org.junit.BeforeClass;
24  import org.junit.Test;
25  
26  import javax.swing.tree.DefaultMutableTreeNode;
27  import java.util.Arrays;
28  
29  /*
30   * <pre>
31   *  + Root               Path: []
32   *  |
33   *  +-+ Node             Path: [0]
34   *  | |
35   *  | +-+ Sub-Node       Path: [0, 0]
36   *  | |
37   *  | +-+ Sub-Node       Path: [0, 1]
38   *  |
39   *  +-+ Node             Path: [1]
40   *    |
41   *    +-+ Sub-Node       Path: [1, 0]
42   *    |
43   *    +-+ Sub-Node       Path: [1, 1]
44   *    |
45   *    +-+ Sub-Node       Path: [1, 2]
46   * </pre>
47   */
48  public class TreePathUnitTest {
49  
50    private static final DefaultMutableTreeNode ROOT = new DefaultMutableTreeNode("root");
51    private static final DefaultMutableTreeNode A = new DefaultMutableTreeNode("a");
52    private static final DefaultMutableTreeNode B = new DefaultMutableTreeNode("b");
53    private static final DefaultMutableTreeNode A1 = new DefaultMutableTreeNode("a1");
54    private static final DefaultMutableTreeNode A2 = new DefaultMutableTreeNode("a2");
55    private static final DefaultMutableTreeNode B1 = new DefaultMutableTreeNode("b1");
56    private static final DefaultMutableTreeNode B2 = new DefaultMutableTreeNode("b2");
57    private static final DefaultMutableTreeNode B3 = new DefaultMutableTreeNode("b3");
58  
59    @BeforeClass
60    public static void setup() {
61      ROOT.add(A);
62      ROOT.add(B);
63      A.add(A1);
64      A.add(A2);
65      B.add(B1);
66      B.add(B2);
67      B.add(B3);
68    }
69  
70    @Test
71    public void test() {
72  
73      final TreePath root = new TreePath(ROOT);
74      final TreePath a = new TreePath(A);
75      final TreePath b = new TreePath(B);
76      final TreePath a1 = new TreePath(A1);
77      final TreePath a2 = new TreePath(A2);
78      final TreePath b1 = new TreePath(B1);
79      final TreePath b2 = new TreePath(B2);
80      final TreePath b3 = new TreePath(B3);
81  
82      Assert.assertEquals(0, root.getPath().length);
83      Assert.assertEquals(1, a.getPath().length);
84      Assert.assertEquals(0, a.getPath()[0]);
85      Assert.assertEquals(1, b.getPath().length);
86      Assert.assertEquals(1, b.getPath()[0]);
87      Assert.assertEquals(2, a1.getPath().length);
88      Assert.assertEquals(0, a1.getPath()[0]);
89      Assert.assertEquals(0, a1.getPath()[1]);
90      Assert.assertEquals(2, a2.getPath().length);
91      Assert.assertEquals(0, a2.getPath()[0]);
92      Assert.assertEquals(1, a2.getPath()[1]);
93      Assert.assertEquals(2, b1.getPath().length);
94      Assert.assertEquals(1, b1.getPath()[0]);
95      Assert.assertEquals(0, b1.getPath()[1]);
96      Assert.assertEquals(2, b2.getPath().length);
97      Assert.assertEquals(1, b2.getPath()[0]);
98      Assert.assertEquals(1, b2.getPath()[1]);
99      Assert.assertEquals(2, b3.getPath().length);
100     Assert.assertEquals(1, b3.getPath()[0]);
101     Assert.assertEquals(2, b3.getPath()[1]);
102   }
103 
104   @Test
105   public void testGetPath() {
106     TreePath treePath = new TreePath(0, 1, 2);
107     Assert.assertTrue(Arrays.equals(new int[]{0, 1, 2}, treePath.getPath()));
108     TreePath treePath2 = new TreePath(treePath, 3);
109     Assert.assertTrue(Arrays.equals(new int[]{0, 1, 2, 3}, treePath2.getPath()));
110   }
111 
112   @Test
113   public void testGetPathString() {
114     TreePath treePath = new TreePath(0, 1, 2);
115     Assert.assertEquals("_0_1_2", treePath.getPathString());
116     TreePath treePath2 = new TreePath(treePath, 3);
117     Assert.assertEquals("_0_1_2_3", treePath2.getPathString());
118   }
119 
120   @Test
121   public void testGetNode() {
122 
123     DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
124     DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("node1");
125     root.add(node1);
126     DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("node2");
127     DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("node3");
128     node2.add(node3);
129     root.add(node2);
130 
131     Assert.assertEquals(root, new TreePath(0).getNode(root));
132     Assert.assertEquals(node1, new TreePath(0, 0).getNode(root));
133     Assert.assertEquals(node2, new TreePath(0, 1).getNode(root));
134     Assert.assertEquals(node3, new TreePath(0, 1, 0).getNode(root));
135   }
136 
137   @Test
138   public void testGetNode2() {
139 
140     DefaultMutableTreeNode tree = new DefaultMutableTreeNode("Category");
141     tree.add(new DefaultMutableTreeNode("Sports"));
142     tree.add(new DefaultMutableTreeNode("Movies"));
143     DefaultMutableTreeNode science = new DefaultMutableTreeNode("Science");
144     tree.add(science);
145     science.add(new DefaultMutableTreeNode("Geography"));
146     science.add(new DefaultMutableTreeNode("Mathematics"));
147     DefaultMutableTreeNode astronomy = new DefaultMutableTreeNode("Astronomy");
148     astronomy.add(new DefaultMutableTreeNode("Education"));
149     astronomy.add(new DefaultMutableTreeNode("Pictures"));
150     science.add(astronomy);
151     tree.add(new DefaultMutableTreeNode("Music"));
152     tree.add(new DefaultMutableTreeNode("Games"));
153 
154     Assert.assertEquals("Category", new TreePath(0).getNode(tree).getUserObject());
155     Assert.assertEquals("Sports", new TreePath(0, 0).getNode(tree).getUserObject());
156     Assert.assertEquals("Astronomy", new TreePath(0, 2, 2).getNode(tree).getUserObject());
157     Assert.assertEquals("Games", new TreePath(0, 4).getNode(tree).getUserObject());
158   }
159 }