View Javadoc
1   /*
2    * This file is part of dependency-check-core.
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   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.lucene;
19  
20  import org.junit.After;
21  import org.junit.AfterClass;
22  import static org.junit.Assert.assertEquals;
23  import org.junit.Before;
24  import org.junit.BeforeClass;
25  import org.junit.Test;
26  
27  /**
28   *
29   * @author Jeremy Long
30   */
31  public class LuceneUtilsTest {
32  
33      @BeforeClass
34      public static void setUpClass() throws Exception {
35      }
36  
37      @AfterClass
38      public static void tearDownClass() throws Exception {
39      }
40  
41      @Before
42      public void setUp() {
43      }
44  
45      @After
46      public void tearDown() {
47      }
48  
49      /**
50       * Test of appendEscapedLuceneQuery method, of class LuceneUtils.
51       */
52      @Test
53      public void testAppendEscapedLuceneQuery() {
54          StringBuilder buf = new StringBuilder();
55          CharSequence text = "test encoding + - & | ! ( ) { } [ ] ^ \" ~ * ? : \\";
56          String expResult = "test encoding \\+ \\- \\& \\| \\! \\( \\) \\{ \\} \\[ \\] \\^ \\\" \\~ \\* \\? \\: \\\\";
57          LuceneUtils.appendEscapedLuceneQuery(buf, text);
58          assertEquals(expResult, buf.toString());
59      }
60  
61      /**
62       * Test of appendEscapedLuceneQuery method, of class LuceneUtils.
63       */
64      @Test
65      public void testAppendEscapedLuceneQuery_null() {
66          StringBuilder buf = new StringBuilder();
67          CharSequence text = null;
68          LuceneUtils.appendEscapedLuceneQuery(buf, text);
69          assertEquals(0, buf.length());
70      }
71  
72      /**
73       * Test of escapeLuceneQuery method, of class LuceneUtils.
74       */
75      @Test
76      public void testEscapeLuceneQuery() {
77          CharSequence text = "test encoding + - & | ! ( ) { } [ ] ^ \" ~ * ? : \\";
78          String expResult = "test encoding \\+ \\- \\& \\| \\! \\( \\) \\{ \\} \\[ \\] \\^ \\\" \\~ \\* \\? \\: \\\\";
79          String result = LuceneUtils.escapeLuceneQuery(text);
80          assertEquals(expResult, result);
81      }
82  
83      /**
84       * Test of escapeLuceneQuery method, of class LuceneUtils.
85       */
86      @Test
87      public void testEscapeLuceneQuery_null() {
88          CharSequence text = null;
89          String expResult = null;
90          String result = LuceneUtils.escapeLuceneQuery(text);
91          assertEquals(expResult, result);
92      }
93  }