huaxingao commented on code in PR #18800:
URL: https://github.com/apache/iceberg/pull/18800#discussion_r4475126011


##########
core/src/test/java/org/apache/iceberg/TestInternalData.java:
##########
@@ -174,6 +180,57 @@ public void 
registerToleratesExceptionInInitializerErrorOnInvoke() {
     assertThatNoException().isThrownBy(() -> 
register(ThrowsInitErrorOnInvoke.class.getName()));
   }
 
+  @Test
+  void registersParquetWhenTheContextClassLoaderCannotSeeIceberg() throws 
Exception {
+    // formats are registered once per loaded copy of InternalData, so this 
needs a fresh copy
+    try (URLClassLoader isolated =
+        new URLClassLoader(testClasspath(), 
ClassLoader.getPlatformClassLoader())) {
+      AtomicReference<Object> writeBuilder = new AtomicReference<>();
+      AtomicReference<Throwable> failure = new AtomicReference<>();
+      Thread thread =
+          new Thread(
+              () -> {
+                try {
+                  writeBuilder.set(writeParquet(isolated));
+                } catch (InvocationTargetException e) {
+                  failure.set(e.getCause());
+                } catch (Throwable e) {
+                  failure.set(e);
+                }
+              });
+      thread.setContextClassLoader(null);
+      thread.start();
+      thread.join();
+
+      assertThat(failure.get()).isNull();
+      assertThat(writeBuilder.get()).isNotNull();
+    }
+  }
+
+  private Object writeParquet(ClassLoader loader) throws 
ReflectiveOperationException {
+    Class<?> internalData = Class.forName("org.apache.iceberg.InternalData", 
true, loader);
+    assertThat(internalData).isNotSameAs(InternalData.class);
+
+    Class<?> fileFormat = Class.forName("org.apache.iceberg.FileFormat", true, 
loader);
+    Class<?> outputFile = Class.forName("org.apache.iceberg.io.OutputFile", 
true, loader);
+    Object parquet = fileFormat.getField("PARQUET").get(null);
+    Object file =
+        Class.forName("org.apache.iceberg.Files", true, loader)
+            .getMethod("localOutput", File.class)
+            .invoke(null, tempDir.resolve("internal.parquet").toFile());
+
+    return internalData.getMethod("write", fileFormat, 
outputFile).invoke(null, parquet, file);
+  }
+
+  private static URL[] testClasspath() throws MalformedURLException {

Review Comment:
   Done. Thanks



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to