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) 2021, 2022, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.indexer.history; 24 25 import org.junit.jupiter.api.Test; 26 27 import java.io.File; 28 import java.nio.file.Paths; 29 import java.util.Date; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.Set; 33 import java.util.TreeMap; 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.assertNull; 39 import static org.junit.jupiter.api.Assertions.assertTrue; 40 41 class HistoryTest { 42 private final List<HistoryEntry> entries = List.of( 43 new HistoryEntry("84599b3c", new Date(1485438707000L), 44 "Kryštof Tulinger <krystof.tulinger@oracle.com>", 45 " renaming directories\n\n", true, 46 Set.of(File.separator + Paths.get("git", "moved2", "renamed2.c"))), 47 new HistoryEntry("67dfbe26", new Date(1485263397000L), 48 "Kryštof Tulinger <krystof.tulinger@oracle.com>", 49 " renaming renamed -> renamed2\n\n", true, 50 Set.of(File.separator + Paths.get("git", "moved", "renamed2.c")))); 51 52 @Test testEqualsRenamed()53 void testEqualsRenamed() { 54 History history = new History(entries, 55 List.of(Paths.get("moved", "renamed2.c").toString(), 56 Paths.get("moved2", "renamed2.c").toString(), 57 Paths.get("moved", "renamed.c").toString())); 58 History historyNoRenames = new History(entries); 59 assertNotEquals(history, historyNoRenames); 60 } 61 62 @Test testEquals()63 void testEquals() { 64 History history = new History(entries); 65 assertEquals(2, history.getHistoryEntries().size()); 66 History historySmaller = new History(entries.subList(1, 2)); 67 assertEquals(1, historySmaller.getHistoryEntries().size()); 68 assertNotEquals(history, historySmaller); 69 } 70 71 @Test testAddTags()72 void testAddTags() { 73 History history = new History(); 74 HistoryEntry historyEntry = entries.get(0); 75 history.addTags(historyEntry, "foo"); 76 assertEquals("foo", history.getTags().get(historyEntry.getRevision())); 77 history.addTags(historyEntry, "bar"); 78 assertEquals("foo" + History.TAGS_SEPARATOR + "bar", history.getTags().get(historyEntry.getRevision())); 79 } 80 81 @Test testGetSetTags()82 void testGetSetTags() { 83 History history = new History(); 84 assertTrue(history.getTags().isEmpty()); 85 Map<String, String> tags = new TreeMap<>(); 86 tags.put("foo", "bar"); 87 tags.put("bar", "foo"); 88 history.setTags(tags); 89 assertFalse(history.getTags().isEmpty()); 90 assertEquals(tags, history.getTags()); 91 } 92 93 @Test testEqualsTagsEmpty()94 void testEqualsTagsEmpty() { 95 History history1 = new History(); 96 History history2 = new History(); 97 assertTrue(history1.getTags().isEmpty()); 98 assertTrue(history2.getTags().isEmpty()); 99 assertEquals(history1, history2); 100 } 101 102 @Test testEqualsTagsPositive()103 void testEqualsTagsPositive() { 104 History history1 = new History(); 105 History history2 = new History(); 106 history1.setTags(Map.of("foo", "bar", "bar", "foo")); 107 history2.setTags(Map.of("foo", "bar", "bar", "foo")); 108 assertEquals(history1, history2); 109 } 110 111 @Test testEqualsTagsNegative()112 void testEqualsTagsNegative() { 113 History history1 = new History(); 114 History history2 = new History(); 115 history1.setTags(Map.of("foo", "bar", "Bar", "foo")); 116 history2.setTags(Map.of("foo", "bar", "bar", "foo")); 117 assertNotEquals(history1, history2); 118 } 119 120 @Test testGetLastHistoryEntryEmpty()121 void testGetLastHistoryEntryEmpty() { 122 History history = new History(); 123 assertNull(history.getLastHistoryEntry()); 124 } 125 126 @Test testGetLastHistoryEntry()127 void testGetLastHistoryEntry() { 128 History history = new History(entries); 129 assertEquals(entries.get(0), history.getLastHistoryEntry()); 130 } 131 } 132