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  
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.BeforeAll;
25  import org.junit.jupiter.api.Test;
26  
27  import javax.swing.tree.DefaultMutableTreeNode;
28  import java.util.Arrays;
29  
30  /*
31   * <pre>
32   *  + Root               Path: []
33   *  |
34   *  +-+ Node             Path: [0]
35   *  | |
36   *  | +-+ Sub-Node       Path: [0, 0]
37   *  | |
38   *  | +-+ Sub-Node       Path: [0, 1]
39   *  |
40   *  +-+ Node             Path: [1]
41   *    |
42   *    +-+ Sub-Node       Path: [1, 0]
43   *    |
44   *    +-+ Sub-Node       Path: [1, 1]
45   *    |
46   *    +-+ Sub-Node       Path: [1, 2]
47   * </pre>
48   */
49  public class TreePathUnitTest {
50  
51    private static final DefaultMutableTreeNode ROOT = new DefaultMutableTreeNode("root");
52    private static final DefaultMutableTreeNode A = new DefaultMutableTreeNode("a");
53    private static final DefaultMutableTreeNode B = new DefaultMutableTreeNode("b");
54    private static final DefaultMutableTreeNode A1 = new DefaultMutableTreeNode("a1");
55    private static final DefaultMutableTreeNode A2 = new DefaultMutableTreeNode("a2");
56    private static final DefaultMutableTreeNode B1 = new DefaultMutableTreeNode("b1");
57    private static final DefaultMutableTreeNode B2 = new DefaultMutableTreeNode("b2");
58    private static final DefaultMutableTreeNode B3 = new DefaultMutableTreeNode("b3");
59  
60    @BeforeAll
61    public static void setup() {
62      ROOT.add(A);
63      ROOT.add(B);
64      A.add(A1);
65      A.add(A2);
66      B.add(B1);
67      B.add(B2);
68      B.add(B3);
69    }
70  
71    @Test
72    public void test() {
73  
74      final TreePath root = new TreePath(ROOT);
75      final TreePath a = new TreePath(A);
76      final TreePath b = new TreePath(B);
77      final TreePath a1 = new TreePath(A1);
78      final TreePath a2 = new TreePath(A2);
79      final TreePath b1 = new TreePath(B1);
80      final TreePath b2 = new TreePath(B2);
81      final TreePath b3 = new TreePath(B3);
82  
83      Assertions.assertEquals(0, root.getPath().length);
84      Assertions.assertEquals(1, a.getPath().length);
85      Assertions.assertEquals(0, a.getPath()[0]);
86      Assertions.assertEquals(1, b.getPath().length);
87      Assertions.assertEquals(1, b.getPath()[0]);
88      Assertions.assertEquals(2, a1.getPath().length);
89      Assertions.assertEquals(0, a1.getPath()[0]);
90      Assertions.assertEquals(0, a1.getPath()[1]);
91      Assertions.assertEquals(2, a2.getPath().length);
92      Assertions.assertEquals(0, a2.getPath()[0]);
93      Assertions.assertEquals(1, a2.getPath()[1]);
94      Assertions.assertEquals(2, b1.getPath().length);
95      Assertions.assertEquals(1, b1.getPath()[0]);
96      Assertions.assertEquals(0, b1.getPath()[1]);
97      Assertions.assertEquals(2, b2.getPath().length);
98      Assertions.assertEquals(1, b2.getPath()[0]);
99      Assertions.assertEquals(1, b2.getPath()[1]);
100     Assertions.assertEquals(2, b3.getPath().length);
101     Assertions.assertEquals(1, b3.getPath()[0]);
102     Assertions.assertEquals(2, b3.getPath()[1]);
103   }
104 
105   @Test
106   public void testGetPath() {
107     final TreePath treePath = new TreePath(0, 1, 2);
108     Assertions.assertTrue(Arrays.equals(new int[]{0, 1, 2}, treePath.getPath()));
109   }
110 
111 }