|
| 1 | +/* |
| 2 | + * Copyright 2021 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
| 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 | +package org.utplsql.sqldev.test; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.utplsql.sqldev.model.runner.ItemNode; |
| 27 | + |
| 28 | +public class ItemNodeTest { |
| 29 | + private final HashMap<String, ItemNode> model = new HashMap<>(); |
| 30 | + private final List<ItemNode> selection = new ArrayList<>(); |
| 31 | + |
| 32 | + private void addItem(String id, String parentId) { |
| 33 | + // using suite only |
| 34 | + org.utplsql.sqldev.model.runner.Suite suite = new org.utplsql.sqldev.model.runner.Suite(); |
| 35 | + suite.setId(id); |
| 36 | + ItemNode node = new ItemNode(suite); |
| 37 | + model.put(id, node); |
| 38 | + ItemNode parent; |
| 39 | + if (parentId != null) { |
| 40 | + parent = model.get(parentId); |
| 41 | + parent.add(node); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setup() { |
| 47 | + /* |
| 48 | + * Setup model for all test cases: |
| 49 | + * |
| 50 | + * a |
| 51 | + * +- a.a |
| 52 | + * +- a.a.a |
| 53 | + * +- a.a.b |
| 54 | + * +- a.a.b.a |
| 55 | + * +- a.a.b.b |
| 56 | + * +- a.b |
| 57 | + * +- a.b.a |
| 58 | + * +- a.b.b |
| 59 | + * b |
| 60 | + * +- b.a |
| 61 | + * +- b.b |
| 62 | + */ |
| 63 | + model.clear(); |
| 64 | + addItem("a" , null); |
| 65 | + addItem("a.a" , "a"); |
| 66 | + addItem("a.a.a" , "a.a"); |
| 67 | + addItem("a.a.b" , "a.a"); |
| 68 | + addItem("a.a.b.a", "a.a.b"); |
| 69 | + addItem("a.a.b.b", "a.a.b"); |
| 70 | + addItem("a.b" , "a"); |
| 71 | + addItem("a.b.a" , "a.b"); |
| 72 | + addItem("a.b.b" , "a.b"); |
| 73 | + addItem("b" , null); |
| 74 | + addItem("b.a" , "b"); |
| 75 | + addItem("b.b" , "b"); |
| 76 | + selection.clear(); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Test |
| 81 | + public void null_input() { |
| 82 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(null); |
| 83 | + Assert.assertEquals(0, actual.size()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void empty_input() { |
| 88 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(selection); |
| 89 | + Assert.assertEquals(0, actual.size()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void one_top_node() { |
| 94 | + selection.add(model.get("a")); |
| 95 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(selection); |
| 96 | + Assert.assertEquals(1, actual.size()); |
| 97 | + Assert.assertTrue(actual.contains(model.get("a"))); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void one_top_node_one_child() { |
| 102 | + selection.add(model.get("a")); |
| 103 | + selection.add(model.get("a.a.b.a")); |
| 104 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(selection); |
| 105 | + Assert.assertEquals(1, actual.size()); |
| 106 | + Assert.assertTrue(actual.contains(model.get("a"))); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void one_top_node_two_chidren() { |
| 111 | + selection.add(model.get("a.b")); |
| 112 | + selection.add(model.get("a")); |
| 113 | + selection.add(model.get("a.a.b.a")); |
| 114 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(selection); |
| 115 | + Assert.assertEquals(1, actual.size()); |
| 116 | + Assert.assertTrue(actual.contains(model.get("a"))); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void one_top_node_three_chidren() { |
| 121 | + selection.add(model.get("a")); |
| 122 | + selection.add(model.get("a.a.b.a")); |
| 123 | + selection.add(model.get("a.b")); |
| 124 | + selection.add(model.get("a.b.a")); |
| 125 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(selection); |
| 126 | + Assert.assertEquals(1, actual.size()); |
| 127 | + Assert.assertTrue(actual.contains(model.get("a"))); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void three_chidren() { |
| 132 | + selection.add(model.get("a.a.b.a")); |
| 133 | + selection.add(model.get("a.b")); |
| 134 | + selection.add(model.get("a.b.a")); |
| 135 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(selection); |
| 136 | + Assert.assertEquals(2, actual.size()); |
| 137 | + Assert.assertTrue(actual.contains(model.get("a.a.b.a"))); |
| 138 | + Assert.assertTrue(actual.contains(model.get("a.b"))); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void two_top_nodes() { |
| 143 | + selection.add(model.get("a")); |
| 144 | + selection.add(model.get("b")); |
| 145 | + Set<ItemNode> actual = ItemNode.createNonOverlappingSet(selection); |
| 146 | + Assert.assertEquals(2, actual.size()); |
| 147 | + Assert.assertTrue(actual.contains(model.get("a"))); |
| 148 | + Assert.assertTrue(actual.contains(model.get("b"))); |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments