diff options
11 files changed, 209 insertions, 306 deletions
diff --git a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerClusterConfigurerTest.java b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerClusterConfigurerTest.java index f437e6aa67d..330174f0313 100644 --- a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerClusterConfigurerTest.java +++ b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerClusterConfigurerTest.java @@ -6,12 +6,17 @@ import com.yahoo.cloud.config.ZookeepersConfig; import com.yahoo.jdisc.Metric; import com.yahoo.vespa.config.content.FleetcontrollerConfig; import com.yahoo.vespa.config.content.StorDistributionConfig; -import junit.framework.TestCase; +import org.junit.Test; import java.util.Map; -public class ClusterControllerClusterConfigurerTest extends TestCase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +public class ClusterControllerClusterConfigurerTest { + + @Test public void testSimple() throws Exception { ClusterController controller = new ClusterController(); StorDistributionConfig.Builder distributionConfig = new StorDistributionConfig.Builder(); diff --git a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerTest.java b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerTest.java index fcef483e7d7..b86d9a561d3 100644 --- a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerTest.java +++ b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/ClusterControllerTest.java @@ -8,13 +8,18 @@ package com.yahoo.vespa.clustercontroller.apps.clustercontroller; import com.yahoo.jdisc.Metric; import com.yahoo.vespa.clustercontroller.core.FleetController; import com.yahoo.vespa.clustercontroller.core.FleetControllerOptions; -import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; import java.util.Map; -public class ClusterControllerTest extends TestCase { +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ClusterControllerTest { private FleetControllerOptions options = new FleetControllerOptions("storage"); + private Metric metric = new Metric() { @Override public void set(String s, Number number, Context context) {} @@ -24,6 +29,7 @@ public class ClusterControllerTest extends TestCase { public Context createContext(Map<String, ?> stringMap) { return null; } }; + @Before public void setUp() { options = new FleetControllerOptions("storage"); options.zooKeeperServerAddress = null; @@ -31,9 +37,10 @@ public class ClusterControllerTest extends TestCase { options.slobrokConnectionSpecs = null; } + @Test public void testSimple() throws Exception { - // Cluster controller object keeps state and should never be remade, so should - // inject nothing + // Cluster controller object keeps state and should never be remade, so should + // inject nothing ClusterController cc = new ClusterController(); cc.setOptions("storage", options, metric); cc.setOptions("storage", options, metric); @@ -45,6 +52,7 @@ public class ClusterControllerTest extends TestCase { cc.deconstruct(); } + @Test public void testShutdownException() throws Exception { ClusterController cc = new ClusterController() { void shutdownController(FleetController controller) throws Exception { diff --git a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StateRestApiV2HandlerTest.java b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StateRestApiV2HandlerTest.java index dbc7834a601..e972d2ead39 100644 --- a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StateRestApiV2HandlerTest.java +++ b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StateRestApiV2HandlerTest.java @@ -3,16 +3,16 @@ package com.yahoo.vespa.clustercontroller.apps.clustercontroller; import com.yahoo.cloud.config.ClusterInfoConfig; import com.yahoo.vespa.clustercontroller.core.restapiv2.ClusterControllerStateRestAPI; -import junit.framework.TestCase; +import org.junit.Test; import java.util.Map; import java.util.TreeMap; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; -public class StateRestApiV2HandlerTest extends TestCase { +import static org.junit.Assert.assertEquals; +public class StateRestApiV2HandlerTest { + + @Test public void testNoMatchingSockets() { ClusterController controller = new ClusterController(); ClusterInfoConfig config = new ClusterInfoConfig( @@ -21,6 +21,7 @@ public class StateRestApiV2HandlerTest extends TestCase { new StateRestApiV2Handler(controller, config, StateRestApiV2Handler.testOnlyContext()); } + @Test public void testMappingOfIndexToClusterControllers() { ClusterInfoConfig.Builder builder = new ClusterInfoConfig.Builder() .clusterId("cluster-id") diff --git a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StatusHandlerTest.java b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StatusHandlerTest.java index 49e0a637368..49537597f83 100644 --- a/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StatusHandlerTest.java +++ b/clustercontroller-apps/src/test/java/com/yahoo/vespa/clustercontroller/apps/clustercontroller/StatusHandlerTest.java @@ -1,14 +1,11 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.clustercontroller.apps.clustercontroller; -import junit.framework.TestCase; +import org.junit.Test; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -public class StatusHandlerTest extends TestCase { +public class StatusHandlerTest { + @Test public void testSimple() { ClusterController controller = new ClusterController(); StatusHandler handler = new StatusHandler(controller, StatusHandler.testOnlyContext()); diff --git a/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/StateChangeHandlerTest.java b/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/StateChangeHandlerTest.java index b3716168300..7ef6d298885 100644 --- a/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/StateChangeHandlerTest.java +++ b/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/StateChangeHandlerTest.java @@ -3,19 +3,29 @@ package com.yahoo.vespa.clustercontroller.core; import com.yahoo.vdslib.distribution.ConfiguredNode; import com.yahoo.vdslib.distribution.Distribution; -import com.yahoo.vdslib.state.*; +import com.yahoo.vdslib.state.ClusterState; +import com.yahoo.vdslib.state.Node; +import com.yahoo.vdslib.state.NodeState; +import com.yahoo.vdslib.state.NodeType; +import com.yahoo.vdslib.state.State; import com.yahoo.vespa.clustercontroller.core.hostinfo.HostInfo; import com.yahoo.vespa.clustercontroller.core.listeners.NodeStateOrHostInfoChangeHandler; import com.yahoo.vespa.clustercontroller.core.mocks.TestEventLog; import com.yahoo.vespa.clustercontroller.core.testutils.LogFormatter; -import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + import java.util.LinkedList; import java.util.Set; import java.util.TreeSet; import java.util.logging.Logger; -public class StateChangeHandlerTest extends TestCase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class StateChangeHandlerTest { + private static final Logger log = Logger.getLogger(StateChangeHandlerTest.class.getName()); private class Config { int nodeCount = 3; @@ -61,6 +71,7 @@ public class StateChangeHandlerTest extends TestCase { private TestNodeStateOrHostInfoChangeHandler nodeStateUpdateListener; private final ClusterStateGenerator.Params params = new ClusterStateGenerator.Params(); + @Before public void setUp() { LogFormatter.initializeLogging(); } @@ -141,7 +152,8 @@ public class StateChangeHandlerTest extends TestCase { assertEquals(0, cluster.getNodeInfo(node).getPrematureCrashCount()); } - public void testUnstableNodeInSlobrok() throws Exception { + @Test + public void testUnstableNodeInSlobrok() { initialize(new Config()); startWithStableStateClusterWithNodesUp(); Node node = new Node(NodeType.STORAGE, 0); @@ -169,4 +181,5 @@ public class StateChangeHandlerTest extends TestCase { verifyPrematureCrashCountCleared(node); } } + } diff --git a/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ZooKeeperStressTest.java b/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ZooKeeperStressTest.java deleted file mode 100644 index e1e5e7c6c15..00000000000 --- a/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/ZooKeeperStressTest.java +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.core; - -import com.yahoo.vdslib.state.NodeState; -import com.yahoo.vdslib.state.Node; -import com.yahoo.vespa.clustercontroller.core.database.ZooKeeperDatabase; - -import java.util.Map; - -public class ZooKeeperStressTest extends junit.framework.TestCase { - private Object lock = new Object(); - private int waitTime = 0; - - class LoadGiver extends Thread { - ZooKeeperDatabase db; - public int count = 0; - public int errors = 0; - public int index; - public boolean stopNow = false; - - LoadGiver(ZooKeeperDatabase db, int index) { - this.db = db; - this.index = index; - } - - public void doStop() { - stopNow = true; - } - - public void run() { - try{ - while (!this.isInterrupted() && !stopNow) { - // Needs to take lock for each operation. Store new mastervote can not run at the same time as - // another store new master vote as they kill the ephemeral node - synchronized (lock) { - if (db.isClosed()) { System.err.println(this + " Session broke"); break; } - ++count; - if (db.retrieveLatestSystemStateVersion() == null) { - System.err.println("retrieveLatestSystemStateVersion() failed"); - ++errors; - } - } - Map<Node, NodeState> wantedStates; - synchronized (lock) { - if (db.isClosed()) { System.err.println(this + " Session broke"); break; } - ++count; - wantedStates = db.retrieveWantedStates(); - if (wantedStates == null) { - System.err.println("retrieveWantedStates() failed"); - ++errors; - } - } - synchronized (lock) { - if (db.isClosed()) { System.err.println(this + " Session broke"); break; } - ++count; - if (!db.storeLatestSystemStateVersion(5)) { - System.err.println("storeLastestSystemStateVersion() failed"); - ++errors; - } - } - synchronized (lock) { - if (db.isClosed()) { System.err.println(this + " Session broke"); break; } - ++count; - if (!db.storeMasterVote(0)) { - System.err.println("storeMasterVote() failed"); - ++errors; - } - } - synchronized (lock) { - if (db.isClosed()) { System.err.println(this + " Session broke"); break; } - if (wantedStates != null) { - ++count; - if (!db.storeWantedStates(wantedStates)) { - System.err.println("storeWantedState() failed"); - ++errors; - } - } - } - try{ Thread.sleep(waitTime); } catch (Exception e) {} - } - } catch (InterruptedException e) {} - } - - public String toString() { - return "LoadGiver(" + index + ": count " + count + ", errors " + errors + ")"; - } - } - - public void testNothing() throws Exception { - // Stupid junit fails if there's testclass without tests - } - - public void testZooKeeperStressed() throws Exception { - // Disabled for now.: Unstable - /* - ZooKeeperTestServer zooKeeperServer = new ZooKeeperTestServer(); - Database.DatabaseListener zksl = new Database.DatabaseListener() { - public void handleZooKeeperSessionDown() { - assertFalse("We lost session to ZooKeeper. Shouldn't happen", true); - } - - public void handleMasterData(Map<Integer, Integer> data) { - } - }; - VdsCluster cluster = new VdsCluster("mycluster", 10, 10, true); - int timeout = 30000; - ZooKeeperDatabase db = new ZooKeeperDatabase(cluster, 0, zooKeeperServer.getAddress(), timeout, zksl); - - Collection<LoadGiver> loadGivers = new ArrayList(); - long time = System.currentTimeMillis(); - for (int i = 0; i<10; ++i) { - loadGivers.add(new LoadGiver(db, i)); - } - for (LoadGiver lg : loadGivers) { - lg.start(); - } - for (int i = 0; i<30000; i += 100) { - Thread.sleep(100); - boolean failed = false; - for (LoadGiver lg : loadGivers) { - if (lg.errors > 0) { - failed = true; - } - } - if (failed) i += 5000; - } - int throughput = 0; - int errors = 0; - for (LoadGiver lg : loadGivers) { - assertTrue("Error check prior to attempting to stop: " + lg.toString(), lg.errors == 0); - } - for (LoadGiver lg : loadGivers) { - lg.doStop(); - throughput += lg.count; - errors += lg.errors; - } - time = System.currentTimeMillis() - time; - Double timesecs = new Double(time / 1000.0); - if (timesecs > 0.001) { - System.err.println("Throughput is " + (throughput / timesecs) + "msgs/sec, " + errors + " errors, total messages sent: " + throughput + ", waittime = " + waitTime); - } else { - System.err.println("too small time period " + time + " to calculate throughput"); - } - //try{ Thread.sleep(5000); } catch (Exception e) {} - for (LoadGiver lg : loadGivers) { - lg.join(); - } - for (LoadGiver lg : loadGivers) { - System.err.println(lg); - } - // Disabling test. This fails occasionally for some reason. - for (LoadGiver lg : loadGivers) { - // assertTrue("Error check after having stopped: " + lg.toString(), lg.errors == 0); - } - */ - } -} diff --git a/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/restapiv2/RequestTest.java b/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/restapiv2/RequestTest.java index d318b63f8c1..f76cfd13af1 100644 --- a/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/restapiv2/RequestTest.java +++ b/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/restapiv2/RequestTest.java @@ -2,15 +2,17 @@ package com.yahoo.vespa.clustercontroller.core.restapiv2; import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InternalFailure; -import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.StateRestApiException; -import junit.framework.TestCase; +import org.junit.Test; -public class RequestTest extends TestCase { +import static org.junit.Assert.assertTrue; +public class RequestTest { + + @Test public void testGetResultBeforeCompletion() { Request<String> r = new Request<String>(Request.MasterState.MUST_BE_MASTER) { @Override - public String calculateResult(Context context) throws StateRestApiException { + public String calculateResult(Context context) { return "foo"; } }; @@ -30,4 +32,5 @@ public class RequestTest extends TestCase { assertTrue(false); } } + } diff --git a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java index 15af9bd2c8c..1a1fe9603bf 100644 --- a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java +++ b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcherTest.java @@ -1,7 +1,14 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.clustercontroller.standalone; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + public class ClusterControllerConfigFetcherTest extends ClusterControllerTest { + + @Test public void testSimple() throws Exception { setFleetControllerConfigProperty(); setSlobrokConfigProperty(); @@ -16,6 +23,7 @@ public class ClusterControllerConfigFetcherTest extends ClusterControllerTest { configFetcher.close(); } + @Test public void testInitialConfigFailure() throws Exception { setFleetControllerConfigProperty(); setSlobrokConfigProperty(); @@ -35,6 +43,7 @@ public class ClusterControllerConfigFetcherTest extends ClusterControllerTest { } } + @Test public void testConfigUpdate() throws Exception { setFleetControllerConfigProperty(); setSlobrokConfigProperty(); @@ -49,4 +58,5 @@ public class ClusterControllerConfigFetcherTest extends ClusterControllerTest { }; configFetcher.updated(1000); } + } diff --git a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java index acd06e27b13..805a5d49330 100644 --- a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java +++ b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerTest.java @@ -2,7 +2,8 @@ package com.yahoo.vespa.clustercontroller.standalone; import com.yahoo.vdslib.distribution.Distribution; -import junit.framework.TestCase; +import org.junit.After; +import org.junit.Before; import java.io.File; import java.io.FileWriter; @@ -10,7 +11,8 @@ import java.io.IOException; import java.util.Map; import java.util.TreeMap; -public abstract class ClusterControllerTest extends TestCase { +public abstract class ClusterControllerTest { + private final Map<String, String> overriddenProperties = new TreeMap<>(); private File tempDirectory; @@ -28,12 +30,12 @@ public abstract class ClusterControllerTest extends TestCase { return f; } - @Override + @Before public void setUp() throws Exception { tempDirectory = createTemporaryDirectory(); } - @Override + @After public void tearDown() { for (Map.Entry<String, String> e : overriddenProperties.entrySet()) { if (e.getValue() == null) { @@ -93,4 +95,5 @@ public abstract class ClusterControllerTest extends TestCase { addDistributionConfig(); addZookeepersConfig(); } + } diff --git a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java index 0f20ccb8f9b..f2ff9576548 100644 --- a/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java +++ b/clustercontroller-standalone/src/test/java/com/yahoo/vespa/clustercontroller/standalone/StandAloneClusterControllerTest.java @@ -1,6 +1,12 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.clustercontroller.standalone; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + public class StandAloneClusterControllerTest extends ClusterControllerTest { abstract class Runner implements Runnable { @@ -19,6 +25,7 @@ public class StandAloneClusterControllerTest extends ClusterControllerTest { public abstract void attemptRun() throws Exception; } + @Test public void testSimpleStartStop() throws Exception { setupConfig(); ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher(); @@ -28,12 +35,13 @@ public class StandAloneClusterControllerTest extends ClusterControllerTest { controller.stop(); } + @Test public void testShortRun() throws Exception { setupConfig(); ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher() { int counter = 0; @Override - public boolean updated(long timeoutMillis) throws Exception { + public boolean updated(long timeoutMillis) { return (++counter % 2 == 0); } @Override @@ -66,30 +74,33 @@ public class StandAloneClusterControllerTest extends ClusterControllerTest { assertNull(r.exception); } + @Test public void testFailStart() throws Exception { setupConfig(); ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher(); StandAloneClusterController controller = new StandAloneClusterController(configFetcher) { @Override - public void start() throws Exception { + public void start() { throw new RuntimeException("Foo"); } }; StandAloneClusterController.runApplication(controller); } + @Test public void testFailRun() throws Exception { setupConfig(); ClusterControllerConfigFetcher configFetcher = new ClusterControllerConfigFetcher(); StandAloneClusterController controller = new StandAloneClusterController(configFetcher) { @Override - public void run() throws Exception { + public void run() { throw new RuntimeException("Foo"); } }; StandAloneClusterController.runApplication(controller); } + @Test public void testCallMainToGetCoverage() throws Exception { tearDown(); setProperty("config.id", "file:non-existing"); diff --git a/container-search/src/test/java/com/yahoo/fs4/test/QueryTestCase.java b/container-search/src/test/java/com/yahoo/fs4/test/QueryTestCase.java index 603e38e04f1..815919cd29e 100644 --- a/container-search/src/test/java/com/yahoo/fs4/test/QueryTestCase.java +++ b/container-search/src/test/java/com/yahoo/fs4/test/QueryTestCase.java @@ -5,88 +5,93 @@ import com.yahoo.fs4.BufferTooSmallException; import com.yahoo.fs4.Packet; import com.yahoo.fs4.QueryPacket; import com.yahoo.prelude.Freshness; -import com.yahoo.prelude.query.*; -import com.yahoo.prelude.querytransform.QueryRewrite; +import com.yahoo.prelude.query.AndItem; +import com.yahoo.prelude.query.Highlight; +import com.yahoo.prelude.query.PhraseItem; +import com.yahoo.prelude.query.PhraseSegmentItem; +import com.yahoo.prelude.query.WeightedSetItem; +import com.yahoo.prelude.query.WordItem; import com.yahoo.search.Query; +import org.junit.Test; import java.nio.ByteBuffer; -import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; /** * Tests encoding of query x packages * * @author bratseth */ -public class QueryTestCase extends junit.framework.TestCase { - - public QueryTestCase(String name) { - super(name); - } +public class QueryTestCase { - public void testEncodePacket() throws BufferTooSmallException { - Query query=new Query("/?query=chain&timeout=0"); + @Test + public void testEncodePacket() { + Query query = new Query("/?query=chain&timeout=0"); query.setWindow(2, 8); - QueryPacket packet=QueryPacket.create(query); - assertEquals(2,packet.getOffset()); - assertEquals(8,packet.getHits()); + QueryPacket packet = QueryPacket.create(query); + assertEquals(2, packet.getOffset()); + assertEquals(8, packet.getHits()); byte[] encoded = packetToBytes(packet); - byte[] correctBuffer=new byte[] {0,0,0,46,0,0,0,-38,0,0,0,0, // Header - 0,0,0,6, // Features - 2, - 8, - 0,0,0,1, // querytimeout - 0,0,0x40,0x03, // qflags - 7, - 'd', 'e', 'f', 'a', 'u', 'l', 't', - 0,0,0,1,0,0,0,8,4, - 0,5, - 99,104,97,105,110}; - assertEqualArrays(correctBuffer,encoded); + byte[] correctBuffer = new byte[] {0,0,0,46,0,0,0,-38,0,0,0,0, // Header + 0,0,0,6, // Features + 2, + 8, + 0,0,0,1, // querytimeout + 0,0,0x40,0x03, // qflags + 7, + 'd', 'e', 'f', 'a', 'u', 'l', 't', + 0,0,0,1,0,0,0,8,4, + 0,5, + 99,104,97,105,110}; + assertEqualArrays(correctBuffer, encoded); } - public void testEncodeQueryPacketWithSomeAdditionalFeatures() throws BufferTooSmallException { - Query query=new Query("/?query=chain&dataset=10&type=phrase&timeout=0"); + @Test + public void testEncodeQueryPacketWithSomeAdditionalFeatures() { + Query query = new Query("/?query=chain&dataset=10&type=phrase&timeout=0"); // Because the rank mapping now needs config and a searcher, // we do the sledgehammer dance: query.getRanking().setProfile("two"); query.setWindow(2, 8); - QueryPacket packet=QueryPacket.create(query); + QueryPacket packet = QueryPacket.create(query); byte[] encoded = packetToBytes(packet); - byte[] correctBuffer=new byte[] {0,0,0,42,0,0,0,-38,0,0,0,0, // Header - 0,0,0,6, // Features - 2, - 8, - 0,0,0,1, // querytimeout - 0,0,0x40,0x03, // QFlags - 3, - 't','w','o', // Ranking - 0,0,0,1,0,0,0,8,4, - 0,5, - 99,104,97,105,110}; + byte[] correctBuffer = new byte[] {0,0,0,42,0,0,0,-38,0,0,0,0, // Header + 0,0,0,6, // Features + 2, + 8, + 0,0,0,1, // querytimeout + 0,0,0x40,0x03, // QFlags + 3, + 't','w','o', // Ranking + 0,0,0,1,0,0,0,8,4, + 0,5, + 99,104,97,105,110}; assertEqualArrays(correctBuffer, encoded); } /** This test will tell you if you have screwed up the binary encoding, but it won't tell you how */ - public void testEncodeQueryPacketWithManyFeatures() throws BufferTooSmallException { - Query query=new Query("/?query=chain" + - "&ranking.features.query(foo)=30.3&ranking.features.query(bar)=0" + - "&ranking.properties.property.p1=v1&ranking.properties.property.p2=v2" + - "&pos.ll=S22.4532;W123.9887&pos.radius=3&pos.attribute=place&ranking.freshness=37" + - "&model.searchPath=7/3"); + @Test + public void testEncodeQueryPacketWithManyFeatures() { + Query query = new Query("/?query=chain" + + "&ranking.features.query(foo)=30.3&ranking.features.query(bar)=0" + + "&ranking.properties.property.p1=v1&ranking.properties.property.p2=v2" + + "&pos.ll=S22.4532;W123.9887&pos.radius=3&pos.attribute=place&ranking.freshness=37" + + "&model.searchPath=7/3"); query.getRanking().setFreshness(new Freshness("123456")); query.getRanking().setSorting("+field1 -field2"); query.getRanking().setProfile("two"); Highlight highlight = new Highlight(); - highlight.addHighlightTerm("field1","term1"); - highlight.addHighlightTerm("field1","term2"); + highlight.addHighlightTerm("field1", "term1"); + highlight.addHighlightTerm("field1", "term2"); query.getPresentation().setHighlight(highlight); query.prepare(); - QueryPacket packet=QueryPacket.create(query); + QueryPacket packet = QueryPacket.create(query); byte[] encoded = packetToBytes(packet); - // System.out.println(Arrays.toString(encoded)); byte[] correctBuffer=new byte[] { 0, 0, 1, 23, 0, 0, 0, -38, 0, 0, 0, 0, 0, 16, 0, -122, 0, 10, ignored, ignored, ignored, ignored, 0, 0, 0x40, 0x03, 3, 't', 'w', 'o', 0, 0, 0, 3, 0, 0, 0, 4, 'r', 'a', 'n', 'k', 0, 0, 0, 5, 0, 0, 0, 11, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'y', 46, 'p', '2', 0, 0, 0, 2, 'v', '2', 0, 0, 0, 11, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'y', 46, 'p', '1', 0, 0, 0, 2, 'v', '1', 0, 0, 0, 3, 'f', 'o', 'o', 0, 0, 0, 4, '3', '0', 46, '3', 0, 0, 0, 3, 'b', 'a', 'r', 0, 0, 0, 1, '0', 0, 0, 0, 9, 'v', 'e', 's', 'p', 'a', 46, 'n', 'o', 'w', 0, 0, 0, 6, '1', '2', '3', '4', '5', '6', 0, 0, 0, 14, 'h', 'i', 'g', 'h', 'l', 'i', 'g', 'h', 't', 't', 'e', 'r', 'm', 's', 0, 0, 0, 3, 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 1, '2', 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 5, 't', 'e', 'r', 'm', '1', 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 5, 't', 'e', 'r', 'm', '2', 0, 0, 0, 5, 'm', 'o', 'd', 'e', 'l', 0, 0, 0, 1, 0, 0, 0, 10, 's', 'e', 'a', 'r', 'c', 'h', 'p', 'a', 't', 'h', 0, 0, 0, 3, '7', 47, '3', 0, 0, 0, 15, 43, 'f', 'i', 'e', 'l', 'd', '1', 32, 45, 'f', 'i', 'e', 'l', 'd', '2', 0, 0, 0, 1, 0, 0, 0, 9, 68, 1, 0, 5, 'c', 'h', 'a', 'i', 'n' }; @@ -94,33 +99,34 @@ public class QueryTestCase extends junit.framework.TestCase { } /** This test will tell you if you have screwed up the binary encoding, but it won't tell you how */ - public void testEncodeQueryPacketWithManyFeaturesFresnhessAsString() throws BufferTooSmallException { - Query query=new Query("/?query=chain" + - "&ranking.features.query(foo)=30.3&ranking.features.query(bar)=0" + - "&ranking.properties.property.p1=v1&ranking.properties.property.p2=v2" + - "&pos.ll=S22.4532;W123.9887&pos.radius=3&pos.attribute=place&ranking.freshness=37" + - "&model.searchPath=7/3"); + @Test + public void testEncodeQueryPacketWithManyFeaturesFresnhessAsString() { + Query query = new Query("/?query=chain" + + "&ranking.features.query(foo)=30.3&ranking.features.query(bar)=0" + + "&ranking.properties.property.p1=v1&ranking.properties.property.p2=v2" + + "&pos.ll=S22.4532;W123.9887&pos.radius=3&pos.attribute=place&ranking.freshness=37" + + "&model.searchPath=7/3"); query.getRanking().setFreshness("123456"); query.getRanking().setSorting("+field1 -field2"); query.getRanking().setProfile("two"); Highlight highlight = new Highlight(); - highlight.addHighlightTerm("field1","term1"); - highlight.addHighlightTerm("field1","term2"); + highlight.addHighlightTerm("field1", "term1"); + highlight.addHighlightTerm("field1", "term2"); query.getPresentation().setHighlight(highlight); query.prepare(); - QueryPacket packet=QueryPacket.create(query); + QueryPacket packet = QueryPacket.create(query); byte[] encoded = packetToBytes(packet); - // System.out.println(Arrays.toString(encoded)); byte[] correctBuffer=new byte[] { 0, 0, 1, 23, 0, 0, 0, -38, 0, 0, 0, 0, 0, 16, 0, -122, 0, 10, ignored, ignored, ignored, ignored, 0, 0, 0x40, 0x03, 3, 't', 'w', 'o', 0, 0, 0, 3, 0, 0, 0, 4, 'r', 'a', 'n', 'k', 0, 0, 0, 5, 0, 0, 0, 11, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'y', 46, 'p', '2', 0, 0, 0, 2, 'v', '2', 0, 0, 0, 11, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'y', 46, 'p', '1', 0, 0, 0, 2, 'v', '1', 0, 0, 0, 3, 'f', 'o', 'o', 0, 0, 0, 4, '3', '0', 46, '3', 0, 0, 0, 3, 'b', 'a', 'r', 0, 0, 0, 1, '0', 0, 0, 0, 9, 'v', 'e', 's', 'p', 'a', 46, 'n', 'o', 'w', 0, 0, 0, 6, '1', '2', '3', '4', '5', '6', 0, 0, 0, 14, 'h', 'i', 'g', 'h', 'l', 'i', 'g', 'h', 't', 't', 'e', 'r', 'm', 's', 0, 0, 0, 3, 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 1, '2', 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 5, 't', 'e', 'r', 'm', '1', 0, 0, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 0, 0, 0, 5, 't', 'e', 'r', 'm', '2', 0, 0, 0, 5, 'm', 'o', 'd', 'e', 'l', 0, 0, 0, 1, 0, 0, 0, 10, 's', 'e', 'a', 'r', 'c', 'h', 'p', 'a', 't', 'h', 0, 0, 0, 3, '7', 47, '3', 0, 0, 0, 15, 43, 'f', 'i', 'e', 'l', 'd', '1', 32, 45, 'f', 'i', 'e', 'l', 'd', '2', 0, 0, 0, 1, 0, 0, 0, 9, 68, 1, 0, 5, 'c', 'h', 'a', 'i', 'n' }; - assertEqualArrays(correctBuffer,encoded); + assertEqualArrays(correctBuffer, encoded); } - public void testEncodeQueryPacketWithLabelsConnectivityAndSignificance() throws BufferTooSmallException { - Query query=new Query(); + @Test + public void testEncodeQueryPacketWithLabelsConnectivityAndSignificance() { + Query query = new Query(); AndItem and = new AndItem(); WeightedSetItem taggable1 = new WeightedSetItem("field1"); taggable1.setLabel("foo"); @@ -140,47 +146,49 @@ public class QueryTestCase extends junit.framework.TestCase { query.prepare(); - QueryPacket packet=QueryPacket.create(query); + QueryPacket packet = QueryPacket.create(query); byte[] encoded = packetToBytes(packet); byte[] correctBuffer=new byte[] { 0, 0, 1, 16, 0, 0, 0, -38, 0, 0, 0, 0, 0, 16, 0, 6, 0, 10, ignored, ignored, ignored, ignored, 0, 0, 0x40, 0x03, 7, 'd', 'e', 'f', 'a', 'u', 'l', 't', 0, 0, 0, 1, 0, 0, 0, 4, 'r', 'a', 'n', 'k', 0, 0, 0, 5, 0, 0, 0, 18, 'v', 'e', 's', 'p', 'a', 46, 'l', 'a', 'b', 'e', 'l', 46, 'b', 'a', 'r', 46, 'i', 'd', 0, 0, 0, 1, '1', 0, 0, 0, 22, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 'c', 'o', 'n', 'n', 'e', 'x', 'i', 't', 'y', 0, 0, 0, 1, '3', 0, 0, 0, 22, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 'c', 'o', 'n', 'n', 'e', 'x', 'i', 't', 'y', 0, 0, 0, 4, '0', 46, '1', '5', 0, 0, 0, 25, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '3', 46, 's', 'i', 'g', 'n', 'i', 'f', 'i', 'c', 'a', 'n', 'c', 'e', 0, 0, 0, 4, '0', 46, '3', '7', 0, 0, 0, 25, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 's', 'i', 'g', 'n', 'i', 'f', 'i', 'c', 'a', 'n', 'c', 'e', 0, 0, 0, 4, '0', 46, '8', '1', 0, 0, 0, 5, 0, 0, 0, '4', 1, 4, 79, 1, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 79, 2, 0, 6, 'f', 'i', 'e', 'l', 'd', '2', 68, 3, 6, 'f', 'i', 'e', 'l', 'd', '3', 5, 'w', 'o', 'r', 'd', '1', 68, 4, 6, 'f', 'i', 'e', 'l', 'd', '3', 5, 'w', 'o', 'r', 'd', 49 }; - assertEqualArrays(correctBuffer,encoded); + assertEqualArrays(correctBuffer, encoded); } + @Test public void testEncodeSortSpec() throws BufferTooSmallException { - Query query=new Query("/?query=chain&sortspec=%2Ba+-b&timeout=0"); + Query query = new Query("/?query=chain&sortspec=%2Ba+-b&timeout=0"); query.setWindow(2, 8); - QueryPacket packet=QueryPacket.create(query); - ByteBuffer buffer=ByteBuffer.allocate(500); + QueryPacket packet = QueryPacket.create(query); + ByteBuffer buffer = ByteBuffer.allocate(500); buffer.limit(0); packet.encode(buffer, 0); - byte[] encoded=new byte[buffer.position()]; + byte[] encoded = new byte[buffer.position()]; buffer.rewind(); buffer.get(encoded); - byte[] correctBuffer=new byte[] {0,0,0,55,0,0,0,-38,0,0,0,0, // Header - 0,0,0,-122, // Features - 2, // offset - 8, // maxhits - 0,0,0,1, // querytimeout - 0,0,0x40,0x03, // qflags - 7, - 'd', 'e', 'f', 'a', 'u', 'l', 't', - 0,0,0,5, // sortspec length - 43,97,32,45,98, // sortspec - 0,0,0,1, // num stackitems - 0,0,0,8,4, - 0,5, - 99,104,97,105,110}; - assertEqualArrays(correctBuffer,encoded); + byte[] correctBuffer = new byte[] {0,0,0,55,0,0,0,-38,0,0,0,0, // Header + 0,0,0,-122, // Features + 2, // offset + 8, // maxhits + 0,0,0,1, // querytimeout + 0,0,0x40,0x03, // qflags + 7, + 'd', 'e', 'f', 'a', 'u', 'l', 't', + 0,0,0,5, // sortspec length + 43,97,32,45,98, // sortspec + 0,0,0,1, // num stackitems + 0,0,0,8,4, + 0,5, + 99,104,97,105,110}; + assertEqualArrays(correctBuffer, encoded); // Encode again to test grantEncodingBuffer buffer = packet.grantEncodingBuffer(0); encoded = new byte[buffer.limit()]; buffer.get(encoded); - assertEqualArrays(correctBuffer,encoded); + assertEqualArrays(correctBuffer, encoded); } + @Test public void testPhraseEqualsPhraseWithPhraseSegment() throws BufferTooSmallException { Query query = new Query(); PhraseItem p = new PhraseItem(); @@ -220,47 +228,48 @@ public class QueryTestCase extends junit.framework.TestCase { assertEqualArrays(encoded2, encoded1); } - public void testPatchInChannelId() throws BufferTooSmallException { - Query query=new Query("/?query=chain&timeout=0"); + @Test + public void testPatchInChannelId() { + Query query = new Query("/?query=chain&timeout=0"); query.setWindow(2, 8); - QueryPacket packet=QueryPacket.create(query); + QueryPacket packet = QueryPacket.create(query); assertEquals(2,packet.getOffset()); assertEquals(8, packet.getHits()); ByteBuffer buffer = packet.grantEncodingBuffer(0x07070707); - byte[] correctBuffer=new byte[] {0,0,0,46,0,0,0,-38,7,7,7,7, // Header - 0,0,0,6, // Features - 2, - 8, - 0,0,0,1, // querytimeout - 0,0,0x40,0x03, // qflags - 7, - 'd', 'e', 'f', 'a', 'u', 'l', 't', - 0,0,0,1,0,0,0,8,4, - 0,5, - 99,104,97,105,110}; - - byte[] encoded=new byte[buffer.limit()]; + byte[] correctBuffer = new byte[] {0,0,0,46,0,0,0,-38,7,7,7,7, // Header + 0,0,0,6, // Features + 2, + 8, + 0,0,0,1, // querytimeout + 0,0,0x40,0x03, // qflags + 7, + 'd', 'e', 'f', 'a', 'u', 'l', 't', + 0,0,0,1,0,0,0,8,4, + 0,5, + 99,104,97,105,110}; + + byte[] encoded = new byte[buffer.limit()]; buffer.get(encoded); assertEqualArrays(correctBuffer,encoded); packet.allocateAndEncode(0x07070707); buffer = packet.grantEncodingBuffer(0x09090909); - correctBuffer=new byte[] {0,0,0,46,0,0,0,-38,9,9,9,9, // Header - 0,0,0,6, // Features - 2, - 8, - 0,0,0,1, // querytimeout - 0,0,0x40,0x03, // qflags - 7, - 'd', 'e', 'f', 'a', 'u', 'l', 't', - 0,0,0,1,0,0,0,8,4, - 0,5, - 99,104,97,105,110}; - - encoded=new byte[buffer.limit()]; + correctBuffer = new byte[] {0,0,0,46,0,0,0,-38,9,9,9,9, // Header + 0,0,0,6, // Features + 2, + 8, + 0,0,0,1, // querytimeout + 0,0,0x40,0x03, // qflags + 7, + 'd', 'e', 'f', 'a', 'u', 'l', 't', + 0,0,0,1,0,0,0,8,4, + 0,5, + 99,104,97,105,110}; + + encoded = new byte[buffer.limit()]; buffer.get(encoded); assertEqualArrays(correctBuffer,encoded); @@ -268,10 +277,10 @@ public class QueryTestCase extends junit.framework.TestCase { public static byte[] packetToBytes(Packet packet) { try { - ByteBuffer buffer=ByteBuffer.allocate(500); + ByteBuffer buffer = ByteBuffer.allocate(500); buffer.limit(0); - packet.encode(buffer,0); - byte[] encoded=new byte[buffer.position()]; + packet.encode(buffer, 0); + byte[] encoded = new byte[buffer.position()]; buffer.rewind(); buffer.get(encoded); return encoded; @@ -283,7 +292,7 @@ public class QueryTestCase extends junit.framework.TestCase { public static void assertEqualArrays(byte[] correct, byte[] test) { assertEquals("Incorrect length,", correct.length, test.length); - for (int i=0; i<correct.length; i++) { + for (int i = 0; i < correct.length; i++) { if (correct[i] == ignored) continue; // Special value used to ignore bytes we don't want to check assertEquals("Byte nr " + i, correct[i], test[i]); } |