xref: /OpenGrok/opengrok-indexer/src/test/java/org/opengrok/indexer/search/HitTest.java (revision 52d10766ed1db3b0fd2c59a0da7292a32f244b50)
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) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package org.opengrok.indexer.search;
24 
25 import org.junit.jupiter.api.Test;
26 
27 import java.io.File;
28 
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertFalse;
31 import static org.junit.jupiter.api.Assertions.assertNotEquals;
32 import static org.junit.jupiter.api.Assertions.assertNull;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34 
35 /**
36  * Do basic sanity testing of the Hit class.
37  * @author Trond Norbye
38  */
39 public class HitTest {
40 
41     @Test
testFilename()42     public void testFilename() {
43         Hit instance = new Hit();
44         assertNull(instance.getFilename());
45         String expResult = "foobar";
46         instance.setFilename(expResult);
47         assertEquals(expResult, instance.getFilename());
48     }
49 
50     @Test
testPath()51     public void testPath() {
52         Hit instance = new Hit("/foo/bar", null, null, false, false);
53         assertEquals("/foo/bar", instance.getPath());
54         assertEquals(File.separator + "foo", instance.getDirectory());
55     }
56 
57     @Test
testLine()58     public void testLine() {
59         Hit instance = new Hit();
60         assertNull(instance.getLine());
61         String expResult = "This is a line of text";
62         instance.setLine(expResult);
63         assertEquals(expResult, instance.getLine());
64     }
65 
66     @Test
testLineno()67     public void testLineno() {
68         Hit instance = new Hit();
69         assertNull(instance.getLineno());
70         String expResult = "12";
71         instance.setLineno(expResult);
72         assertEquals(expResult, instance.getLineno());
73     }
74 
75     @Test
testCompareTo()76     public void testCompareTo() {
77         Hit o1 = new Hit("/foo", null, null, false, false);
78         Hit o2 = new Hit("/foo", "hi", "there", false, false);
79         assertEquals(o2.compareTo(o1), o1.compareTo(o2));
80         o1.setFilename("bar");
81         assertNotEquals(o2.compareTo(o1), o1.compareTo(o2));
82     }
83 
84     @Test
testBinary()85     public void testBinary() {
86         Hit instance = new Hit();
87         assertFalse(instance.isBinary());
88         instance.setBinary(true);
89         assertTrue(instance.isBinary());
90     }
91 
92     @Test
testTag()93     public void testTag() {
94         Hit instance = new Hit();
95         assertNull(instance.getTag());
96         String expResult = "foobar";
97         instance.setTag(expResult);
98         assertEquals(expResult, instance.getTag());
99     }
100 
101 
102     @Test
testAlt()103     public void testAlt() {
104         Hit instance = new Hit();
105         assertFalse(instance.getAlt());
106         Hit o2 = new Hit(null, null, null, false, true);
107         assertTrue(o2.getAlt());
108     }
109 
110     @Test
testEquals()111     public void testEquals() {
112         Hit o1 = new Hit("/foo", null, null, false, false);
113         Hit o2 = new Hit("/foo", "hi", "there", false, false);
114         assertEquals(o2.equals(o1), o1.equals(o2));
115         o1.setFilename("bar");
116         assertNotEquals(o2, o1);
117         assertNotEquals(o1, o2);
118         assertNotEquals(o1, new Object());
119     }
120 
121     @Test
testHashCode()122     public void testHashCode() {
123         String filename = "bar";
124         Hit instance = new Hit(filename, null, null, false, false);
125         assertEquals(filename.hashCode(), instance.hashCode());
126     }
127 }
128