@Test public void testEquals() throws IOException { try { proxy.equals(proxy); } catch (AvroRuntimeException e) { fail(e.getMessage()); } }
@Test public void testToString() throws IOException { try { proxy.toString(); } catch (AvroRuntimeException e) { fail(e.getMessage()); } }
@Test public void testHashCode() throws IOException { try { proxy.hashCode(); } catch (AvroRuntimeException e) { fail(e.getMessage()); } }
@Test public void testCollectionFailure() throws Exception { try { new Pair("foo", new ArrayList()); } catch (AvroRuntimeException e) { assertTrue(e.getMessage().startsWith("Cannot infer schema")); return; } fail("Expected an AvroRuntimeException"); }
/** Test that the error message contains the name of the class. */ @Test public void testReflectFieldError() throws Exception { Object datum = ""; try { ReflectData.get().getField(datum, "notAFieldOfString", 0); } catch (AvroRuntimeException e) { assertTrue(e.getMessage().contains(datum.getClass().getName())); } }
@Test public void testBadCodec() throws IOException { LOG.debug("Using a bad codec for a SortedKeyValueFile..."); try { SortedKeyValueFile.Writer.Options options = new SortedKeyValueFile.Writer.Options().withCodec("foobar"); } catch (AvroRuntimeException e) { assertEquals("Unrecognized codec: foobar", e.getMessage()); } }
@Test() public void buildWithoutSettingRequiredFields2() { try { new GenericRecordBuilder(recordSchema()). set("anArray", Arrays.asList(new String[] { "one" })). build(); Assert.fail("Should have thrown " + AvroRuntimeException.class.getCanonicalName()); } catch (AvroRuntimeException e) { Assert.assertTrue(e.getMessage().contains("intField")); } }
@Test public void testNegativeLengthEncoding() throws IOException { byte[] bad = new byte[] { (byte)1 }; Decoder bd = factory.binaryDecoder(bad, null); String message = ""; try { bd.readString(); } catch (AvroRuntimeException e) { message = e.getMessage(); } Assert.assertEquals("Malformed data. Length is negative: -1", message); }
@Test public void buildWithoutSettingRequiredFields2() { // Omit required non-primitive field try { Person.newBuilder().setYearOfBirth(1900).setState("MA").build(); Assert.fail("Should have thrown " + AvroRuntimeException.class.getCanonicalName()); } catch (AvroRuntimeException e) { // Exception should mention that the 'name' field has not been set Assert.assertTrue(e.getMessage().contains("name")); } }
@Test public void buildWithoutSettingRequiredFields3() { // Omit required primitive field try { Person.newBuilder().setName("Anon").setState("CA").build(); Assert.fail("Should have thrown " + AvroRuntimeException.class.getCanonicalName()); } catch (AvroRuntimeException e) { // Exception should mention that the 'year_of_birth' field has not been set Assert.assertTrue(e.getMessage().contains("year_of_birth")); } }
@Test public void testDefaultRecordWithDuplicateFieldName() { String recordName = "name"; Schema schema = Schema.createRecord(recordName, "doc", "namespace", false); List<Field> fields = new ArrayList<>(); fields.add(new Field("field_name", Schema.create(Type.NULL), null, null)); fields.add(new Field("field_name", Schema.create(Type.INT), null, null)); try { schema.setFields(fields); fail("Should not be able to create a record with duplicate field name."); } catch (AvroRuntimeException are) { assertTrue(are.getMessage().contains("Duplicate field field_name in record " + recordName)); } }
/** {@inheritDoc} */ @Override public boolean nextKeyValue() throws IOException, InterruptedException { boolean hasNext = false; try { hasNext = super.nextKeyValue(); } catch (AvroRuntimeException are) { // Survive an "Invalid sync!" exception logger().error("Trapping " + are.getMessage()); } return hasNext; }
byte[] convertToAvro(byte[] bytes, Schema schema) { try { return convertToAvro(readJson(bytes, schema), schema); } catch (IOException e) { throw new AvroConversionException("Failed to convert to AVRO.", e); } catch (AvroRuntimeException e) { throw new AvroConversionException( String.format("Failed to convert to AVRO: %s.", e.getMessage()), e); } }
@Test public void testAvroProhibitsShadowing() { // This test verifies that Avro won't serialize a class with two fields of // the same name. This is important for our error reporting, and also how // we lookup a field. try { ReflectData.get().getSchema(SubclassHidingParent.class); fail("Expected AvroTypeException"); } catch (AvroRuntimeException e) { assertThat(e.getMessage(), containsString("mapField")); assertThat(e.getMessage(), containsString("two fields named")); } }