org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader Java Examples

The following examples show how to use org.apache.flink.testutils.ArtificialCNFExceptionThrowingClassLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeSerializerSerializationUtilTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies deserialization failure cases when reading a serializer from bytes, in the
 * case of a {@link InvalidClassException}.
 */
@Test
public void testSerializerSerializationWithInvalidClass() throws Exception {

	TypeSerializer<?> serializer = IntSerializer.INSTANCE;

	byte[] serialized;
	try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
		TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
		serialized = out.toByteArray();
	}

	TypeSerializer<?> deserializedSerializer;

	try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
		deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				Collections.singleton(IntSerializer.class.getName())),
			true);
	}
	Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);
}
 
Example #2
Source File: TypeSerializerSerializationUtilTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies deserialization failure cases when reading a serializer from bytes, in the
 * case of a {@link InvalidClassException}.
 */
@Test
public void testSerializerSerializationWithInvalidClass() throws Exception {

	TypeSerializer<?> serializer = IntSerializer.INSTANCE;

	byte[] serialized;
	try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
		TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
		serialized = out.toByteArray();
	}

	TypeSerializer<?> deserializedSerializer;

	try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
		deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				Collections.singleton(IntSerializer.class.getName())),
			true);
	}
	Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);
}
 
Example #3
Source File: TypeSerializerSerializationUtilTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies deserialization failure cases when reading a serializer from bytes, in the
 * case of a {@link InvalidClassException}.
 */
@Test
public void testSerializerSerializationWithInvalidClass() throws Exception {

	TypeSerializer<?> serializer = IntSerializer.INSTANCE;

	byte[] serialized;
	try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
		TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
		serialized = out.toByteArray();
	}

	TypeSerializer<?> deserializedSerializer;

	try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
		deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				Collections.singleton(IntSerializer.class.getName())),
			true);
	}
	Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);
}
 
Example #4
Source File: TypeSerializerSerializationUtilTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies deserialization failure cases when reading a serializer from bytes, in the
 * case of a {@link ClassNotFoundException}.
 */
@Test
public void testSerializerSerializationWithClassNotFound() throws Exception {

	TypeSerializer<?> serializer = IntSerializer.INSTANCE;

	byte[] serialized;
	try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
		TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
		serialized = out.toByteArray();
	}

	TypeSerializer<?> deserializedSerializer;

	try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
		deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				Collections.singleton(IntSerializer.class.getName())),
			true);
	}
	Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);

	Assert.assertArrayEquals(
			InstantiationUtil.serializeObject(serializer),
			((UnloadableDummyTypeSerializer<?>) deserializedSerializer).getActualBytes());
}
 
Example #5
Source File: TypeSerializerSerializationUtilTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies resilience to serializer deserialization failures when writing and reading
 * serializer and config snapshot pairs.
 */
@Test
public void testSerializerAndConfigPairsSerializationWithSerializerDeserializationFailures() throws Exception {
	TestIntSerializer serializer = new TestIntSerializer();

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = Arrays.asList(
		new Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>(
			serializer, serializer.snapshotConfiguration()));

	byte[] serializedSerializersAndConfigs;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience(
				new DataOutputViewStreamWrapper(out), serializersAndConfigs);
		serializedSerializersAndConfigs = out.toByteArray();
	}

	Set<String> cnfThrowingClassnames = new HashSet<>();
	cnfThrowingClassnames.add(TestIntSerializer.class.getName());

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> restored;
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedSerializersAndConfigs)) {
		restored = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				cnfThrowingClassnames));
	}

	Assert.assertEquals(1, restored.size());
	Assert.assertTrue(restored.get(0).f0 instanceof UnloadableDummyTypeSerializer);
	Assert.assertThat(restored.get(0).f1, Matchers.instanceOf(SimpleTypeSerializerSnapshot.class));
}
 
Example #6
Source File: TypeSerializerSerializationUtilTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies deserialization failure cases when reading a serializer from bytes, in the
 * case of a {@link ClassNotFoundException}.
 */
