From 8fba29168e99e86d0a8072adcaafb84337a16eb6 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Fri, 4 May 2018 14:26:29 +0200 Subject: Use new junit API --- .../vdslib/distribution/DistributionTestFactory.java | 20 ++++++++++++-------- .../com/yahoo/vdslib/distribution/GroupTestCase.java | 13 ++++++++++++- .../com/yahoo/vdslib/state/DiskStateTestCase.java | 18 ++++++++++++++---- .../com/yahoo/vdslib/state/NodeStateTestCase.java | 16 +++++++++++++++- .../test/java/com/yahoo/vdslib/state/NodeTest.java | 14 ++++++++++---- 5 files changed, 63 insertions(+), 18 deletions(-) (limited to 'vdslib/src') diff --git a/vdslib/src/test/java/com/yahoo/vdslib/distribution/DistributionTestFactory.java b/vdslib/src/test/java/com/yahoo/vdslib/distribution/DistributionTestFactory.java index 0b97950ae1e..7a6fb13bcd4 100644 --- a/vdslib/src/test/java/com/yahoo/vdslib/distribution/DistributionTestFactory.java +++ b/vdslib/src/test/java/com/yahoo/vdslib/distribution/DistributionTestFactory.java @@ -8,14 +8,18 @@ import com.yahoo.vdslib.state.Node; import com.yahoo.vdslib.state.NodeState; import com.yahoo.vdslib.state.NodeType; import com.yahoo.vespa.config.content.StorDistributionConfig; -import junit.framework.TestCase; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; import java.util.ArrayList; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + public class DistributionTestFactory extends CrossPlatformTestFactory { + private static final String testDirectory = "src/tests/distribution/testdata"; private int redundancy; private int nodeCount; @@ -82,22 +86,22 @@ public class DistributionTestFactory extends CrossPlatformTestFactory { for (int i=0; i 0) TestCase.assertEquals(toString(), Failure.NONE, failure); - TestCase.assertEquals(toString(), count, nodes.size()); + if (count > 0) assertEquals(toString(), Failure.NONE, failure); + assertEquals(toString(), count, nodes.size()); return this; } public Test assertNodeUsed(int node) { - TestCase.assertEquals(toString(), Failure.NONE, failure); - TestCase.assertTrue(toString(), nodes.contains(node)); + assertEquals(toString(), Failure.NONE, failure); + assertTrue(toString(), nodes.contains(node)); return this; } } @@ -158,7 +162,7 @@ public class DistributionTestFactory extends CrossPlatformTestFactory { } void verifySame(Test javaTest, Test other) { - TestCase.assertEquals("Reference test " + testsRecorded + " differ.", other, javaTest); + assertEquals("Reference test " + testsRecorded + " differ.", other, javaTest); ++testsVerified; } diff --git a/vdslib/src/test/java/com/yahoo/vdslib/distribution/GroupTestCase.java b/vdslib/src/test/java/com/yahoo/vdslib/distribution/GroupTestCase.java index e8aac633fd5..c71216b192d 100644 --- a/vdslib/src/test/java/com/yahoo/vdslib/distribution/GroupTestCase.java +++ b/vdslib/src/test/java/com/yahoo/vdslib/distribution/GroupTestCase.java @@ -1,13 +1,18 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vdslib.distribution; +import org.junit.Test; + import java.text.ParseException; import java.util.*; import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; -public class GroupTestCase extends junit.framework.TestCase { +public class GroupTestCase { private void assertDistribution(String spec, int redundancy, String expectedResult) throws ParseException { Group.Distribution distribution = new Group.Distribution(spec, redundancy); @@ -30,6 +35,7 @@ public class GroupTestCase extends junit.framework.TestCase { } } + @Test public void testStarConversion() throws ParseException { assertDistribution("1|*|*", 5, "2,2,1"); assertDistribution("1|*|*", 6, "3,2,1"); @@ -116,6 +122,7 @@ public class GroupTestCase extends junit.framework.TestCase { return root; } + @Test public void testNormalusage() throws ParseException { Group root = new Group(2, "myroot", new Group.Distribution("*", 2)); assertFalse(root.isLeafGroup()); @@ -147,11 +154,13 @@ public class GroupTestCase extends junit.framework.TestCase { return new Group.Distribution("*", 1); } + @Test public void testRootGroupDoesNotIncludeGroupNameWhenNoChildren() { Group g = new Group(0, "donkeykong"); assertThat(g.getUnixStylePath(), is("/")); } + @Test public void testChildNamesDoNotIncludeRootGroupName() throws Exception { Group g = new Group(0, "donkeykong", dummyDistribution()); Group child = new Group(1, "mario"); @@ -159,6 +168,7 @@ public class GroupTestCase extends junit.framework.TestCase { assertThat(child.getUnixStylePath(), is("/mario")); } + @Test public void testNestedGroupsAreSlashSeparated() throws Exception { Group g = new Group(0, "donkeykong", dummyDistribution()); Group mario = new Group(1, "mario", dummyDistribution()); @@ -169,6 +179,7 @@ public class GroupTestCase extends junit.framework.TestCase { assertThat(toad.getUnixStylePath(), is("/mario/toad")); } + @Test public void testMultipleLeafGroupsAreEnumerated() throws Exception { Group g = new Group(0, "donkeykong", dummyDistribution()); Group mario = new Group(1, "mario", dummyDistribution()); diff --git a/vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java b/vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java index 6357213d7f3..bdc3be3dc70 100644 --- a/vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java +++ b/vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java @@ -1,10 +1,19 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vdslib.state; +import org.junit.Test; + import java.text.ParseException; -public class DiskStateTestCase extends junit.framework.TestCase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DiskStateTestCase { + + private static final double delta = 0.0000000001; + @Test public void testEquals() { DiskState d1 = new DiskState(State.UP, "", 1); DiskState d2 = new DiskState(State.UP, "", 2); @@ -39,13 +48,14 @@ public class DiskStateTestCase extends junit.framework.TestCase { assertFalse(d1.equals("class not instance of Node")); } + @Test public void testSerialization() throws ParseException { DiskState d = new DiskState(); DiskState other = new DiskState(d.serialize("", true)); assertEquals(d, other); assertEquals(d.toString(), other.toString()); assertEquals(State.UP, other.getState()); - assertEquals(1.0, other.getCapacity()); + assertEquals(1.0, other.getCapacity(), delta); assertEquals("", other.getDescription()); assertEquals("s:u", d.serialize("", false)); assertEquals("s:u", d.serialize("", true)); @@ -59,7 +69,7 @@ public class DiskStateTestCase extends junit.framework.TestCase { assertEquals(d, other); assertEquals(d.toString(), other.toString()); assertEquals(State.UP, other.getState()); - assertEquals(1.0, other.getCapacity()); + assertEquals(1.0, other.getCapacity(), delta); assertEquals("Slow disk", other.getDescription()); assertEquals("s:u", d.serialize("", false)); assertEquals("s:u m:Slow\\x20disk", d.serialize("", true)); @@ -71,7 +81,7 @@ public class DiskStateTestCase extends junit.framework.TestCase { assertEquals(d, other); assertEquals(d.toString(), other.toString()); assertEquals(State.DOWN, other.getState()); - assertEquals(2.0, other.getCapacity()); + assertEquals(2.0, other.getCapacity(), delta); assertEquals("Failed disk", other.getDescription()); assertEquals("s:d c:2.0", d.serialize("", false)); assertEquals("s:d c:2.0 m:Failed\\x20disk", d.serialize("", true)); diff --git a/vdslib/src/test/java/com/yahoo/vdslib/state/NodeStateTestCase.java b/vdslib/src/test/java/com/yahoo/vdslib/state/NodeStateTestCase.java index 8f5d8bdbbe2..bbd9f8b6d56 100644 --- a/vdslib/src/test/java/com/yahoo/vdslib/state/NodeStateTestCase.java +++ b/vdslib/src/test/java/com/yahoo/vdslib/state/NodeStateTestCase.java @@ -1,11 +1,18 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vdslib.state; +import org.junit.Test; + import java.text.ParseException; import java.util.List; -public class NodeStateTestCase extends junit.framework.TestCase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class NodeStateTestCase { + @Test public void testOrdinals() { NodeType nt = NodeType.STORAGE; @@ -34,6 +41,7 @@ public class NodeStateTestCase extends junit.framework.TestCase { assertTrue(new NodeState(nt, State.UP).above(new NodeState(nt, State.RETIRED))); } + @Test public void testTrivialitiesToIncreaseCoverage() throws ParseException { NodeState ns = new NodeState(NodeType.STORAGE, State.UP); assertEquals(1, ns.getReliability()); @@ -47,6 +55,7 @@ public class NodeStateTestCase extends junit.framework.TestCase { assertEquals(ns, NodeState.deserialize(NodeType.STORAGE, "s:u d:1 d.0:d")); } + @Test public void testDiskState() throws ParseException { NodeState ns = NodeState.deserialize(NodeType.STORAGE, "s:m"); assertEquals(new DiskState(State.UP, "", 1), ns.getDiskState(0)); @@ -82,6 +91,7 @@ public class NodeStateTestCase extends junit.framework.TestCase { } catch (Exception e) {} } + @Test public void testSerialization() throws ParseException { NodeState ns = new NodeState(NodeType.STORAGE, State.MAINTENANCE); assertEquals("s:m", ns.serialize(false)); @@ -155,6 +165,7 @@ public class NodeStateTestCase extends junit.framework.TestCase { assertEquals(ns.serialize(true), copy2.serialize(true)); } + @Test public void testSimilarTo() { { NodeState ns1 = new NodeState(NodeType.STORAGE, State.INITIALIZING).setInitProgress(0); @@ -203,6 +214,7 @@ public class NodeStateTestCase extends junit.framework.TestCase { } } + @Test public void testReadableOutput() { // toString() and getDiff() is mostly there just to make good error reports when unit tests fails. Make sure toString() is actually run with no test failures // to make sure coverage doesn't complain when no test is failing. @@ -219,6 +231,7 @@ public class NodeStateTestCase extends junit.framework.TestCase { assertEquals(expected, ns1.getTextualDifference(new NodeState(NodeType.STORAGE, State.UP)).substring(0, expected.length())); } + @Test public void testValidInClusterState() { try{ new NodeState(NodeType.DISTRIBUTOR, State.UNKNOWN).verifyValidInSystemState(NodeType.DISTRIBUTOR); @@ -237,4 +250,5 @@ public class NodeStateTestCase extends junit.framework.TestCase { assertTrue("Should not be valid", false); } catch (Exception e) {} } + } diff --git a/vdslib/src/test/java/com/yahoo/vdslib/state/NodeTest.java b/vdslib/src/test/java/com/yahoo/vdslib/state/NodeTest.java index edd28b318ff..ce0af6254cb 100644 --- a/vdslib/src/test/java/com/yahoo/vdslib/state/NodeTest.java +++ b/vdslib/src/test/java/com/yahoo/vdslib/state/NodeTest.java @@ -1,11 +1,15 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vdslib.state; -/** - * - */ -public class NodeTest extends junit.framework.TestCase { +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class NodeTest { + + @Test public void testEquals() { Node n1 = new Node(NodeType.STORAGE, 6); Node n2 = new Node(NodeType.STORAGE, 7); @@ -44,6 +48,7 @@ public class NodeTest extends junit.framework.TestCase { assertFalse(n1.equals("class not instance of Node")); } + @Test public void testSerialization() { Node n = new Node(NodeType.STORAGE, 6); Node other = new Node(n.toString()); @@ -75,6 +80,7 @@ public class NodeTest extends junit.framework.TestCase { } } + @Test public void testMaySetWantedState() { assertTrue(State.UP.maySetWantedStateForThisNodeState(State.DOWN)); assertTrue(State.UP.maySetWantedStateForThisNodeState(State.MAINTENANCE)); -- cgit v1.2.3