public ThriftCodecManager(ThriftCodecFactory factory, Set<ThriftCodec<?>> codecs) { this(factory, new ThriftCatalog(), codecs); }
public ThriftCodecManager(ThriftCodecFactory factory, ThriftCodec<?>... codecs) { this(factory, new ThriftCatalog(), ImmutableSet.copyOf(codecs)); }
/** * @see ThriftServiceMetadata#ThriftServiceMetadata(Class, ThriftCatalog) * @param swiftServiceType * @return */ public static ThriftServiceMetadata parse(Class<?> swiftServiceType) { checkNotNull(swiftServiceType, "swiftServiceType is null"); return new ThriftServiceMetadata(swiftServiceType, new ThriftCatalog()); } /**
@Test( dataProvider = "getTestCasesWithInvalids", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "isLegacyId (must|should only) be specified.*" ) public void testInvalids(Method[] mBox) { Method m = mBox[0]; new ThriftMethodMetadata("DummyService", m, new ThriftCatalog()); }
@Test public void testNonFinalStructsOk() { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), NotFinalStruct.class); builder.build(); }
@Test public void testNonFinalUnionOk() { ThriftUnionMetadataBuilder builder = new ThriftUnionMetadataBuilder(new ThriftCatalog(), NotFinalUnion.class); builder.build(); }
@Test public void testGenericBuilder() { Type structType = new TypeToken<GenericStruct<String>>() {}.getType(); ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), structType); builder.build(); }
private void assertExceptions(String methodName, Class<? extends Exception>... expectedExceptions) throws Exception { ThriftMethodMetadata metadata = new ThriftMethodMetadata("DummyService", DummyService.class.getMethod(methodName), new ThriftCatalog()); Map<Short, Type> actualIdMap = new TreeMap<>(); Map<Short, Type> expectedIdMap = new TreeMap<>(); for (Map.Entry<Short, ThriftType> entry : metadata.getExceptions().entrySet()) { actualIdMap.put(entry.getKey(), entry.getValue().getJavaType()); } short expectedId = 1; for (Class<? extends Exception> expectedException : expectedExceptions) { expectedIdMap.put(expectedId, expectedException); ++expectedId; } // string comparison produces more useful failure message (and is safe, given the types) Assert.assertEquals(actualIdMap.toString(), expectedIdMap.toString()); }
@Test(expectedExceptions = { MetadataErrorException.class }) public void testGenericBuilderForNonGenericStruct() { Type structType = new TypeToken<NonGenericStruct>() {}.getType(); ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), structType); builder.build(); }
@Test public void testLegacyIdCorrectlyAnnotated() { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), LegacyIdCorrect.class); ThriftStructMetadata metadata = builder.build(); Set<Integer> seen = new HashSet<>(); for (ThriftFieldMetadata field : metadata.getFields()) { seen.add((int) field.getId()); } assertThat(seen) .as("fields found in LegacyIdCorrect") .isEqualTo(LegacyIdCorrect.IDS); }
@Test public void testLegacyIdOnUnion() { ThriftUnionMetadataBuilder builder = new ThriftUnionMetadataBuilder(new ThriftCatalog(), LegacyIdUnionCorrect.class); ThriftStructMetadata metadata = builder.build(); Set<Integer> seen = new HashSet<>(); for (ThriftFieldMetadata field : metadata.getFields()) { seen.add((int) field.getId()); } assertThat(seen) .as("fields found in LegacyIdUnionCorrect") .isEqualTo(LegacyIdUnionCorrect.IDS); }
@Test public void testMergeableRequiredness() { ThriftStructMetadata metadata = new ThriftStructMetadataBuilder(new ThriftCatalog(), MergeableRequiredness.class).build(); assertThat(metadata.getField(1).getRequiredness()) .as("requiredness of field 'foo'") .isEqualTo(Requiredness.OPTIONAL); }
@Test(dataProvider="getTestCasesWithNonLegacyFieldIds") public void testNonLegacyFieldIds(Method[] mBox) { Method m = mBox[0]; ThriftMethodMetadata metadata = new ThriftMethodMetadata("DummyService", m, new ThriftCatalog()); List<ThriftFieldMetadata> parameters = metadata.getParameters(); assertThat(parameters) .as("parameters") .hasSize(1); assertThat(parameters.get(0).getId()) .as("the parameter's ID") .isGreaterThanOrEqualTo((short) 0); }
@Test public void testArraysManual() throws Exception { ThriftCatalog catalog = new ThriftCatalog(); ThriftType thriftType = catalog.getThriftType(ArrayField.class); ArrayFieldThriftCodec arrayFieldCodec = new ArrayFieldThriftCodec(thriftType); // manual codec does not implement the Map fields ArrayField arrayField = new ArrayField( new boolean[] {true, false, false, true}, new short[] {0, 1, 2, 3}, new int[] {10, 11, 12, 13}, new long[] {20, Long.MAX_VALUE, Long.MIN_VALUE}, new double[] {3.0, Double.MAX_VALUE, Double.MIN_VALUE}, "hello".getBytes(UTF_8)); testRoundTripSerialize(arrayFieldCodec, arrayFieldCodec, arrayField, new TCompactProtocol.Factory()); }
@Test(dataProvider="getTestCasesWithLegacyFieldIds") public void testLegacyFieldIds(Method[] mBox) { Method m = mBox[0]; ThriftMethodMetadata metadata = new ThriftMethodMetadata("DummyService", m, new ThriftCatalog()); List<ThriftFieldMetadata> parameters = metadata.getParameters(); assertThat(parameters) .as("parameters") .hasSize(1); assertThat(parameters.get(0).getId()) .as("the parameter's ID") .isNegative(); }
@Test public void testStructFieldsManual() throws Exception { ThriftCatalog catalog = new ThriftCatalog(); ThriftType bonkFieldType = catalog.getThriftType(BonkField.class); BonkFieldThriftCodec bonkFieldCodec = new BonkFieldThriftCodec(bonkFieldType); BonkField bonkField = new BonkField("message", 42); testRoundTripSerialize(bonkFieldCodec, bonkFieldCodec, bonkField, new TCompactProtocol.Factory()); }
@Test public void testMultipleTypes() throws Exception { ThriftUnionMetadataBuilder builder = new ThriftUnionMetadataBuilder(new ThriftCatalog(), MultipleTypes.class); MetadataErrors metadataErrors = builder.getMetadataErrors(); assertThat(metadataErrors.getErrors()) .as("metadata errors") .hasSize(1); assertThat(metadataErrors.getWarnings()) .as("metadata warnings") .isEmpty(); assertThat(metadataErrors.getErrors().get(0).getMessage()) .as("error message") .containsIgnoringCase("multiple types"); }
@Test public void testUnsupportedType() throws Exception { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), UnsupportedJavaType.class); MetadataErrors metadataErrors = builder.getMetadataErrors(); assertThat(metadataErrors.getErrors()) .as("metadata errors") .hasSize(1); assertThat(metadataErrors.getWarnings()) .as("metadata warnings") .isEmpty(); assertThat(metadataErrors.getErrors().get(0).getMessage()) .as("error message") .containsIgnoringCase("not a supported Java type"); }
@Test public void testMulitpleRequiredness() { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), MultipleRequiredness.class); MetadataErrors metadataErrors = builder.getMetadataErrors(); assertThat(metadataErrors.getErrors()) .as("metadata errors") .hasSize(1); assertThat(metadataErrors.getWarnings()) .as("metadata warnings") .isEmpty(); assertThat(metadataErrors.getErrors().get(0).getMessage()) .as("error message") .containsIgnoringCase("multiple requiredness"); }
@Test public void testMultipleTypes() throws Exception { ThriftStructMetadataBuilder builder = new ThriftStructMetadataBuilder(new ThriftCatalog(), MultipleTypes.class); MetadataErrors metadataErrors = builder.getMetadataErrors(); assertThat(metadataErrors.getErrors()) .as("metadata errors") .hasSize(1); assertThat(metadataErrors.getWarnings()) .as("metadata warnings") .isEmpty(); assertThat(metadataErrors.getErrors().get(0).getMessage()) .as("error message") .containsIgnoringCase("multiple types"); }