838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger/*
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * CDDL HEADER START
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger *
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * The contents of this file are subject to the terms of the
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * Common Development and Distribution License (the "License").
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * You may not use this file except in compliance with the License.
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger *
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * See LICENSE.txt included in this distribution for the specific
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * language governing permissions and limitations under the License.
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger *
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * When distributing Covered Code, include this CDDL HEADER in each
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * file and include the License file at LICENSE.txt.
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * If applicable, add the following below this CDDL HEADER, with the
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * fields enclosed by brackets "[]" replaced with your own identifying
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * information: Portions Copyright [yyyy] [name of copyright owner]
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger *
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * CDDL HEADER END
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger */
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger /*
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger */
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerpackage org.opensolaris.opengrok.configuration.messages;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport java.util.Arrays;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport java.util.Date;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport java.util.TreeSet;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport org.junit.After;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport org.junit.Assert;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport org.junit.Before;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport org.junit.Test;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulingerpublic class NormalMessageTest {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger RuntimeEnvironment env;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger private Message[] makeArray(Message... messages) {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger return messages;
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger @Before
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger public void setUp() {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger env = RuntimeEnvironment.getInstance();
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger env.removeAllMessages();
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger @After
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger public void tearDown() {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger env.removeAllMessages();
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger @Test
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger public void testApplyNoTag() {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Message m = new NormalMessage();
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(0, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m.apply(env);
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(0, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger @Test
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger public void testApplySingle() {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Message m = new NormalMessage().addTag("main");
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(0, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m.apply(env);
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(1, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger @Test
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger public void testApplyMultiple() {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Message[] m = makeArray(new NormalMessage(), new NormalMessage(), new NormalMessage());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger for (int i = 0; i < m.length; i++) {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m[i].addTag("main");
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m[i].addTag("project");
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m[i].addTag("pull");
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m[i].setCreated(new Date(System.currentTimeMillis() + i * 1000));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(0, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger for (int i = 0; i < m.length; i++) {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m[i].apply(env);
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger // 3 * 3 - each message for each tag
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(3 * 3, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertNotNull(env.getMessages());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(3, env.getMessages().size());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertNotNull(env.getMessages("main"));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(3, env.getMessages("main").size());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(new TreeSet<Message>(Arrays.asList(m)), env.getMessages("main"));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertNotNull(env.getMessages("project"));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(3, env.getMessages("project").size());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(new TreeSet<Message>(Arrays.asList(m)), env.getMessages("project"));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertNotNull(env.getMessages("pull"));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(3, env.getMessages("pull").size());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(new TreeSet<Message>(Arrays.asList(m)), env.getMessages("pull"));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger @Test
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger public void testApplyMultipleUnique() {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Message[] m = makeArray(new NormalMessage(), new NormalMessage(), new NormalMessage());
58b5e3f01775316ef8ee80bb57dde8ad048806deKryštof Tulinger Date d = new Date();
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger for (int i = 0; i < m.length; i++) {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m[i].addTag("main");
58b5e3f01775316ef8ee80bb57dde8ad048806deKryštof Tulinger m[i].setCreated(d);
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(0, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger for (int i = 0; i < m.length; i++) {
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger m[i].apply(env);
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(1, env.getMessagesInTheSystem());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertNotNull(env.getMessages());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(1, env.getMessages().size());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertNotNull(env.getMessages("main"));
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger Assert.assertEquals(1, env.getMessages("main").size());
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger }
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger
838924562130977ca1a3d3839d146fcda39ea1afKryštof Tulinger}