Java Code Examples for org.apache.flink.testutils.ClassLoaderUtils#compileAndLoadJava()

The following examples show how to use org.apache.flink.testutils.ClassLoaderUtils#compileAndLoadJava() . 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: PackagedProgramUtilsPipelineTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private List<URL> getClassUrls(String className) throws IOException {
	URLClassLoader urlClassLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(),
		className + ".java",
		"import com.esotericsoftware.kryo.Kryo;\n" +
			"import com.esotericsoftware.kryo.Serializer;\n" +
			"import com.esotericsoftware.kryo.io.Input;\n" +
			"import com.esotericsoftware.kryo.io.Output;\n"
			+ "public class " + className + " extends Serializer {\n" +
			"\t@Override\n" +
			"\tpublic void write(\n" +
			"\t\tKryo kryo,\n" +
			"\t\tOutput output,\n" +
			"\t\tObject object) {\n" +
			"\t}\n" +
			"\n" +
			"\t@Override\n" +
			"\tpublic Object read(Kryo kryo, Input input, Class type) {\n" +
			"\t\treturn null;\n" +
			"\t}\n" +
			"}");
	return Arrays.asList(urlClassLoader.getURLs());
}
 
Example 2
Source File: EnumSerializerUpgradeTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static TypeSerializerSchemaCompatibility checkCompatibility(String enumSourceA, String enumSourceB)
	throws IOException, ClassNotFoundException {

	ClassLoader classLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceA);

	EnumSerializer enumSerializer = new EnumSerializer(classLoader.loadClass(ENUM_NAME));

	TypeSerializerSnapshot snapshot = enumSerializer.snapshotConfiguration();
	byte[] snapshotBytes;
	try (
		ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
		DataOutputViewStreamWrapper outputViewStreamWrapper = new DataOutputViewStreamWrapper(outBuffer)) {

		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			outputViewStreamWrapper, snapshot, enumSerializer);
		snapshotBytes = outBuffer.toByteArray();
	}

	ClassLoader classLoader2 = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceB);

	TypeSerializerSnapshot restoredSnapshot;
	try (
		ByteArrayInputStream inBuffer = new ByteArrayInputStream(snapshotBytes);
		DataInputViewStreamWrapper inputViewStreamWrapper = new DataInputViewStreamWrapper(inBuffer)) {

		restoredSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			inputViewStreamWrapper, classLoader2, enumSerializer);
	}

	EnumSerializer enumSerializer2 = new EnumSerializer(classLoader2.loadClass(ENUM_NAME));
	return restoredSnapshot.resolveSchemaCompatibility(enumSerializer2);
}
 
Example 3
Source File: ClassLoaderTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageDecodingWithUnavailableClass() throws Exception {
	final ClassLoader systemClassLoader = getClass().getClassLoader();

	final String className = "UserClass";
	final URLClassLoader userClassLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(),
		className + ".java",
		"import java.io.Serializable;\n"
			+ "public class " + className + " implements Serializable {}");

	RemoteRpcInvocation method = new RemoteRpcInvocation(
		"test",
		new Class<?>[] {
			int.class,
			Class.forName(className, false, userClassLoader)},
		new Object[] {
			1,
			Class.forName(className, false, userClassLoader).newInstance()});

	SerializedValue<RemoteRpcInvocation> serializedMethod = new SerializedValue<>(method);

	expectedException.expect(ClassNotFoundException.class);
	expectedException.expect(
		allOf(
			isA(ClassNotFoundException.class),
			hasProperty("suppressed",
				hasItemInArray(
					allOf(
						isA(ClassNotFoundException.class),
						hasProperty("message",
							containsString("Could not deserialize 1th parameter type of method test(int, ...).")))))));

	RemoteRpcInvocation deserializedMethod = serializedMethod.deserializeValue(systemClassLoader);
	deserializedMethod.getMethodName();

	userClassLoader.close();
}
 
Example 4
Source File: EnumSerializerUpgradeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static TypeSerializerSchemaCompatibility checkCompatibility(String enumSourceA, String enumSourceB)
	throws IOException, ClassNotFoundException {

	ClassLoader classLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceA);

	EnumSerializer enumSerializer = new EnumSerializer(classLoader.loadClass(ENUM_NAME));

	TypeSerializerSnapshot snapshot = enumSerializer.snapshotConfiguration();
	byte[] snapshotBytes;
	try (
		ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
		DataOutputViewStreamWrapper outputViewStreamWrapper = new DataOutputViewStreamWrapper(outBuffer)) {

		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			outputViewStreamWrapper, snapshot, enumSerializer);
		snapshotBytes = outBuffer.toByteArray();
	}

	ClassLoader classLoader2 = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceB);

	TypeSerializerSnapshot restoredSnapshot;
	try (
		ByteArrayInputStream inBuffer = new ByteArrayInputStream(snapshotBytes);
		DataInputViewStreamWrapper inputViewStreamWrapper = new DataInputViewStreamWrapper(inBuffer)) {

		restoredSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			inputViewStreamWrapper, classLoader2, enumSerializer);
	}

	EnumSerializer enumSerializer2 = new EnumSerializer(classLoader2.loadClass(ENUM_NAME));
	return restoredSnapshot.resolveSchemaCompatibility(enumSerializer2);
}
 
