diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-05-04 14:26:29 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-05-04 14:26:29 +0200 |
commit | 8fba29168e99e86d0a8072adcaafb84337a16eb6 (patch) | |
tree | 801101c3c410fc85e4fee445fa897eef1f411fb8 /vespajlib | |
parent | 8d41cf849b2e56bde07585bcf7ef1c1416b7b49c (diff) |
Use new junit API
Diffstat (limited to 'vespajlib')
11 files changed, 206 insertions, 157 deletions
diff --git a/vespajlib/src/test/java/com/yahoo/binaryprefix/BinaryScaledAmountTestCase.java b/vespajlib/src/test/java/com/yahoo/binaryprefix/BinaryScaledAmountTestCase.java index bd5d176095e..4bfb0441668 100644 --- a/vespajlib/src/test/java/com/yahoo/binaryprefix/BinaryScaledAmountTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/binaryprefix/BinaryScaledAmountTestCase.java @@ -1,12 +1,16 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.binaryprefix; -import junit.framework.TestCase; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; /** * @author tonytv */ -public class BinaryScaledAmountTestCase extends TestCase { +public class BinaryScaledAmountTestCase { + + @Test public void testConversion() { BinaryScaledAmount oneMeg = new BinaryScaledAmount(1024, BinaryPrefix.kilo); @@ -21,6 +25,7 @@ public class BinaryScaledAmountTestCase extends TestCase { assertEquals(true, oneMeg.equals(v)); } + @Test public void testSymbols() { BinaryScaledAmount oneMeg = new BinaryScaledAmount(1024, BinaryPrefix.kilo); @@ -35,4 +40,5 @@ public class BinaryScaledAmountTestCase extends TestCase { } assertEquals(true, ex); } + } diff --git a/vespajlib/src/test/java/com/yahoo/collections/BobHashTestCase.java b/vespajlib/src/test/java/com/yahoo/collections/BobHashTestCase.java index e1c7a01164e..f1835119d7e 100644 --- a/vespajlib/src/test/java/com/yahoo/collections/BobHashTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/collections/BobHashTestCase.java @@ -1,21 +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.collections; +import org.junit.Test; -import com.yahoo.collections.BobHash; - +import static org.junit.Assert.assertEquals; /** * Basic consistency check of BobHash implementation * - * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + * @author Steinar Knutsen */ -public class BobHashTestCase extends junit.framework.TestCase { - - public BobHashTestCase(String name) { - super(name); - } +public class BobHashTestCase { + @Test public void testit() { // Teststring: minprice // Basic ASCII string @@ -42,4 +39,5 @@ public class BobHashTestCase extends junit.framework.TestCase { assertEquals(BobHash.hash(lastnamefirstinitial, 0), 0xF36B4BD3); } + } diff --git a/vespajlib/src/test/java/com/yahoo/collections/MD5TestCase.java b/vespajlib/src/test/java/com/yahoo/collections/MD5TestCase.java index 86166a1e874..3be483106f2 100644 --- a/vespajlib/src/test/java/com/yahoo/collections/MD5TestCase.java +++ b/vespajlib/src/test/java/com/yahoo/collections/MD5TestCase.java @@ -1,10 +1,17 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.collections; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** - * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a> + * @author Einar M R Rosenvinge */ -public class MD5TestCase extends junit.framework.TestCase { +public class MD5TestCase { + + @Test public void testMD5() { MD5 md5 = new MD5(); int a = md5.hash("foobar"); @@ -30,4 +37,5 @@ public class MD5TestCase extends junit.framework.TestCase { assertTrue((c & 0x0000FF00) != 0); assertTrue((c & 0x000000FF) != 0); } + } diff --git a/vespajlib/src/test/java/com/yahoo/concurrent/EventBarrierTestCase.java b/vespajlib/src/test/java/com/yahoo/concurrent/EventBarrierTestCase.java index 6a91c502571..7946e8ebc56 100644 --- a/vespajlib/src/test/java/com/yahoo/concurrent/EventBarrierTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/concurrent/EventBarrierTestCase.java @@ -1,13 +1,17 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.concurrent; -import junit.framework.TestCase; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** - * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + * @author Simon Thoresen */ -public class EventBarrierTestCase extends TestCase { +public class EventBarrierTestCase { + @Test public void testEmpty() { // waiting for an empty set of events Barrier b = new Barrier(); @@ -27,6 +31,7 @@ public class EventBarrierTestCase extends TestCase { assertEquals(eb.getNumBarriers(), 0); } + @Test public void testSimple() { // a single barrier waiting for a single event Barrier b = new Barrier(); @@ -49,6 +54,7 @@ public class EventBarrierTestCase extends TestCase { assertEquals(eb.getNumBarriers(), 0); } + @Test public void testBarrierChain() { // more than one barrier waiting for the same set of events Barrier b1 = new Barrier(); @@ -80,6 +86,7 @@ public class EventBarrierTestCase extends TestCase { assertEquals(eb.getNumBarriers(), 0); } + @Test public void testEventAfter() { // new events starting after the start of a barrier Barrier b = new Barrier(); @@ -111,6 +118,7 @@ public class EventBarrierTestCase extends TestCase { assertEquals(eb.getNumBarriers(), 0); } + @Test public void testReorder() { // events completing in a different order than they started Barrier b1 = new Barrier(); @@ -165,4 +173,5 @@ public class EventBarrierTestCase extends TestCase { done = true; } } + } diff --git a/vespajlib/src/test/java/com/yahoo/geo/BoundingBoxParserTestCase.java b/vespajlib/src/test/java/com/yahoo/geo/BoundingBoxParserTestCase.java index 54b0bc49294..9602bdb8d94 100644 --- a/vespajlib/src/test/java/com/yahoo/geo/BoundingBoxParserTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/geo/BoundingBoxParserTestCase.java @@ -1,36 +1,39 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.geo; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + /** * Tests for the BoundingBoxParser class. * * @author arnej27959 */ -public class BoundingBoxParserTestCase extends junit.framework.TestCase { +public class BoundingBoxParserTestCase { - private BoundingBoxParser parser; + private static final double delta = 0.000000000001; - public BoundingBoxParserTestCase(String name) { - super(name); - } + private BoundingBoxParser parser; private void allZero(BoundingBoxParser data) { - assertEquals(0d, data.n); - assertEquals(0d, data.s); - assertEquals(0d, data.e); - assertEquals(0d, data.w); + assertEquals(0d, data.n, delta); + assertEquals(0d, data.s, delta); + assertEquals(0d, data.e, delta); + assertEquals(0d, data.w, delta); } private void all1234(BoundingBoxParser data) { - assertEquals(1d, data.n); - assertEquals(2d, data.s); - assertEquals(3d, data.e); - assertEquals(4d, data.w); + assertEquals(1d, data.n, delta); + assertEquals(2d, data.s, delta); + assertEquals(3d, data.e, delta); + assertEquals(4d, data.w, delta); } /** * Tests different inputs that should all produce 0 */ + @Test public void testZero() { parser = new BoundingBoxParser("n=0,s=0,e=0,w=0"); allZero(parser); @@ -48,6 +51,7 @@ public class BoundingBoxParserTestCase extends junit.framework.TestCase { allZero(parser); } + @Test public void testOneTwoThreeFour() { parser = new BoundingBoxParser("n=1,s=2,e=3,w=4"); all1234(parser); @@ -68,12 +72,14 @@ public class BoundingBoxParserTestCase extends junit.framework.TestCase { /** * Tests various legal inputs and print the output */ + @Test public void testPrint() { String here = "n=63.418417 E=10.433033 S=37.7 W=-122.02"; parser = new BoundingBoxParser(here); System.out.println(here+" -> "+parser); } + @Test public void testGeoPlanetExample() { /* example XML: <boundingBox> @@ -98,6 +104,7 @@ public class BoundingBoxParserTestCase extends junit.framework.TestCase { assertEquals(-74.728798d, parser.e, 0.0000001); } + @Test public void testGwsExample() { /* example XML: <boundingbox> @@ -115,6 +122,7 @@ public class BoundingBoxParserTestCase extends junit.framework.TestCase { /** * Tests various inputs that contain syntax errors. */ + @Test public void testInputErrors() { String message = ""; try { @@ -159,4 +167,5 @@ public class BoundingBoxParserTestCase extends junit.framework.TestCase { } assertEquals("multiple limits for 'w' boundary", message); } + } diff --git a/vespajlib/src/test/java/com/yahoo/geo/DegreesParserTestCase.java b/vespajlib/src/test/java/com/yahoo/geo/DegreesParserTestCase.java index 79ce71071d5..2f12305cbb8 100644 --- a/vespajlib/src/test/java/com/yahoo/geo/DegreesParserTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/geo/DegreesParserTestCase.java @@ -1,154 +1,127 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.geo; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + /** * Tests for the DegreesParser class. * - * @author <a href="mailto:gunnarga@yahoo-inc.com">Gunnar Gauslaa Bergem</a> + * @author Gunnar Gauslaa Bergem */ -public class DegreesParserTestCase extends junit.framework.TestCase { +public class DegreesParserTestCase { - private DegreesParser parser; + private static final double delta = 0.000000000001; - public DegreesParserTestCase(String name) { - super(name); - } + private DegreesParser parser; /** * Tests different inputs that should all produce 0 or -0. */ + @Test public void testZero() { parser = new DegreesParser("N0;E0"); - assertEquals(0d, parser.latitude); - assertEquals(0d, parser.longitude); + assertEquals(0d, parser.latitude, delta); + assertEquals(0d, parser.longitude, delta); parser = new DegreesParser("S0;W0"); - assertEquals(-0d, parser.latitude); - assertEquals(-0d, parser.longitude); + assertEquals(-0d, parser.latitude, delta); + assertEquals(-0d, parser.longitude, delta); parser = new DegreesParser("N0.0;E0.0"); - assertEquals(0d, parser.latitude); - assertEquals(0d, parser.longitude); + assertEquals(0d, parser.latitude, delta); + assertEquals(0d, parser.longitude, delta); parser = new DegreesParser("S0.0;W0.0"); - assertEquals(-0d, parser.latitude); - assertEquals(-0d, parser.longitude); + assertEquals(-0d, parser.latitude, delta); + assertEquals(-0d, parser.longitude, delta); parser = new DegreesParser("N0\u00B00'0;E0\u00B00'0"); - assertEquals(0d, parser.latitude); - assertEquals(0d, parser.longitude); + assertEquals(0d, parser.latitude, delta); + assertEquals(0d, parser.longitude, delta); parser = new DegreesParser("S0\u00B00'0;W0\u00B00'0"); - assertEquals(-0d, parser.latitude); - assertEquals(-0d, parser.longitude); + assertEquals(-0d, parser.latitude, delta); + assertEquals(-0d, parser.longitude, delta); parser = new DegreesParser("S0o0'0;W0o0'0"); - assertEquals(-0d, parser.latitude); - assertEquals(-0d, parser.longitude); - } - - /** - * Tests various legal inputs and print the output - */ - public void testPrint() { - String here = "63N025.105;010E25.982"; - parser = new DegreesParser(here); - System.out.println(here+" -> "+parser.latitude+"/"+parser.longitude+" (lat/long)"); - - here = "N63.418417 E10.433033"; - parser = new DegreesParser(here); - System.out.println(here+" -> "+parser.latitude+"/"+parser.longitude+" (lat/long)"); - - here = "N63o025.105;E010o25.982"; - parser = new DegreesParser(here); - System.out.println(here+" -> "+parser.latitude+"/"+parser.longitude+" (lat/long)"); - - here = "N63.418417;E10.433033"; - parser = new DegreesParser(here); - System.out.println(here+" -> "+parser.latitude+"/"+parser.longitude+" (lat/long)"); - - here = "63.418417N;10.433033E"; - parser = new DegreesParser(here); - System.out.println(here+" -> "+parser.latitude+"/"+parser.longitude+" (lat/long)"); - - here = "N37.417075;W122.025358"; - parser = new DegreesParser(here); - System.out.println(here+" -> "+parser.latitude+"/"+parser.longitude+" (lat/long)"); - - here = "N37\u00B024.983;W122\u00B001.481"; - parser = new DegreesParser(here); - System.out.println(here+" -> "+parser.latitude+"/"+parser.longitude+" (lat/long)"); + assertEquals(-0d, parser.latitude, delta); + assertEquals(-0d, parser.longitude, delta); } /** * Tests inputs that are close to 0. */ + @Test public void testNearZero() { parser = new DegreesParser("N0.0001;E0.0001"); - assertEquals(0.0001, parser.latitude); - assertEquals(0.0001, parser.longitude); + assertEquals(0.0001, parser.latitude, delta); + assertEquals(0.0001, parser.longitude, delta); parser = new DegreesParser("S0.0001;W0.0001"); - assertEquals(-0.0001, parser.latitude); - assertEquals(-0.0001, parser.longitude); + assertEquals(-0.0001, parser.latitude, delta); + assertEquals(-0.0001, parser.longitude, delta); parser = new DegreesParser("N0.000001;E0.000001"); - assertEquals(0.000001, parser.latitude); - assertEquals(0.000001, parser.longitude); + assertEquals(0.000001, parser.latitude, delta); + assertEquals(0.000001, parser.longitude, delta); parser = new DegreesParser("S0.000001;W0.000001"); - assertEquals(-0.000001, parser.latitude); - assertEquals(-0.000001, parser.longitude); + assertEquals(-0.000001, parser.latitude, delta); + assertEquals(-0.000001, parser.longitude, delta); parser = new DegreesParser("N0\u00B00'1;E0\u00B00'1"); - assertEquals(1/3600d, parser.latitude); - assertEquals(1/3600d, parser.longitude); + assertEquals(1/3600d, parser.latitude, delta); + assertEquals(1/3600d, parser.longitude, delta); parser = new DegreesParser("S0\u00B00'1;W0\u00B00'1"); - assertEquals(-1/3600d, parser.latitude); - assertEquals(-1/3600d, parser.longitude); + assertEquals(-1/3600d, parser.latitude, delta); + assertEquals(-1/3600d, parser.longitude, delta); } /** * Tests inputs that are close to latitude 90/-90 degrees and longitude 180/-180 degrees. */ + @Test public void testNearBoundary() { - parser = new DegreesParser("N89.9999;E179.9999"); - assertEquals(89.9999, parser.latitude); - assertEquals(179.9999, parser.longitude); + assertEquals(89.9999, parser.latitude, delta); + assertEquals(179.9999, parser.longitude, delta); parser = new DegreesParser("S89.9999;W179.9999"); - assertEquals(-89.9999, parser.latitude); - assertEquals(-179.9999, parser.longitude); + assertEquals(-89.9999, parser.latitude, delta); + assertEquals(-179.9999, parser.longitude, delta); parser = new DegreesParser("N89.999999;E179.999999"); - assertEquals(89.999999, parser.latitude); - assertEquals(179.999999, parser.longitude); + assertEquals(89.999999, parser.latitude, delta); + assertEquals(179.999999, parser.longitude, delta); parser = new DegreesParser("S89.999999;W179.999999"); - assertEquals(-89.999999, parser.latitude); - assertEquals(-179.999999, parser.longitude); + assertEquals(-89.999999, parser.latitude, delta); + assertEquals(-179.999999, parser.longitude, delta); parser = new DegreesParser("N89\u00B059'59;E179\u00B059'59"); - assertEquals(89+59/60d+59/3600d, parser.latitude); - assertEquals(179+59/60d+59/3600d, parser.longitude); + assertEquals(89+59/60d+59/3600d, parser.latitude, delta); + assertEquals(179+59/60d+59/3600d, parser.longitude, delta); parser = new DegreesParser("S89\u00B059'59;W179\u00B059'59"); - assertEquals(-(89+59/60d+59/3600d), parser.latitude); - assertEquals(-(179+59/60d+59/3600d), parser.longitude); + assertEquals(-(89+59/60d+59/3600d), parser.latitude, delta); + assertEquals(-(179+59/60d+59/3600d), parser.longitude, delta); } /** * Tests inputs that are on latitude 90/-90 degrees and longitude 180/-180 degrees. */ + @Test public void testOnBoundary() { parser = new DegreesParser("N90;E180"); - assertEquals(90d, parser.latitude); - assertEquals(180d, parser.longitude); + assertEquals(90d, parser.latitude, delta); + assertEquals(180d, parser.longitude, delta); parser = new DegreesParser("S90;W180"); - assertEquals(-90d, parser.latitude); - assertEquals(-180d, parser.longitude); + assertEquals(-90d, parser.latitude, delta); + assertEquals(-180d, parser.longitude, delta); parser = new DegreesParser("N90\u00B00'0;E180\u00B00'0"); - assertEquals(90d, parser.latitude); - assertEquals(180d, parser.longitude); + assertEquals(90d, parser.latitude, delta); + assertEquals(180d, parser.longitude, delta); parser = new DegreesParser("S90\u00B00'0;W180\u00B00'0"); - assertEquals(-90d, parser.latitude); - assertEquals(-180d, parser.longitude); + assertEquals(-90d, parser.latitude, delta); + assertEquals(-180d, parser.longitude, delta); } /** * Tests inputs that are above latitude 90/-90 degrees and longitude 180/-180 degrees. - */ + */ + @Test public void testAboveBoundary() { String message = ""; try { @@ -204,6 +177,7 @@ public class DegreesParserTestCase extends junit.framework.TestCase { /** * Tests various inputs that contain syntax errors. */ + @Test public void testInputErrors() { String message = ""; try { @@ -279,4 +253,5 @@ public class DegreesParserTestCase extends junit.framework.TestCase { } assertEquals("invalid character: O", message); } + } diff --git a/vespajlib/src/test/java/com/yahoo/geo/ZCurveTestCase.java b/vespajlib/src/test/java/com/yahoo/geo/ZCurveTestCase.java index 2abbfa2f68a..b1a6149f956 100644 --- a/vespajlib/src/test/java/com/yahoo/geo/ZCurveTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/geo/ZCurveTestCase.java @@ -1,21 +1,22 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.geo; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + /** * Tests for the ZCurve class. * * @author gjoranv */ -public class ZCurveTestCase extends junit.framework.TestCase { - - public ZCurveTestCase(String name) { - super(name); - } +public class ZCurveTestCase { /** - * Verify that encoded values return the expected bit pattern - */ - public void testEncoding() { + * Verify that encoded values return the expected bit pattern + */ + @Test + public void testEncoding() { int x = 0; int y = 0; long z = ZCurve.encode(x, y); @@ -45,11 +46,12 @@ public class ZCurveTestCase extends junit.framework.TestCase { y = Integer.MIN_VALUE / 2; z = ZCurve.encode(x, y); assertEquals(0xa555555555555555L, z); - } + } /** * Verify that decoded values are equal to inputs in different cases */ + @Test public void testDecoding() { int x = 0; int y = 0; @@ -94,11 +96,10 @@ public class ZCurveTestCase extends junit.framework.TestCase { assertEquals(y, xy[1]); } - - /** * Verify that encoded values return the expected bit pattern */ + @Test public void testEncoding_slow() { int x = 0; int y = 0; @@ -134,6 +135,7 @@ public class ZCurveTestCase extends junit.framework.TestCase { /** * Verify that decoded values are equal to inputs in different cases */ + @Test public void testDecoding_slow() { int x = 0; int y = 0; @@ -178,6 +180,7 @@ public class ZCurveTestCase extends junit.framework.TestCase { assertEquals(xy[1], y); } + @Test public void testBenchmarkEncoding() { int limit = 2000000; @@ -187,8 +190,8 @@ public class ZCurveTestCase extends junit.framework.TestCase { z1 += ZCurve.encode(i,-i); } long elapsed = System.currentTimeMillis() - start; - System.out.println("Fast method: elapsed time: " + elapsed + " ms"); - System.out.println("Per encoding: " + elapsed/(1.0*limit) * 1000000 + " ns"); + //System.out.println("Fast method: elapsed time: " + elapsed + " ms"); + //System.out.println("Per encoding: " + elapsed/(1.0*limit) * 1000000 + " ns"); long z2 = 0L; start = System.currentTimeMillis(); @@ -196,8 +199,8 @@ public class ZCurveTestCase extends junit.framework.TestCase { z2 += ZCurve.encode_slow(i,-i); } elapsed = System.currentTimeMillis() - start; - System.out.println("Slow method: elapsed time: " + elapsed + " ms"); - System.out.println("Per encoding: " + elapsed/(1.0*limit) * 1000000 + " ns"); + //System.out.println("Slow method: elapsed time: " + elapsed + " ms"); + //System.out.println("Per encoding: " + elapsed/(1.0*limit) * 1000000 + " ns"); assertEquals(z1, z2); } diff --git a/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java b/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java index 5b03c63de54..c9018224b1c 100644 --- a/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/io/BlobTestCase.java @@ -1,16 +1,23 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.io; +import org.junit.Test; + import java.nio.ByteBuffer; -public class BlobTestCase extends junit.framework.TestCase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class BlobTestCase { + @Test public void testEmpty() { Blob empty = new Blob(); assertTrue(empty.get() != null); assertEquals(0, empty.get().length); } + @Test public void testCopyArray() { byte[] d = { 1, 2, 3 }; Blob b = new Blob(d); @@ -23,6 +30,7 @@ public class BlobTestCase extends junit.framework.TestCase { assertEquals(3, b.get()[2]); } + @Test public void testCopyArraySubset() { byte[] d = { 1, 2, 3 }; Blob b = new Blob(d, 1, 1); @@ -33,6 +41,7 @@ public class BlobTestCase extends junit.framework.TestCase { assertEquals(2, b.get()[0]); } + @Test public void testCopyBlob() { byte[] d = { 1, 2, 3 }; Blob b = new Blob(d); @@ -45,6 +54,7 @@ public class BlobTestCase extends junit.framework.TestCase { assertEquals(3, x.get()[2]); } + @Test public void testReadBuffer() { ByteBuffer buf = ByteBuffer.allocate(100); buf.put((byte)1); @@ -60,6 +70,7 @@ public class BlobTestCase extends junit.framework.TestCase { assertEquals(3, b.get()[2]); } + @Test public void testReadPartialBuffer() { ByteBuffer buf = ByteBuffer.allocate(100); buf.put((byte)1); @@ -80,6 +91,7 @@ public class BlobTestCase extends junit.framework.TestCase { assertEquals(0, buf.remaining()); } + @Test public void testWriteBuffer() { byte[] d = { 1, 2, 3 }; Blob b = new Blob(d); @@ -92,4 +104,5 @@ public class BlobTestCase extends junit.framework.TestCase { assertEquals(3, buf.get()); assertEquals(0, buf.remaining()); } + } diff --git a/vespajlib/src/test/java/com/yahoo/io/ByteWriterTestCase.java b/vespajlib/src/test/java/com/yahoo/io/ByteWriterTestCase.java index b34a0e4a2f9..c6278133d4c 100644 --- a/vespajlib/src/test/java/com/yahoo/io/ByteWriterTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/io/ByteWriterTestCase.java @@ -2,6 +2,9 @@ package com.yahoo.io; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -14,25 +17,18 @@ import java.util.IdentityHashMap; import com.yahoo.text.Utf8; import com.yahoo.text.Utf8Array; +import org.junit.Test; /** * Test the ByteWriter class. ByteWriter is also implicitly tested in * com.yahoo.prelude.templates.test.TemplateTestCase. * - * @author <a href="mailt:steinar@yahoo-inc.com">Steinar Knutsen</a> + * @author Steinar Knutsen */ -public class ByteWriterTestCase extends junit.framework.TestCase { - private CharsetEncoder encoder; - private ByteArrayOutputStream stream; +public class ByteWriterTestCase { - // TODO split BufferChain tests from ByteWriter tests - public ByteWriterTestCase (String name) { - super(name); - Charset cs = Charset.forName("UTF-8"); - encoder = cs.newEncoder(); - stream = new ByteArrayOutputStream(); - - } + private final CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); + private final ByteArrayOutputStream stream = new ByteArrayOutputStream(); /** * A stream which does nothing, but complains if it is called and asked to @@ -43,18 +39,17 @@ public class ByteWriterTestCase extends junit.framework.TestCase { static final String ZERO_LENGTH_WRITE = "Was asked to do zero length write."; @Override - public void write(int b) throws IOException { + public void write(int b) { // NOP - } @Override - public void close() throws IOException { + public void close() { // NOP } @Override - public void flush() throws IOException { + public void flush() { // NOP } @@ -74,6 +69,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { } + @Test public void testMuchData() throws java.io.IOException { final int SINGLE_BUFFER = 500; final int APPENDS = 500; @@ -96,6 +92,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue("ByteWriter seems to have introduced data errors.", Arrays.equals(completeData, res)); } + @Test public void testLongString() throws IOException { final int length = BufferChain.BUFFERSIZE * BufferChain.MAXBUFFERS * 3; StringBuilder b = new StringBuilder(length); @@ -112,6 +109,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertEquals(s, res); } + @Test public void testNoSpuriousWrite() throws IOException { OutputStream grumpy = new CurmudgeonlyStream(); ByteWriter bw = new ByteWriter(grumpy, encoder); @@ -145,6 +143,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { } } + @Test public void testDoubleFlush() throws IOException { stream.reset(); byte[] c = new byte[] { 97, 98, 99 }; @@ -157,6 +156,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(Arrays.equals(new byte[] { 97, 98, 99 }, res)); } + @Test public void testCharArrays() throws java.io.IOException { stream.reset(); char[] c = new char[] { 'a', 'b', 'c', '\u00F8' }; @@ -167,6 +167,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(Arrays.equals(new byte[] { 97, 98, 99, (byte) 0xc3, (byte) 0xb8 }, res)); } + @Test public void testByteBuffers() throws java.io.IOException { stream.reset(); ByteBuffer b = ByteBuffer.allocate(16); @@ -181,6 +182,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(Arrays.equals(new byte[] { 97, 98, 99 }, res)); } + @Test public void testByteArrays() throws java.io.IOException { stream.reset(); byte[] c = new byte[] { 97, 98, 99 }; @@ -191,6 +193,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(Arrays.equals(new byte[] { 97, 98, 99 }, res)); } + @Test public void testByteArrayWithOffset() throws java.io.IOException { final int length = BufferChain.BUFFERSIZE * 3 / 2; final int offset = 1; @@ -210,6 +213,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertEquals(valid, res[0]); } + @Test public void testStrings() throws java.io.IOException { stream.reset(); String c = "abc\u00F8"; @@ -220,6 +224,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(Arrays.equals(new byte[] { 97, 98, 99, (byte) 0xc3, (byte) 0xb8 }, res)); } + @Test public void testStringsAndByteArrays() throws java.io.IOException { stream.reset(); String c = "abc\u00F8"; @@ -232,6 +237,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(Arrays.equals(new byte[] { 97, 98, 99, (byte) 0xc3, (byte) 0xb8, 97, 98, 99 }, res)); } + @Test public void testByteBuffersAndByteArrays() throws java.io.IOException { stream.reset(); ByteBuffer b = ByteBuffer.allocate(16); @@ -248,6 +254,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(Arrays.equals(new byte[] { 97, 98, 99, 100, 101, 102 }, res)); } + @Test public void testOverFlow() throws java.io.IOException { stream.reset(); byte[] b = new byte[] { 97, 98, 99 }; @@ -271,6 +278,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { } } + @Test public void testUnMappableCharacter() throws java.io.IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteWriter writer = new ByteWriter(stream, Charset.forName("ascii").newEncoder()); @@ -280,6 +288,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertTrue(stream.toString("ascii").contains("bahoo")); } + @Test public void testNoRecycling() throws IOException { final int SINGLE_BUFFER = 500; final int APPENDS = 500; @@ -307,6 +316,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { } } + @Test public void testGetEncoding() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -314,6 +324,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { b.close(); } + @Test public void testWriteLong() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -322,6 +333,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("1000000000000"), stream.toByteArray()); } + @Test public void testWriteInt() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -330,6 +342,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("z"), stream.toByteArray()); } + @Test public void testSurrogatePairs() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -339,6 +352,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("\uD800\uDFD0"), stream.toByteArray()); } + @Test public void testSurrogatePairsMixedWithSingleCharacters() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -354,7 +368,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("\u00F8\uD800\uDFD0\u00F8ab"), stream.toByteArray()); } - + @Test public void testWriteDouble() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -363,6 +377,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("12.0"), stream.toByteArray()); } + @Test public void testWriteFloat() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -371,6 +386,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("12.0"), stream.toByteArray()); } + @Test public void testWriteShort() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -379,6 +395,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("12"), stream.toByteArray()); } + @Test public void testWriteBoolean() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -387,6 +404,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(Utf8.toBytes("true"), stream.toByteArray()); } + @Test public void testAppendSingleByte() throws java.io.IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -395,6 +413,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertArrayEquals(new byte[] { (byte) 'a' }, stream.toByteArray()); } + @Test public void testAppended() throws IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -406,6 +425,7 @@ public class ByteWriterTestCase extends junit.framework.TestCase { assertEquals(bytes.length, b.appended()); } + @Test public void testWriteUtf8Array() throws IOException { stream.reset(); ByteWriter b = new ByteWriter(stream, encoder); @@ -441,4 +461,5 @@ public class ByteWriterTestCase extends junit.framework.TestCase { datastore.flush(); } } + } diff --git a/vespajlib/src/test/java/com/yahoo/io/FileReadTestCase.java b/vespajlib/src/test/java/com/yahoo/io/FileReadTestCase.java index f03944ff6f8..1acd225b18a 100644 --- a/vespajlib/src/test/java/com/yahoo/io/FileReadTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/io/FileReadTestCase.java @@ -8,8 +8,10 @@ import java.io.IOException; import java.io.StringReader; import java.nio.charset.Charset; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -public class FileReadTestCase extends junit.framework.TestCase { +public class FileReadTestCase { @Test public void testReadByteArray() throws IOException { diff --git a/vespajlib/src/test/java/com/yahoo/io/GrowableBufferOutputStreamTestCase.java b/vespajlib/src/test/java/com/yahoo/io/GrowableBufferOutputStreamTestCase.java index 70e21cef62a..001023f62a4 100644 --- a/vespajlib/src/test/java/com/yahoo/io/GrowableBufferOutputStreamTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/io/GrowableBufferOutputStreamTestCase.java @@ -5,14 +5,20 @@ import java.nio.channels.WritableByteChannel; import java.nio.ByteBuffer; import java.io.IOException; import com.yahoo.io.GrowableBufferOutputStream; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; /** * Tests the GrowableBufferOutputStream * - * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a> + * @author Bjorn Borud */ -public class GrowableBufferOutputStreamTestCase extends junit.framework.TestCase { +public class GrowableBufferOutputStreamTestCase { + private byte[] testData; static class DummyWritableByteChannel implements WritableByteChannel { @@ -22,7 +28,7 @@ public class GrowableBufferOutputStreamTestCase extends junit.framework.TestCase this.buffer = buffer; } - public int write(ByteBuffer src) throws IOException { + public int write(ByteBuffer src) { int written = Math.min(src.remaining(), buffer.remaining()); if (buffer.remaining() < src.remaining()) { @@ -40,13 +46,10 @@ public class GrowableBufferOutputStreamTestCase extends junit.framework.TestCase return true; } - public void close() throws IOException {} - } - - public GrowableBufferOutputStreamTestCase(String name) { - super(name); + public void close() {} } + @Before public void setUp() { testData = new byte[100]; for (int i = 0; i < 100; ++i) { @@ -54,6 +57,7 @@ public class GrowableBufferOutputStreamTestCase extends junit.framework.TestCase } } + @Test public void testSimple() throws IOException { GrowableBufferOutputStream g = new GrowableBufferOutputStream(10, 5); @@ -123,4 +127,5 @@ public class GrowableBufferOutputStreamTestCase extends junit.framework.TestCase g.clearAll(); assertEquals(0, g.numWritableBuffers()); } + } |