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     final TreePath treePath = new TreePath(0, 1, 2);
107     Assert.assertTrue(Arrays.equals(new int[]{0, 1, 2}, treePath.getPath()));
108   }
109 
110 }