Example 5
Source File: ClassLoaderTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageDecodingWithUnavailableClass() throws Exception {
	final ClassLoader systemClassLoader = getClass().getClassLoader();

	final String className = "UserClass";
	final URLClassLoader userClassLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(),
		className + ".java",
		"import java.io.Serializable;\n"
			+ "public class " + className + " implements Serializable {}");

	RemoteRpcInvocation method = new RemoteRpcInvocation(
		"test",
		new Class<?>[] {
			int.class,
			Class.forName(className, false, userClassLoader)},
		new Object[] {
			1,
			Class.forName(className, false, userClassLoader).newInstance()});

	SerializedValue<RemoteRpcInvocation> serializedMethod = new SerializedValue<>(method);

	expectedException.expect(ClassNotFoundException.class);
	expectedException.expect(
		allOf(
			isA(ClassNotFoundException.class),
			hasProperty("suppressed",
				hasItemInArray(
					allOf(
						isA(ClassNotFoundException.class),
						hasProperty("message",
							containsString("Could not deserialize 1th parameter type of method test(int, ...).")))))));

	RemoteRpcInvocation deserializedMethod = serializedMethod.deserializeValue(systemClassLoader);
	deserializedMethod.getMethodName();

	userClassLoader.close();
}
 
Example 6
Source File: EnumSerializerCompatibilityTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static TypeSerializerSchemaCompatibility checkCompatibility(String enumSourceA, String enumSourceB)
	throws IOException, ClassNotFoundException {

	ClassLoader classLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceA);

	EnumSerializer enumSerializer = new EnumSerializer(classLoader.loadClass(ENUM_NAME));

	TypeSerializerSnapshot snapshot = enumSerializer.snapshotConfiguration();
	byte[] snapshotBytes;
	try (
		ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
		DataOutputViewStreamWrapper outputViewStreamWrapper = new DataOutputViewStreamWrapper(outBuffer)) {

		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			outputViewStreamWrapper, snapshot, enumSerializer);
		snapshotBytes = outBuffer.toByteArray();
	}

	ClassLoader classLoader2 = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(), ENUM_NAME + ".java", enumSourceB);

	TypeSerializerSnapshot restoredSnapshot;
	try (
		ByteArrayInputStream inBuffer = new ByteArrayInputStream(snapshotBytes);
		DataInputViewStreamWrapper inputViewStreamWrapper = new DataInputViewStreamWrapper(inBuffer)) {

		restoredSnapshot = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			inputViewStreamWrapper, classLoader2, enumSerializer);
	}

	EnumSerializer enumSerializer2 = new EnumSerializer(classLoader2.loadClass(ENUM_NAME));
	return restoredSnapshot.resolveSchemaCompatibility(enumSerializer2);
}
 
Example 7
Source File: FlinkUserCodeClassLoadersTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageDecodingWithUnavailableClass() throws Exception {
	final ClassLoader systemClassLoader = getClass().getClassLoader();

	final String className = "UserClass";
	final URLClassLoader userClassLoader = ClassLoaderUtils.compileAndLoadJava(
		temporaryFolder.newFolder(),
		className + ".java",
		"import java.io.Serializable;\n"
			+ "public class " + className + " implements Serializable {}");

	RemoteRpcInvocation method = new RemoteRpcInvocation(
		"test",
		new Class<?>[] {
			int.class,
			Class.forName(className, false, userClassLoader)},
		new Object[] {
			1,
			Class.forName(className, false, userClassLoader).newInstance()});

	SerializedValue<RemoteRpcInvocation> serializedMethod = new SerializedValue<>(method);

	expectedException.expect(ClassNotFoundException.class);
	expectedException.expect(
		allOf(
			isA(ClassNotFoundException.class),
			hasProperty("suppressed",
				hasItemInArray(
					allOf(
						isA(ClassNotFoundException.class),
						hasProperty("message",
							containsString("Could not deserialize 1th parameter type of method test(int, ...).")))))));

	RemoteRpcInvocation deserializedMethod = serializedMethod.deserializeValue(systemClassLoader);
	deserializedMethod.getMethodName();

	userClassLoader.close();
}