1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.libcore.timezone.tzlookup.zonetree; 18 19 import org.junit.Test; 20 21 import java.util.ArrayList; 22 import java.util.Arrays; 23 import java.util.List; 24 25 import static java.util.Arrays.asList; 26 import static org.junit.Assert.assertEquals; 27 import static org.junit.Assert.assertFalse; 28 import static org.junit.Assert.assertNull; 29 import static org.junit.Assert.assertSame; 30 import static org.junit.Assert.assertTrue; 31 import static org.junit.Assert.fail; 32 33 public class TreeNodeTest { 34 35 private static class TestTreeVisitor implements TreeNode.Visitor<TestTreeNode> { 36 37 private List<String> ids = new ArrayList<>(); 38 39 @Override visit(TestTreeNode node)40 public void visit(TestTreeNode node) { 41 ids.add(node.getId()); 42 } 43 reset()44 public void reset() { 45 ids.clear(); 46 } 47 } 48 49 private static class TestTreeNode extends TreeNode<TestTreeNode> { TestTreeNode(String id)50 public TestTreeNode(String id) { 51 super(id); 52 } 53 } 54 55 @Test testTreeNode_single()56 public void testTreeNode_single() { 57 TestTreeNode testTreeNode = new TestTreeNode("id"); 58 assertEquals("id", testTreeNode.getId()); 59 assertEquals(0, testTreeNode.getChildrenCount()); 60 assertTrue(testTreeNode.getChildren().isEmpty()); 61 assertNull(testTreeNode.getParent()); 62 assertTrue(testTreeNode.isRoot()); 63 assertTrue(testTreeNode.isLeaf()); 64 65 TestTreeVisitor testVisitor = new TestTreeVisitor(); 66 testTreeNode.visitSelfThenChildrenRecursive(testVisitor); 67 assertEquals(testVisitor.ids, asList("id")); 68 } 69 70 @Test testTreeNode_many()71 public void testTreeNode_many() { 72 TestTreeNode node1 = new TestTreeNode("1"); 73 TestTreeNode node11 = new TestTreeNode("1.1"); 74 TestTreeNode node12 = new TestTreeNode("1.2"); 75 TestTreeNode node111 = new TestTreeNode("1.1.1"); 76 TestTreeNode node112 = new TestTreeNode("1.1.2"); 77 TestTreeNode node113 = new TestTreeNode("1.1.3"); 78 TestTreeNode node121 = new TestTreeNode("1.2.1"); 79 TestTreeNode node122 = new TestTreeNode("1.2.2"); 80 TestTreeNode node123 = new TestTreeNode("1.2.3"); 81 82 // Build the tree. 83 node1.addChild(node11); 84 node1.addChild(node12); 85 node11.addChild(node111); 86 node11.addChild(node112); 87 node11.addChild(node113); 88 node12.addChild(node121); 89 node12.addChild(node122); 90 node12.addChild(node123); 91 92 assertTrue(node1.isRoot()); 93 assertFalse(node1.isLeaf()); 94 assertFalse(node11.isRoot()); 95 assertFalse(node11.isLeaf()); 96 assertFalse(node111.isRoot()); 97 assertTrue(node111.isLeaf()); 98 99 // Visit the tree. 100 TestTreeVisitor testVisitor = new TestTreeVisitor(); 101 node1.visitSelfThenChildrenRecursive(testVisitor); 102 assertEquals( 103 asList("1", "1.1", "1.1.1", "1.1.2", "1.1.3", "1.2", "1.2.1", "1.2.2", "1.2.3"), 104 testVisitor.ids); 105 106 // Remove a node from the tree. 107 node1.removeChild(node11); 108 assertNull(node11.getParent()); 109 assertTrue(node11.isRoot()); 110 assertFalse(node11.isLeaf()); 111 112 // Visit the tree again. 113 testVisitor.reset(); 114 node1.visitSelfThenChildrenRecursive(testVisitor); 115 assertEquals(asList("1", "1.2", "1.2.1", "1.2.2", "1.2.3"), testVisitor.ids); 116 117 // Visit the removed node. 118 testVisitor.reset(); 119 node11.visitSelfThenChildrenRecursive(testVisitor); 120 assertEquals( 121 asList("1.1", "1.1.1", "1.1.2", "1.1.3"), 122 testVisitor.ids); 123 } 124 125 @Test testTreeNode_cannotAddTwice()126 public void testTreeNode_cannotAddTwice() { 127 TestTreeNode node1 = new TestTreeNode("1"); 128 TestTreeNode node11 = new TestTreeNode("1.1"); 129 node1.addChild(node11); 130 131 try { 132 node1.addChild(node11); 133 fail(); 134 } catch (IllegalStateException expected) { 135 } 136 137 assertSame(node11, node1.removeChild(node11)); 138 node1.addChild(node11); 139 140 TestTreeNode unattachedNode = new TestTreeNode("Unattached"); 141 assertNull(node1.removeChild(unattachedNode)); 142 } 143 } 144