diff options
Diffstat (limited to 'core/src/test/java/org/elasticsearch/common')
13 files changed, 164 insertions, 170 deletions
diff --git a/core/src/test/java/org/elasticsearch/common/geo/builders/AbstractShapeBuilderTestCase.java b/core/src/test/java/org/elasticsearch/common/geo/builders/AbstractShapeBuilderTestCase.java index 9311db44da..63f6ecd0e6 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/builders/AbstractShapeBuilderTestCase.java +++ b/core/src/test/java/org/elasticsearch/common/geo/builders/AbstractShapeBuilderTestCase.java @@ -50,15 +50,7 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte public static void init() { if (namedWriteableRegistry == null) { namedWriteableRegistry = new NamedWriteableRegistry(); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, PointBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, CircleBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, EnvelopeBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiPointBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, LineStringBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiLineStringBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, PolygonBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiPolygonBuilder.PROTOTYPE); - namedWriteableRegistry.registerPrototype(ShapeBuilder.class, GeometryCollectionBuilder.PROTOTYPE); + ShapeBuilders.register(namedWriteableRegistry); } } @@ -146,8 +138,7 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte try (BytesStreamOutput output = new BytesStreamOutput()) { original.writeTo(output); try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { - ShapeBuilder prototype = (ShapeBuilder) namedWriteableRegistry.getPrototype(ShapeBuilder.class, original.getWriteableName()); - return prototype.readFrom(in); + return namedWriteableRegistry.getReader(ShapeBuilder.class, original.getWriteableName()).read(in); } } } diff --git a/core/src/test/java/org/elasticsearch/common/geo/builders/EnvelopeBuilderTests.java b/core/src/test/java/org/elasticsearch/common/geo/builders/EnvelopeBuilderTests.java index 881db868ef..c2730f91df 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/builders/EnvelopeBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/builders/EnvelopeBuilderTests.java @@ -19,30 +19,21 @@ package org.elasticsearch.common.geo.builders; -import org.locationtech.spatial4j.shape.Rectangle; import com.vividsolutions.jts.geom.Coordinate; + import org.elasticsearch.test.geo.RandomShapeGenerator; +import org.locationtech.spatial4j.shape.Rectangle; import java.io.IOException; -import static org.hamcrest.Matchers.equalTo; - public class EnvelopeBuilderTests extends AbstractShapeBuilderTestCase<EnvelopeBuilder> { public void testInvalidConstructorArgs() { - try { - new EnvelopeBuilder(null, new Coordinate(1.0, -1.0)); - fail("Exception expected"); - } catch (NullPointerException e) { - assertThat("topLeft of envelope cannot be null", equalTo(e.getMessage())); - } - - try { - new EnvelopeBuilder(new Coordinate(1.0, -1.0), null); - fail("Exception expected"); - } catch (NullPointerException e) { - assertThat("bottomRight of envelope cannot be null", equalTo(e.getMessage())); - } + NullPointerException e; + e = expectThrows(NullPointerException.class, () -> new EnvelopeBuilder(null, new Coordinate(1.0, -1.0))); + assertEquals("topLeft of envelope cannot be null", e.getMessage()); + e = expectThrows(NullPointerException.class, () -> new EnvelopeBuilder(new Coordinate(1.0, -1.0), null)); + assertEquals("bottomRight of envelope cannot be null", e.getMessage()); } @Override @@ -60,16 +51,21 @@ public class EnvelopeBuilderTests extends AbstractShapeBuilderTestCase<EnvelopeB // move one corner to the middle of original switch (randomIntBetween(0, 3)) { case 0: - mutation = new EnvelopeBuilder(new Coordinate(randomDoubleBetween(-180.0, original.bottomRight().x, true), original.topLeft().y), original.bottomRight()); + mutation = new EnvelopeBuilder( + new Coordinate(randomDoubleBetween(-180.0, original.bottomRight().x, true), original.topLeft().y), + original.bottomRight()); break; case 1: - mutation = new EnvelopeBuilder(new Coordinate(original.topLeft().x, randomDoubleBetween(original.bottomRight().y, 90.0, true)), original.bottomRight()); + mutation = new EnvelopeBuilder(new Coordinate(original.topLeft().x, randomDoubleBetween(original.bottomRight().y, 90.0, true)), + original.bottomRight()); break; case 2: - mutation = new EnvelopeBuilder(original.topLeft(), new Coordinate(randomDoubleBetween(original.topLeft().x, 180.0, true), original.bottomRight().y)); + mutation = new EnvelopeBuilder(original.topLeft(), + new Coordinate(randomDoubleBetween(original.topLeft().x, 180.0, true), original.bottomRight().y)); break; case 3: - mutation = new EnvelopeBuilder(original.topLeft(), new Coordinate(original.bottomRight().x, randomDoubleBetween(-90.0, original.topLeft().y, true))); + mutation = new EnvelopeBuilder(original.topLeft(), + new Coordinate(original.bottomRight().x, randomDoubleBetween(-90.0, original.topLeft().y, true))); break; } return mutation; diff --git a/core/src/test/java/org/elasticsearch/common/geo/builders/LineStringBuilderTests.java b/core/src/test/java/org/elasticsearch/common/geo/builders/LineStringBuilderTests.java index f6fcf8449d..e96c35287c 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/builders/LineStringBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/builders/LineStringBuilderTests.java @@ -27,31 +27,15 @@ import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType; import java.io.IOException; import java.util.List; -import static org.hamcrest.Matchers.equalTo; - public class LineStringBuilderTests extends AbstractShapeBuilderTestCase<LineStringBuilder> { public void testInvalidConstructorArgs() { - try { - new LineStringBuilder((List<Coordinate>) null); - fail("Exception expected"); - } catch (IllegalArgumentException e) { - assertThat("cannot create point collection with empty set of points", equalTo(e.getMessage())); - } - - try { - new LineStringBuilder(new CoordinatesBuilder()); - fail("Exception expected"); - } catch (IllegalArgumentException e) { - assertThat("cannot create point collection with empty set of points", equalTo(e.getMessage())); - } - - try { - new LineStringBuilder(new CoordinatesBuilder().coordinate(0.0, 0.0)); - fail("Exception expected"); - } catch (IllegalArgumentException e) { - assertThat("invalid number of points in LineString (found [1] - must be >= 2)", equalTo(e.getMessage())); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new LineStringBuilder((List<Coordinate>) null)); + assertEquals("cannot create point collection with empty set of points", e.getMessage()); + e = expectThrows(IllegalArgumentException.class, () -> new LineStringBuilder(new CoordinatesBuilder())); + assertEquals("cannot create point collection with empty set of points", e.getMessage()); + e = expectThrows(IllegalArgumentException.class, () -> new LineStringBuilder(new CoordinatesBuilder().coordinate(0.0, 0.0))); + assertEquals("invalid number of points in LineString (found [1] - must be >= 2)", e.getMessage()); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/geo/builders/MultiLineStringBuilderTests.java b/core/src/test/java/org/elasticsearch/common/geo/builders/MultiLineStringBuilderTests.java index 3c618fd369..925d177c57 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/builders/MultiLineStringBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/builders/MultiLineStringBuilderTests.java @@ -68,9 +68,6 @@ public class MultiLineStringBuilderTests extends AbstractShapeBuilderTestCase<Mu } static MultiLineStringBuilder createRandomShape() { - if (true) { - return new MultiLineStringBuilder(); - } - return (MultiLineStringBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.MULTILINESTRING); + return new MultiLineStringBuilder(); } } diff --git a/core/src/test/java/org/elasticsearch/common/geo/builders/MultiPointBuilderTests.java b/core/src/test/java/org/elasticsearch/common/geo/builders/MultiPointBuilderTests.java index 006064578e..ec2eb50bd3 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/builders/MultiPointBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/builders/MultiPointBuilderTests.java @@ -20,29 +20,20 @@ package org.elasticsearch.common.geo.builders; import com.vividsolutions.jts.geom.Coordinate; + import org.elasticsearch.test.geo.RandomShapeGenerator; import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType; import java.io.IOException; - -import static org.hamcrest.Matchers.equalTo; +import java.util.List; public class MultiPointBuilderTests extends AbstractShapeBuilderTestCase<MultiPointBuilder> { public void testInvalidBuilderException() { - try { - new MultiPointBuilder(null); - fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException e) { - assertThat("cannot create point collection with empty set of points", equalTo(e.getMessage())); - } - - try { - new MultiPointBuilder(new CoordinatesBuilder().build()); - fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException e) { - assertThat("cannot create point collection with empty set of points", equalTo(e.getMessage())); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new MultiPointBuilder((List<Coordinate>) null)); + assertEquals("cannot create point collection with empty set of points", e.getMessage()); + e = expectThrows(IllegalArgumentException.class, () -> new MultiPointBuilder(new CoordinatesBuilder().build())); + assertEquals("cannot create point collection with empty set of points", e.getMessage()); // one point is minimum new MultiPointBuilder(new CoordinatesBuilder().coordinate(0.0, 0.0).build()); diff --git a/core/src/test/java/org/elasticsearch/common/geo/builders/PolygonBuilderTests.java b/core/src/test/java/org/elasticsearch/common/geo/builders/PolygonBuilderTests.java index 24e0bc8571..9a35690f94 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/builders/PolygonBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/builders/PolygonBuilderTests.java @@ -80,7 +80,8 @@ public class PolygonBuilderTests extends AbstractShapeBuilderTestCase<PolygonBui * This is done so we don't have to expose a setter for orientation in the actual class */ private static PolygonBuilder polyWithOposingOrientation(PolygonBuilder pb) { - PolygonBuilder mutation = new PolygonBuilder(pb.shell(), pb.orientation() == Orientation.LEFT ? Orientation.RIGHT : Orientation.LEFT); + PolygonBuilder mutation = new PolygonBuilder(pb.shell(), + pb.orientation() == Orientation.LEFT ? Orientation.RIGHT : Orientation.LEFT); for (LineStringBuilder hole : pb.holes()) { mutation.hole(hole); } diff --git a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 80bad2e1ec..7a2828c0a1 100644 --- a/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -29,8 +29,10 @@ import java.io.IOException; import java.util.Objects; import static org.hamcrest.Matchers.closeTo; +import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.startsWith; /** * Tests for {@link BytesStreamOutput} paging behaviour. @@ -301,7 +303,7 @@ public class BytesStreamsTests extends ESTestCase { public void testNamedWriteable() throws IOException { BytesStreamOutput out = new BytesStreamOutput(); NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(); - namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); + namedWriteableRegistry.register(BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new); TestNamedWriteable namedWriteableIn = new TestNamedWriteable(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10)); out.writeNamedWriteable(namedWriteableIn); byte[] bytes = out.bytes().toBytes(); @@ -314,32 +316,25 @@ public class BytesStreamsTests extends ESTestCase { public void testNamedWriteableDuplicates() throws IOException { NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(); - namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); - try { - namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); - fail("registerPrototype should have failed"); - } catch(IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("named writeable of type [" + TestNamedWriteable.class.getName() + "] with name [" + TestNamedWriteable.NAME + "] is already registered by type [" - + TestNamedWriteable.class.getName() + "] within category [" + BaseNamedWriteable.class.getName() + "]")); - } + namedWriteableRegistry.register(BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> namedWriteableRegistry.register(BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new)); + assertThat(e.getMessage(), startsWith("named writeable [" + BaseNamedWriteable.class.getName() + "][" + TestNamedWriteable.NAME + + "] is already registered by [")); } public void testNamedWriteableUnknownCategory() throws IOException { BytesStreamOutput out = new BytesStreamOutput(); out.writeNamedWriteable(new TestNamedWriteable("test1", "test2")); StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes().toBytes()), new NamedWriteableRegistry()); - try { - //no named writeable registered with given name, can write but cannot read it back - in.readNamedWriteable(BaseNamedWriteable.class); - fail("read should have failed"); - } catch(IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("unknown named writeable category [" + BaseNamedWriteable.class.getName() + "]")); - } + //no named writeable registered with given name, can write but cannot read it back + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> in.readNamedWriteable(BaseNamedWriteable.class)); + assertThat(e.getMessage(), equalTo("unknown named writeable category [" + BaseNamedWriteable.class.getName() + "]")); } public void testNamedWriteableUnknownNamedWriteable() throws IOException { NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(); - namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); + namedWriteableRegistry.register(BaseNamedWriteable.class, TestNamedWriteable.NAME, TestNamedWriteable::new); BytesStreamOutput out = new BytesStreamOutput(); out.writeNamedWriteable(new NamedWriteable() { @Override @@ -362,7 +357,7 @@ public class BytesStreamsTests extends ESTestCase { in.readNamedWriteable(BaseNamedWriteable.class); fail("read should have failed"); } catch(IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("unknown named writeable with name [unknown] within category [" + BaseNamedWriteable.class.getName() + "]")); + assertThat(e.getMessage(), equalTo("unknown named writeable [" + BaseNamedWriteable.class.getName() + "][unknown]")); } } @@ -379,6 +374,27 @@ public class BytesStreamsTests extends ESTestCase { } } + public void testNamedWriteableReaderReturnsNull() throws IOException { + BytesStreamOutput out = new BytesStreamOutput(); + NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(); + namedWriteableRegistry.register(BaseNamedWriteable.class, TestNamedWriteable.NAME, (StreamInput in) -> null); + TestNamedWriteable namedWriteableIn = new TestNamedWriteable(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10)); + out.writeNamedWriteable(namedWriteableIn); + byte[] bytes = out.bytes().toBytes(); + StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry); + assertEquals(in.available(), bytes.length); + IOException e = expectThrows(IOException.class, () -> in.readNamedWriteable(BaseNamedWriteable.class)); + assertThat(e.getMessage(), endsWith("] returned null which is not allowed and probably means it screwed up the stream.")); + } + + public void testOptionalWriteableReaderReturnsNull() throws IOException { + BytesStreamOutput out = new BytesStreamOutput(); + out.writeOptionalWriteable(new TestNamedWriteable(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10))); + StreamInput in = StreamInput.wrap(out.bytes().toBytes()); + IOException e = expectThrows(IOException.class, () -> in.readOptionalWriteable((StreamInput ignored) -> null)); + assertThat(e.getMessage(), endsWith("] returned null which is not allowed and probably means it screwed up the stream.")); + } + private static abstract class BaseNamedWriteable<T> implements NamedWriteable<T> { } @@ -395,6 +411,11 @@ public class BytesStreamsTests extends ESTestCase { this.field2 = field2; } + public TestNamedWriteable(StreamInput in) throws IOException { + field1 = in.readString(); + field2 = in.readString(); + } + @Override public String getWriteableName() { return NAME; @@ -407,11 +428,6 @@ public class BytesStreamsTests extends ESTestCase { } @Override - public TestNamedWriteable readFrom(StreamInput in) throws IOException { - return new TestNamedWriteable(in.readString(), in.readString()); - } - - @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; diff --git a/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java b/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java index bd794f96da..4d8f3e6e58 100644 --- a/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java +++ b/core/src/test/java/org/elasticsearch/common/network/NetworkModuleTests.java @@ -191,23 +191,24 @@ public class NetworkModuleTests extends ModuleTestCase { Settings settings = Settings.EMPTY; NetworkModule module = new NetworkModule(new NetworkService(settings), settings, false, registry); - // Builtin prototype comes back - assertNotNull(registry.getPrototype(Task.Status.class, ReplicationTask.Status.PROTOTYPE.getWriteableName())); + // Builtin reader comes back + assertNotNull(registry.getReader(Task.Status.class, ReplicationTask.Status.NAME)); - Task.Status dummy = new DummyTaskStatus(); - module.registerTaskStatus(dummy); - assertThat(registry.getPrototype(Task.Status.class, "dummy"), sameInstance(dummy)); + module.registerTaskStatus(DummyTaskStatus.NAME, DummyTaskStatus::new); + assertEquals("test", expectThrows(UnsupportedOperationException.class, + () -> registry.getReader(Task.Status.class, DummyTaskStatus.NAME).read(null)).getMessage()); } private class DummyTaskStatus implements Task.Status { - @Override - public String getWriteableName() { - return "dummy"; + public static final String NAME = "dummy"; + + public DummyTaskStatus(StreamInput in) { + throw new UnsupportedOperationException("test"); } @Override - public Status readFrom(StreamInput in) throws IOException { - throw new UnsupportedOperationException(); + public String getWriteableName() { + return NAME; } @Override diff --git a/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java b/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java index e3f2bc1bb2..eb6cc56816 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java @@ -201,8 +201,8 @@ public class SettingsTests extends ESTestCase { assertThat(settings.getAsArray("value"), arrayContaining("2", "3")); settings = settingsBuilder() - .put(new YamlSettingsLoader().load("value: 1")) - .put(new YamlSettingsLoader().load("value: [ 2, 3 ]")) + .put(new YamlSettingsLoader(false).load("value: 1")) + .put(new YamlSettingsLoader(false).load("value: [ 2, 3 ]")) .build(); assertThat(settings.getAsArray("value"), arrayContaining("2", "3")); diff --git a/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java b/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java index d7f10891f2..154ef8ee03 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/loader/JsonSettingsLoaderTests.java @@ -25,15 +25,14 @@ import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.test.ESTestCase; import static org.elasticsearch.common.settings.Settings.settingsBuilder; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.equalTo; -/** - * - */ public class JsonSettingsLoaderTests extends ESTestCase { + public void testSimpleJsonSettings() throws Exception { - String json = "/org/elasticsearch/common/settings/loader/test-settings.json"; - Settings settings = settingsBuilder() + final String json = "/org/elasticsearch/common/settings/loader/test-settings.json"; + final Settings settings = settingsBuilder() .loadFromStream(json, getClass().getResourceAsStream(json)) .build(); @@ -50,15 +49,23 @@ public class JsonSettingsLoaderTests extends ESTestCase { } public void testDuplicateKeysThrowsException() { - String json = "{\"foo\":\"bar\",\"foo\":\"baz\"}"; - try { - settingsBuilder() - .loadFromSource(json) - .build(); - fail("expected exception"); - } catch (SettingsException e) { - assertEquals(e.getCause().getClass(), ElasticsearchParseException.class); - assertTrue(e.toString().contains("duplicate settings key [foo] found at line number [1], column number [20], previous value [bar], current value [baz]")); - } + final String json = "{\"foo\":\"bar\",\"foo\":\"baz\"}"; + final SettingsException e = expectThrows(SettingsException.class, () -> settingsBuilder().loadFromSource(json).build()); + assertEquals(e.getCause().getClass(), ElasticsearchParseException.class); + assertThat( + e.toString(), + containsString("duplicate settings key [foo] " + + "found at line number [1], " + + "column number [20], " + + "previous value [bar], " + + "current value [baz]")); } + + public void testNullValuedSettingThrowsException() { + final String json = "{\"foo\":null}"; + final ElasticsearchParseException e = + expectThrows(ElasticsearchParseException.class, () -> new JsonSettingsLoader(false).load(json)); + assertThat(e.toString(), containsString("null-valued setting found for key [foo] found at line number [1], column number [8]")); + } + } diff --git a/core/src/test/java/org/elasticsearch/common/settings/loader/PropertiesSettingsLoaderTests.java b/core/src/test/java/org/elasticsearch/common/settings/loader/PropertiesSettingsLoaderTests.java index 7a1897fbaf..c13ae7cc68 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/loader/PropertiesSettingsLoaderTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/loader/PropertiesSettingsLoaderTests.java @@ -21,27 +21,37 @@ package org.elasticsearch.common.settings.loader; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.test.ESTestCase; +import org.junit.Before; import java.io.IOException; import java.nio.charset.Charset; public class PropertiesSettingsLoaderTests extends ESTestCase { + + private PropertiesSettingsLoader loader; + + @Before + public void setUp() throws Exception { + super.setUp(); + loader = new PropertiesSettingsLoader(); + } + public void testDuplicateKeyFromStringThrowsException() throws IOException { - PropertiesSettingsLoader loader = new PropertiesSettingsLoader(); - try { - loader.load("foo=bar\nfoo=baz"); - fail("expected exception"); - } catch (ElasticsearchParseException e) { - assertEquals(e.getMessage(), "duplicate settings key [foo] found, previous value [bar], current value [baz]"); - } + final ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> loader.load("foo=bar\nfoo=baz")); + assertEquals(e.getMessage(), "duplicate settings key [foo] found, previous value [bar], current value [baz]"); } public void testDuplicateKeysFromBytesThrowsException() throws IOException { - PropertiesSettingsLoader loader = new PropertiesSettingsLoader(); - try { - loader.load("foo=bar\nfoo=baz".getBytes(Charset.defaultCharset())); - } catch (ElasticsearchParseException e) { - assertEquals(e.getMessage(), "duplicate settings key [foo] found, previous value [bar], current value [baz]"); - } + final ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> loader.load("foo=bar\nfoo=baz".getBytes(Charset.defaultCharset())) + ); + assertEquals(e.getMessage(), "duplicate settings key [foo] found, previous value [bar], current value [baz]"); } + + public void testThatNoDuplicatesPropertiesDoesNotAcceptNullValues() { + final PropertiesSettingsLoader.NoDuplicatesProperties properties = loader.new NoDuplicatesProperties(); + expectThrows(NullPointerException.class, () -> properties.put("key", null)); + } + } diff --git a/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java b/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java index 48703044ec..2e2a187da0 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/loader/YamlSettingsLoaderTests.java @@ -28,13 +28,11 @@ import static org.elasticsearch.common.settings.Settings.settingsBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; -/** - * - */ public class YamlSettingsLoaderTests extends ESTestCase { + public void testSimpleYamlSettings() throws Exception { - String yaml = "/org/elasticsearch/common/settings/loader/test-settings.yml"; - Settings settings = settingsBuilder() + final String yaml = "/org/elasticsearch/common/settings/loader/test-settings.yml"; + final Settings settings = settingsBuilder() .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) .build(); @@ -51,39 +49,41 @@ public class YamlSettingsLoaderTests extends ESTestCase { } public void testIndentation() { - String yaml = "/org/elasticsearch/common/settings/loader/indentation-settings.yml"; - try { - settingsBuilder() - .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) - .build(); - fail("Expected SettingsException"); - } catch(SettingsException e ) { - assertThat(e.getMessage(), containsString("Failed to load settings")); - } + final String yaml = "/org/elasticsearch/common/settings/loader/indentation-settings.yml"; + final SettingsException e = + expectThrows( + SettingsException.class, + () -> settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)).build()); + assertThat(e.getMessage(), containsString("Failed to load settings")); } public void testIndentationWithExplicitDocumentStart() { - String yaml = "/org/elasticsearch/common/settings/loader/indentation-with-explicit-document-start-settings.yml"; - try { - settingsBuilder() - .loadFromStream(yaml, getClass().getResourceAsStream(yaml)) - .build(); - fail("Expected SettingsException"); - } catch (SettingsException e) { - assertThat(e.getMessage(), containsString("Failed to load settings")); - } + final String yaml = "/org/elasticsearch/common/settings/loader/indentation-with-explicit-document-start-settings.yml"; + final SettingsException e = + expectThrows( + SettingsException.class, + () -> settingsBuilder().loadFromStream(yaml, getClass().getResourceAsStream(yaml)).build()); + assertThat(e.getMessage(), containsString("Failed to load settings")); } public void testDuplicateKeysThrowsException() { - String yaml = "foo: bar\nfoo: baz"; - try { - settingsBuilder() - .loadFromSource(yaml) - .build(); - fail("expected exception"); - } catch (SettingsException e) { - assertEquals(e.getCause().getClass(), ElasticsearchParseException.class); - assertTrue(e.toString().contains("duplicate settings key [foo] found at line number [2], column number [6], previous value [bar], current value [baz]")); - } + final String yaml = "foo: bar\nfoo: baz"; + final SettingsException e = expectThrows(SettingsException.class, () -> settingsBuilder().loadFromSource(yaml).build()); + assertEquals(e.getCause().getClass(), ElasticsearchParseException.class); + assertThat( + e.toString(), + containsString("duplicate settings key [foo] " + + "found at line number [2], " + + "column number [6], " + + "previous value [bar], " + + "current value [baz]")); + } + + public void testNullValuedSettingThrowsException() { + final String yaml = "foo:"; + final ElasticsearchParseException e = + expectThrows(ElasticsearchParseException.class, () -> new YamlSettingsLoader(false).load(yaml)); + assertThat(e.toString(), containsString("null-valued setting found for key [foo] found at line number [1], column number [5]")); } + } diff --git a/core/src/test/java/org/elasticsearch/common/unit/DistanceUnitTests.java b/core/src/test/java/org/elasticsearch/common/unit/DistanceUnitTests.java index 5d7bbb3ca1..f9a4d3f22a 100644 --- a/core/src/test/java/org/elasticsearch/common/unit/DistanceUnitTests.java +++ b/core/src/test/java/org/elasticsearch/common/unit/DistanceUnitTests.java @@ -83,7 +83,7 @@ public class DistanceUnitTests extends ESTestCase { try (BytesStreamOutput out = new BytesStreamOutput()) { unit.writeTo(out); try (StreamInput in = StreamInput.wrap(out.bytes())) { - assertThat("Roundtrip serialisation failed.", DistanceUnit.readDistanceUnit(in), equalTo(unit)); + assertThat("Roundtrip serialisation failed.", DistanceUnit.readFromStream(in), equalTo(unit)); } } } |