/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.search.model.impl;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.perforce.search.manager.SchemaKey;
import com.perforce.search.model.SearchField;
public class SearchModelImplTest {
static private Logger log = Logger.getLogger(SearchModelImplTest.class);
static private String timeBooster = "{!boost b=recip(ms(NOW,"
+ SchemaKey.MODIFIED_TIME + "),3.16e-11,1,1)}";
static private int contentProximity = 100;
static private int filenameProximity = 10;
@Test
public void testContentOnly() {
SearchModelImpl smi0 = new SearchModelImpl();
smi0.setQuery("c:unboxed");
final String targetResult = timeBooster + "(\"unboxed\"~"
+ String.valueOf(contentProximity) + ")";
log.debug("target: " + targetResult);
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
SearchModelImpl smi1 = new SearchModelImpl();
smi1.setQuery("content:unboxed");
Assert.assertEquals(targetResult, smi1.getQueryProcessed());
}
@Test
public void testFilenameSingleWord() {
SearchModelImpl smi0 = new SearchModelImpl();
smi0.setQuery("f:somefile");
final String targetResult = timeBooster
+ "(filename:somefile OR filename:*somefile*)";
log.debug("target: " + targetResult);
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
SearchModelImpl smi1 = new SearchModelImpl();
smi1.setQuery("files:somefile");
Assert.assertEquals(targetResult, smi1.getQueryProcessed());
}
@Test
public void testFilenameMultiword() {
SearchModelImpl smi0 = new SearchModelImpl();
smi0.setQuery("f:some file");
final String targetResult = timeBooster + "(filename:\"some file\"~"
+ String.valueOf(filenameProximity) + ")";
log.debug("target: " + targetResult);
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
SearchModelImpl smi1 = new SearchModelImpl();
smi1.setQuery("files:some file");
Assert.assertEquals(targetResult, smi1.getQueryProcessed());
}
@Test
public void testSimpleWordSearch() {
SearchModelImpl smi0 = new SearchModelImpl();
smi0.setQuery("somefile.doc");
final String targetResult = timeBooster
+ "((filename:somefile.doc OR filename:*somefile.doc*) OR (\"somefile.doc\"~"
+ String.valueOf(contentProximity) + "))";
log.debug("target: " + targetResult);
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
}
@Test
public void testMultiWordSearch() {
SearchModelImpl smi0 = new SearchModelImpl();
smi0.setQuery("more than one word");
final String targetResult = timeBooster
+ "((filename:\"more than one word\"~"
+ String.valueOf(filenameProximity)
+ ") OR (\"more than one word\"~"
+ String.valueOf(contentProximity) + "))";
log.debug("target: " + targetResult);
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
}
@Test
public void testWordPathSearch() {
SearchModelImpl smi0 = new SearchModelImpl();
final String targetResult = timeBooster
+ "((filename:\"search for it\"~"
+ String.valueOf(filenameProximity)
+ ") OR (\"search for it\"~"
+ String.valueOf(contentProximity)
+ ")) AND (id:\\/\\/depot\\/path\\/to\\/file\\/* OR id:\\/\\/commons\\/spaces\\/1\\/files\\/*)";
smi0.setQuery("search for it");
smi0.setPaths(Arrays.asList(new String[] { "//depot/path/to/file/...",
"//commons/spaces/1/files/..." }));
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
}
@Test
public void testFieldValueSearch() {
SearchModelImpl smi0 = new SearchModelImpl();
final String targetResult = timeBooster
+ "((filename:\"search for it\"~"
+ String.valueOf(filenameProximity)
+ ") OR (\"search for it\"~" + String.valueOf(contentProximity)
+ ")) AND (author:Tony* AND link:http\\:\\/\\/google.com)";
log.debug("target: " + targetResult);
smi0.setQuery("search for it");
smi0.setSearchFields(Arrays.asList(new SearchField[] {
new SearchFieldImpl("author", "Tony*"),
new SearchFieldImpl("link", "http://google.com") }));
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
}
@Test
public void testNoSearch() {
SearchModelImpl smi0 = new SearchModelImpl();
smi0.setQuery("");
Assert.assertEquals("", smi0.getQueryProcessed());
}
@Test
public void testEmbeddedColons() {
SearchModelImpl smi0 = new SearchModelImpl();
final String targetResult = timeBooster
+ "((filename:something\\:else OR "
+ "filename:*something\\:else*) OR " + "(\"something\\:else\"~"
+ String.valueOf(contentProximity) + "))";
smi0.setQuery("something:else");
Assert.assertEquals(targetResult, smi0.getQueryProcessed());
}
}