/** Verifies that the given {@code Coder<T>} can be correctly serialized and deserialized. */ public static <T> void coderSerializable(Coder<T> coder) { SerializableUtils.ensureSerializable(coder); }
private Unbounded(@Nullable String name, UnboundedSource<T, ?> source) { super(name); this.source = SerializableUtils.ensureSerializable(source); }
private Bounded(@Nullable String name, BoundedSource<T> source) { super(name); this.source = SerializableUtils.ensureSerializable(source); }
/** * Serializes a Coder and verifies that it can be correctly deserialized. * * <p>Throws a RuntimeException if serialized Coder cannot be deserialized, or if the deserialized * instance is not equal to the original. * * @return the deserialized Coder */ public static Coder<?> ensureSerializable(Coder<?> coder) { // Make sure that Coders are java serializable as well since // they are regularly captured within DoFn's. Coder<?> copy = (Coder<?>) ensureSerializable((Serializable) coder); checkState( coder.equals(copy), "Coder not equal to original after serialization, indicating that the Coder may not " + "implement serialization correctly. Before: %s, after: %s", coder, copy); return copy; }
private void assertSerializationClassLoader( final ClassLoader loader, final Serializable customLoaderSourceInstance) { final Serializable copy = SerializableUtils.ensureSerializable(customLoaderSourceInstance); assertEquals(loader, copy.getClass().getClassLoader()); assertEquals( copy.getClass().getClassLoader(), customLoaderSourceInstance.getClass().getClassLoader()); }
@Test public void testEncodable() throws Exception { SerializableUtils.ensureSerializable(new MyCustomCoder("key")); } }
@Test public void testAvroCoderIsSerializable() throws Exception { AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class); // Check that the coder is serializable using the regular JSON approach. SerializableUtils.ensureSerializable(coder); }
@Test public void testAnythingSerializable() throws Exception { SerializableUtils.ensureSerializable(anything()); }
@Test public void testCanSerializeItemSpecReference() { DisplayData.ItemSpec<?> spec = DisplayData.item("clazz", DisplayDataTest.class); SerializableUtils.ensureSerializable(new HoldsItemSpecReference(spec)); }
@Test public void testAllOfSerializable() throws Exception { SerializableUtils.ensureSerializable(allOf(anything())); }
@Test public void testContainsInAnyOrderSerializable() throws Exception { assertThat( ImmutableList.of(2, 1, 3), SerializableUtils.ensureSerializable(containsInAnyOrder(1, 2, 3))); }
@Test public void testEnsureSerializableWithUnserializableCoderByJava() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("unable to serialize"); SerializableUtils.ensureSerializable(new UnserializableCoderByJava()); }
@Test public void testTranscode() { String stringValue = "hi bob"; int intValue = 42; SerializableByJava testObject = new SerializableByJava(stringValue, intValue); SerializableByJava testCopy = SerializableUtils.ensureSerializable(testObject); assertEquals(stringValue, testCopy.stringValue); assertEquals(intValue, testCopy.intValue); }
@Test public void testNestedValueProviderSerialize() throws Exception { ValueProvider<NonSerializable> nvp = NestedValueProvider.of(StaticValueProvider.of("foo"), new NonSerializableTranslator()); SerializableUtils.ensureSerializable(nvp); }
@Test public void testKvKeyMatcherSerializable() throws Exception { assertThat(KV.of("hello", 42L), SerializableUtils.ensureSerializable(kvWithKey("hello"))); }
@Test public void testSourceIsSerializableWithUnserializableElements() throws Exception { List<UnserializableRecord> elements = ImmutableList.of( new UnserializableRecord("foo"), new UnserializableRecord("bar"), new UnserializableRecord("baz")); CreateSource<UnserializableRecord> source = CreateSource.fromIterable(elements, new UnserializableRecord.UnserializableRecordCoder()); SerializableUtils.ensureSerializable(source); }
@Test public void testContainsInAnyOrderNotSerializable() throws Exception { assertThat( ImmutableList.of(new NotSerializableClass()), SerializableUtils.ensureSerializable( containsInAnyOrder(new NotSerializableClassCoder(), new NotSerializableClass()))); }