@Test
public void testSerializerSerializationWithClassNotFound() throws Exception {

	TypeSerializer<?> serializer = IntSerializer.INSTANCE;

	byte[] serialized;
	try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
		TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
		serialized = out.toByteArray();
	}

	TypeSerializer<?> deserializedSerializer;

	try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
		deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				Collections.singleton(IntSerializer.class.getName())),
			true);
	}
	Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);

	Assert.assertArrayEquals(
			InstantiationUtil.serializeObject(serializer),
			((UnloadableDummyTypeSerializer<?>) deserializedSerializer).getActualBytes());
}
 
Example #7
Source File: TypeSerializerSerializationUtilTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies resilience to serializer deserialization failures when writing and reading
 * serializer and config snapshot pairs.
 */
@Test
public void testSerializerAndConfigPairsSerializationWithSerializerDeserializationFailures() throws Exception {
	TestIntSerializer serializer = new TestIntSerializer();

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = Arrays.asList(
		new Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>(
			serializer, serializer.snapshotConfiguration()));

	byte[] serializedSerializersAndConfigs;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience(
				new DataOutputViewStreamWrapper(out), serializersAndConfigs);
		serializedSerializersAndConfigs = out.toByteArray();
	}

	Set<String> cnfThrowingClassnames = new HashSet<>();
	cnfThrowingClassnames.add(TestIntSerializer.class.getName());

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> restored;
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedSerializersAndConfigs)) {
		restored = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				cnfThrowingClassnames));
	}

	Assert.assertEquals(1, restored.size());
	Assert.assertTrue(restored.get(0).f0 instanceof UnloadableDummyTypeSerializer);
	Assert.assertThat(restored.get(0).f1, Matchers.instanceOf(SimpleTypeSerializerSnapshot.class));
}
 
Example #8
Source File: TypeSerializerSerializationUtilTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies deserialization failure cases when reading a serializer from bytes, in the
 * case of a {@link ClassNotFoundException}.
 */
@Test
public void testSerializerSerializationWithClassNotFound() throws Exception {

	TypeSerializer<?> serializer = IntSerializer.INSTANCE;

	byte[] serialized;
	try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
		TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer);
		serialized = out.toByteArray();
	}

	TypeSerializer<?> deserializedSerializer;

	try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
		deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				Collections.singleton(IntSerializer.class.getName())),
			true);
	}
	Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer);

	Assert.assertArrayEquals(
			InstantiationUtil.serializeObject(serializer),
			((UnloadableDummyTypeSerializer<?>) deserializedSerializer).getActualBytes());
}
 
Example #9
Source File: TypeSerializerSerializationUtilTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies resilience to serializer deserialization failures when writing and reading
 * serializer and config snapshot pairs.
 */
@Test
public void testSerializerAndConfigPairsSerializationWithSerializerDeserializationFailures() throws Exception {
	TestIntSerializer serializer = new TestIntSerializer();

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> serializersAndConfigs = Arrays.asList(
		new Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>(
			serializer, serializer.snapshotConfiguration()));

	byte[] serializedSerializersAndConfigs;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSerializationUtil.writeSerializersAndConfigsWithResilience(
				new DataOutputViewStreamWrapper(out), serializersAndConfigs);
		serializedSerializersAndConfigs = out.toByteArray();
	}

	Set<String> cnfThrowingClassnames = new HashSet<>();
	cnfThrowingClassnames.add(TestIntSerializer.class.getName());

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> restored;
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedSerializersAndConfigs)) {
		restored = TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(
			new DataInputViewStreamWrapper(in),
			new ArtificialCNFExceptionThrowingClassLoader(
				Thread.currentThread().getContextClassLoader(),
				cnfThrowingClassnames));
	}

	Assert.assertEquals(1, restored.size());
	Assert.assertTrue(restored.get(0).f0 instanceof UnloadableDummyTypeSerializer);
	Assert.assertThat(restored.get(0).f1, Matchers.instanceOf(SimpleTypeSerializerSnapshot.class));
}