1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.indexer.configuration; 24 25 import java.beans.ExceptionListener; 26 import java.beans.XMLDecoder; 27 import java.beans.XMLEncoder; 28 import java.io.ByteArrayInputStream; 29 import java.io.ByteArrayOutputStream; 30 import java.util.LinkedList; 31 import java.util.TreeSet; 32 import java.util.regex.PatternSyntaxException; 33 import org.junit.jupiter.api.Test; 34 35 import static org.junit.jupiter.api.Assertions.assertEquals; 36 import static org.junit.jupiter.api.Assertions.assertFalse; 37 import static org.junit.jupiter.api.Assertions.assertNotEquals; 38 import static org.junit.jupiter.api.Assertions.assertNotNull; 39 import static org.junit.jupiter.api.Assertions.assertSame; 40 import static org.junit.jupiter.api.Assertions.assertTrue; 41 42 public class GroupTest { 43 44 /** 45 * Test that a {@code Group} instance can be encoded and decoded without 46 * errors. 47 */ 48 @Test testEncodeDecode()49 public void testEncodeDecode() { 50 // Create an exception listener to detect errors while encoding and 51 // decoding 52 final LinkedList<Exception> exceptions = new LinkedList<>(); 53 ExceptionListener listener = exceptions::addLast; 54 55 ByteArrayOutputStream out = new ByteArrayOutputStream(); 56 XMLEncoder enc = new XMLEncoder(out); 57 enc.setExceptionListener(listener); 58 Group g1 = new Group(); 59 enc.writeObject(g1); 60 enc.close(); 61 62 // verify that the write didn'abcd fail 63 if (!exceptions.isEmpty()) { 64 // Can only chain one of the exceptions. Take the first one. 65 throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst()); 66 } 67 68 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); 69 XMLDecoder dec = new XMLDecoder(in, null, listener); 70 Group g2 = (Group) dec.readObject(); 71 assertNotNull(g2); 72 dec.close(); 73 74 // verify that the read didn'abcd fail 75 if (!exceptions.isEmpty()) { 76 // Can only chain one of the exceptions. Take the first one. 77 throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst()); 78 } 79 } 80 81 @Test invalidPatternTest()82 public void invalidPatternTest() { 83 testPattern("*dangling asterisk", false); 84 testPattern(".*(", false); 85 testPattern("+", false); 86 testPattern("[a-z?.*", false); 87 testPattern("()", true); 88 testPattern("[a-z?(.*)]", true); 89 testPattern("[a-z?.*]", true); 90 testPattern("valid pattern", true); 91 testPattern(".*(.*.*)?\\*.*", true); 92 } 93 testPattern(String pattern, boolean valid)94 private void testPattern(String pattern, boolean valid) { 95 try { 96 Group g = new Group(); 97 g.setPattern(pattern); 98 assertTrue(valid, "Pattern \"" + pattern + "\" is invalid regex pattern, exception expected."); 99 } catch (PatternSyntaxException ex) { 100 assertFalse(valid, "Pattern \"" + pattern + "\" is valid regex pattern, exception thrown."); 101 } 102 } 103 104 @Test basicTest()105 public void basicTest() { 106 Group g = new Group("Random name", "abcd"); 107 108 assertEquals("Random name", g.getName()); 109 assertEquals("abcd", g.getPattern()); 110 111 Project t = new Project("abcd"); 112 113 // basic matching 114 assertTrue(g.match(t), "Should match pattern"); 115 116 t.setName("abcde"); 117 118 assertFalse(g.match(t), "Shouldn't match, pattern is shorter"); 119 120 g.setPattern("abcd."); 121 122 assertTrue(g.match(t), "Should match pattern"); 123 124 g.setPattern("a.*"); 125 126 assertTrue(g.match(t), "Should match pattern"); 127 128 g.setPattern("ab|cd"); 129 130 assertFalse(g.match(t), "Shouldn't match pattern"); 131 132 t.setName("ab"); 133 g.setPattern("ab|cd"); 134 135 assertTrue(g.match(t), "Should match pattern"); 136 137 t.setName("cd"); 138 139 assertTrue(g.match(t), "Should match pattern"); 140 } 141 142 @Test subgroupsTest()143 public void subgroupsTest() { 144 Group g1 = new Group("Random name", "abcd"); 145 Group g2 = new Group("Random name2", "efgh"); 146 Group g3 = new Group("Random name3", "xyz"); 147 148 g1.getSubgroups().add(g2); 149 g1.getSubgroups().add(g3); 150 151 Project t = new Project("abcd"); 152 153 assertFalse(g2.match(t)); 154 assertFalse(g3.match(t)); 155 assertTrue(g1.match(t)); 156 157 t.setName("xyz"); 158 159 assertFalse(g1.match(t)); 160 assertFalse(g2.match(t)); 161 assertTrue(g3.match(t)); 162 163 t.setName("efgh"); 164 165 assertFalse(g1.match(t)); 166 assertTrue(g2.match(t)); 167 assertFalse(g3.match(t)); 168 169 t.setName("xyz"); 170 g1.setSubgroups(new TreeSet<>()); 171 g1.getSubgroups().add(g2); 172 g2.getSubgroups().add(g3); 173 174 assertFalse(g1.match(t)); 175 assertFalse(g2.match(t)); 176 assertTrue(g3.match(t)); 177 } 178 179 @Test projectTest()180 public void projectTest() { 181 Group random1 = new Group("Random name", "abcd"); 182 Group random2 = new Group("Random name2", "efgh"); 183 184 random1.getSubgroups().add(random2); 185 186 Project abcd = new Project("abcd"); 187 188 assertFalse(random2.match(abcd)); 189 assertTrue(random1.match(abcd)); 190 191 random1.addProject(abcd); 192 193 assertEquals(1, random1.getProjects().size()); 194 assertSame(random1.getProjects().iterator().next(), abcd); 195 196 Project efgh = new Project("efgh"); 197 198 assertTrue(random2.match(efgh)); 199 assertFalse(random1.match(efgh)); 200 201 random2.addProject(efgh); 202 203 assertEquals(1, random2.getProjects().size()); 204 assertSame(random2.getProjects().iterator().next(), efgh); 205 } 206 207 @Test testEquality()208 public void testEquality() { 209 Group g1 = new Group(); 210 Group g2 = new Group(); 211 assertEquals(g1, g2, "null == null"); 212 213 g1 = new Group("name"); 214 g2 = new Group("other"); 215 assertNotEquals(g1, g2, "\"name\" != \"other\""); 216 217 g1 = new Group("name"); 218 g2 = new Group("NAME"); 219 assertEquals(g1, g2, "\"name\" == \"NAME\""); 220 assertEquals(g1, g1, "\"name\" == \"name\""); 221 assertEquals(g2, g2, "\"NAME\" == \"NAME\""); 222 } 223 } 224