diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-12-20 18:38:08 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-12-21 01:18:09 +0100 |
commit | e8f31cbe437ee969f4dd0490a0409b62b2fd045d (patch) | |
tree | 694203ae0423448b259c924c167ecb5584c36229 /document/src/test | |
parent | 5d6f586ccff9880a40366d51f75db5234795c0fc (diff) |
GC deprecated junit assertThat.
Diffstat (limited to 'document/src/test')
5 files changed, 66 insertions, 67 deletions
diff --git a/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java b/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java index bee5f78ae83..63a0f8d25ed 100644 --- a/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java +++ b/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java @@ -5,9 +5,7 @@ import com.yahoo.document.idstring.IdIdString; import com.yahoo.document.idstring.IdString; import com.yahoo.vespa.objects.BufferSerializer; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.io.BufferedReader; @@ -17,8 +15,10 @@ import java.io.UnsupportedEncodingException; import java.util.regex.Pattern; import java.util.Arrays; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -26,9 +26,6 @@ public class DocumentIdTestCase { DocumentTypeManager manager = new DocumentTypeManager(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @Before public void setUp() { DocumentType testDocType = new DocumentType("testdoc"); @@ -87,16 +84,22 @@ public class DocumentIdTestCase { @Test public void empty_user_location_value_throws_exception() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("ID location value for 'n=' key is empty"); - new DocumentId("id:namespace:type:n=:foo"); + try { + new DocumentId("id:namespace:type:n=:foo"); + fail(); + } catch (IllegalArgumentException e) { + assertEquals("ID location value for 'n=' key is empty", e.getMessage()); + } } @Test public void empty_group_location_value_throws_exception() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("ID location value for 'g=' key is empty"); - new DocumentId("id:namespace:type:g=:foo"); + try { + new DocumentId("id:namespace:type:g=:foo"); + fail(); + } catch (IllegalArgumentException e) { + assertEquals("ID location value for 'g=' key is empty", e.getMessage()); + } } //Compares globalId with C++ implementation located in @@ -198,14 +201,10 @@ public class DocumentIdTestCase { DocumentId docId0Copy = new DocumentId("id:blabla:type::0"); byte[] docId0CopyGid = docId0Copy.getGlobalId(); - - //GIDs should be the same - for (int i = 0; i < docId0Gid.length; i++) { - assertEquals(docId0Gid[i], docId0CopyGid[i]); - } + assertArrayEquals(docId0Gid, docId0CopyGid); //straight hashCode() of byte arrays won't be the same - assertFalse(docId0Gid.hashCode() == docId0CopyGid.hashCode()); + assertNotEquals(docId0Gid.hashCode(), docId0CopyGid.hashCode()); //Arrays.hashCode() works better... assertEquals(Arrays.hashCode(docId0Gid), Arrays.hashCode(docId0CopyGid)); @@ -231,7 +230,7 @@ public class DocumentIdTestCase { } @Test - public void testSerializedDocumentIdCanContainNonTextCharacter() throws UnsupportedEncodingException { + public void testSerializedDocumentIdCanContainNonTextCharacter() { String strId = new String(new byte[]{105, 100, 58, 97, 58, 98, 58, 58, 7, 99}); // "id:a:b::0x7c" DocumentId docId = DocumentId.createFromSerialized(strId); { @@ -247,7 +246,7 @@ public class DocumentIdTestCase { } @Test - public void testSerializedDocumentIdCannotContainZeroByte() throws UnsupportedEncodingException { + public void testSerializedDocumentIdCannotContainZeroByte() { String strId = new String(new byte[]{105, 100, 58, 97, 58, 98, 58, 58, 0, 99}); // "id:a:b::0x0c" try { DocumentId.createFromSerialized(strId); diff --git a/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java b/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java index a537f5f5108..9ff15339ab4 100644 --- a/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java +++ b/document/src/test/java/com/yahoo/document/datatypes/ReferenceFieldValueTestCase.java @@ -6,15 +6,14 @@ import com.yahoo.document.DocumentId; import com.yahoo.document.DocumentType; import com.yahoo.document.Field; import com.yahoo.document.ReferenceDataType; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author vekterli @@ -22,9 +21,6 @@ import static org.junit.Assert.assertTrue; */ public class ReferenceFieldValueTestCase { - @Rule - public final ExpectedException exceptionRule = ExpectedException.none(); - private static DocumentType createDocumentType(String name) { DocumentType type = new DocumentType(name); type.addField(new Field("foo", DataType.STRING)); @@ -126,10 +122,13 @@ public class ReferenceFieldValueTestCase { public void assigning_reference_field_with_different_type_to_existing_reference_throws_exception() { ReferenceFieldValue existing = new ReferenceFieldValue(referenceTypeFoo()); ReferenceFieldValue newValue = new ReferenceFieldValue(referenceTypeBar()); - exceptionRule.expect(IllegalArgumentException.class); - exceptionRule.expectMessage("Can't assign reference of type Reference<bar> " + - "to reference of type Reference<foo>"); - existing.assign(newValue); + try { + existing.assign(newValue); + fail(); + } catch (IllegalArgumentException e) { + assertEquals("Can't assign reference of type Reference<bar> to reference of type Reference<foo>", + e.getMessage()); + } } @Test diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java index a1c1669ffa1..66ff7a7d4cd 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java @@ -56,9 +56,7 @@ import com.yahoo.text.Utf8; import com.yahoo.yolean.Exceptions; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -96,9 +94,6 @@ public class JsonReaderTestCase { private DocumentTypeManager types; private JsonFactory parserFactory; - @Rule - public ExpectedException exception = ExpectedException.none(); - @Before public void setUp() throws Exception { parserFactory = new JsonFactory(); @@ -202,7 +197,6 @@ public class JsonReaderTestCase { public void tearDown() throws Exception { types = null; parserFactory = null; - exception = ExpectedException.none(); } private JsonReader createReader(String jsonInput) { @@ -963,9 +957,12 @@ public class JsonReaderTestCase { DocumentParseInfo parseInfo = r.parseDocument().get(); DocumentType docType = r.readDocumentType(parseInfo.documentId); DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId)); - exception.expect(IllegalArgumentException.class); - exception.expectMessage("No field 'smething' in the structure of type 'smoke'"); - new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put); + try { + new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().startsWith("No field 'smething' in the structure of type 'smoke'")); + } } @Test @@ -975,9 +972,12 @@ public class JsonReaderTestCase { " { 'put': 'id:test:smoke::1', 'fields': { 'something': 'foo' } },", " { 'put': 'id:test:smoke::2', 'fields': { 'something': 'foo' } },", "]")); - exception.expect(RuntimeException.class); - exception.expectMessage("JsonParseException"); - while (r.next() != null); + try { + while (r.next() != null) ; + fail(); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("JsonParseException")); + } } @Test @@ -1869,9 +1869,6 @@ public class JsonReaderTestCase { @Test public void requireThatUnknownDocTypeThrowsIllegalArgumentException() { - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Document type walrus does not exist"); - final String jsonData = inputJson( "[", " {", @@ -1881,8 +1878,12 @@ public class JsonReaderTestCase { " }", " }", "]"); - - new JsonReader(types, jsonToInputStream(jsonData), parserFactory).next(); + try { + new JsonReader(types, jsonToInputStream(jsonData), parserFactory).next(); + fail(); + } catch (IllegalArgumentException e) { + assertEquals("Document type walrus does not exist", e.getMessage()); + } } private static final String TENSOR_DOC_ID = "id:unittest:testtensor::0"; @@ -2051,10 +2052,13 @@ public class JsonReaderTestCase { // NOTE: Do not call this method multiple times from a test method as it's using the ExpectedException rule private void assertParserErrorMatches(String expectedError, String... json) { - exception.expect(JsonReaderException.class); - exception.expectMessage(expectedError); String jsonData = inputJson(json); - new JsonReader(types, jsonToInputStream(jsonData), parserFactory).next(); + try { + new JsonReader(types, jsonToInputStream(jsonData), parserFactory).next(); + fail(); + } catch (JsonReaderException e) { + assertEquals(expectedError, e.getMessage()); + } } private void assertCreatePutFails(String tensor, String name, String msg) { diff --git a/document/src/test/java/com/yahoo/document/select/DocumentSelectorTestCase.java b/document/src/test/java/com/yahoo/document/select/DocumentSelectorTestCase.java index 5a75110ca74..0a23c14cf16 100644 --- a/document/src/test/java/com/yahoo/document/select/DocumentSelectorTestCase.java +++ b/document/src/test/java/com/yahoo/document/select/DocumentSelectorTestCase.java @@ -28,9 +28,7 @@ import com.yahoo.document.select.parser.ParseException; import com.yahoo.document.select.parser.TokenMgrException; import com.yahoo.yolean.Exceptions; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import java.util.ArrayList; import java.util.Arrays; @@ -49,9 +47,6 @@ import static org.junit.Assert.fail; */ public class DocumentSelectorTestCase { - @Rule - public final ExpectedException exceptionRule = ExpectedException.none(); - private static final DocumentTypeManager manager = new DocumentTypeManager(); @Before @@ -786,13 +781,15 @@ public class DocumentSelectorTestCase { @Test public void imported_fields_only_supported_for_simple_expressions() throws ParseException { - exceptionRule.expect(IllegalArgumentException.class); // TODO we should probably handle this case specially and give a better exception message - exceptionRule.expectMessage("Field 'my_imported_field' not found in type datatype test"); - var documents = createDocs(); - // Nested field access is NOT considered a simple expression. - evaluate("test.my_imported_field.foo", documents.get(0)); + try { + // Nested field access is NOT considered a simple expression. + evaluate("test.my_imported_field.foo", documents.get(0)); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().startsWith("Field 'my_imported_field' not found in type datatype test")); + } } @Test diff --git a/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java b/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java index 5b7e9ad89d5..60dd5ad1d0d 100644 --- a/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java +++ b/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java @@ -5,17 +5,13 @@ import com.yahoo.document.datatypes.TensorFieldValue; import com.yahoo.document.update.TensorModifyUpdate.Operation; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorType; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class TensorModifyUpdateTest { - @Rule - public ExpectedException exception = ExpectedException.none(); - @Test public void convert_to_compatible_type_with_only_mapped_dimensions() { assertConvertToCompatible("tensor(x{})", "tensor(x[])"); @@ -31,10 +27,14 @@ public class TensorModifyUpdateTest { @Test public void use_of_incompatible_tensor_type_throws() { - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Tensor type 'tensor(x[3])' is not compatible as it has no mapped dimensions"); - new TensorModifyUpdate(TensorModifyUpdate.Operation.REPLACE, - new TensorFieldValue(Tensor.from("tensor(x[3])", "{{x:1}:3}"))); + try { + new TensorModifyUpdate(TensorModifyUpdate.Operation.REPLACE, + new TensorFieldValue(Tensor.from("tensor(x[3])", "{{x:1}:3}"))); + fail(); + } catch (IllegalArgumentException e) { + assertEquals("Tensor type 'tensor(x[3])' is not compatible as it has no mapped dimensions", + e.getMessage()); + } } @Test |