You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2019/05/08 20:07:55 UTC

[geode] branch develop updated: GEODE-6731: Deprecate TestUtil and delegate to ResourceUtils

This is an automated email from the ASF dual-hosted git repository.

klund pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 7f3f95a  GEODE-6731: Deprecate TestUtil and delegate to ResourceUtils
7f3f95a is described below

commit 7f3f95ae4e6d43318129b85a78ad665bed2ff122
Author: Kirk Lund <kl...@apache.org>
AuthorDate: Tue May 7 16:20:21 2019 -0700

    GEODE-6731: Deprecate TestUtil and delegate to ResourceUtils
---
 .../java/org/apache/geode/util/test/TestUtil.java  | 71 ++++++++++++----------
 1 file changed, 38 insertions(+), 33 deletions(-)

diff --git a/geode-junit/src/main/java/org/apache/geode/util/test/TestUtil.java b/geode-junit/src/main/java/org/apache/geode/util/test/TestUtil.java
index 4192867..9809101 100644
--- a/geode-junit/src/main/java/org/apache/geode/util/test/TestUtil.java
+++ b/geode-junit/src/main/java/org/apache/geode/util/test/TestUtil.java
@@ -14,15 +14,14 @@
  */
 package org.apache.geode.util.test;
 
-import java.io.File;
-import java.io.IOException;
-import java.net.URISyntaxException;
-import java.net.URL;
 import java.nio.file.FileSystemNotFoundException;
-import java.nio.file.Paths;
 
-import org.apache.commons.io.FileUtils;
+import org.apache.geode.test.util.ResourceUtils;
 
+/**
+ * @deprecated Please use {@link ResourceUtils} instead.
+ */
+@Deprecated
 public class TestUtil {
 
   /**
@@ -30,52 +29,58 @@ public class TestUtil {
    * of class.getResource. For a relative path it will look in the same package as the class, for an
    * absolute path it will start from the base package.
    *
+   * <p>
    * Best practice is to put the resource in the same package as your test class and load it with
    * this method.
    *
+   * <p>
+   * Deprecated: Please use {@link ResourceUtils} instead. Examples:
+   *
+   * <pre>
+   * String resourcePath = ResourceUtils.getResource(clazz, name).getPath();
+   *
+   * String tempFilePath = ResourceUtils.createTempFileFromResource(clazz, name).getAbsolutePath();
+   * </pre>
+   *
    * @param clazz the class to look relative too
    * @param name the name of the resource, eg "cache.xml"
+   *
+   * @deprecated Please use {@link ResourceUtils} instead.
    */
+  @Deprecated
   public static String getResourcePath(Class<?> clazz, String name) {
-    URL resource = clazz.getResource(name);
-    return getResourcePath(name, resource);
+    try {
+      return ResourceUtils.getResource(clazz, name).getPath();
+    } catch (FileSystemNotFoundException e) {
+      return ResourceUtils.createTempFileFromResource(clazz, name).getAbsolutePath();
+    }
   }
 
   /**
    * Return the path to a named resource. This finds the resource on the classpath using the rules
    * of ClassLoader.getResource.
    *
+   * <p>
+   * Deprecated: Please use {@link ResourceUtils} instead. Examples:
+   *
+   * <pre>
+   * String resourcePath = ResourceUtils.getResource(classLoader, name).getPath();
+   *
+   * String tempFilePath =
+   *     ResourceUtils.createTempFileFromResource(classLoader, name).getAbsolutePath();
+   * </pre>
+   *
    * @param classLoader the ClassLoader to look up resource in
    * @param name the name of the resource, eg "cache.xml"
+   *
+   * @deprecated Please use {@link ResourceUtils} instead.
    */
+  @Deprecated
   public static String getResourcePath(ClassLoader classLoader, String name) {
-    URL resource = classLoader.getResource(name);
-    return getResourcePath(name, resource);
-  }
-
-  private static String getResourcePath(String name, URL resource) {
-    if (resource == null) {
-      throw new RuntimeException("Could not find resource " + name);
-    }
     try {
-      return Paths.get(resource.toURI()).toAbsolutePath().toString();
+      return ResourceUtils.getResource(classLoader, name).getPath();
     } catch (FileSystemNotFoundException e) {
-      // create a temporary copy when Paths.get() fails (eg: jar:file:/...)
-      return createTemporaryCopy(name, resource);
-    } catch (URISyntaxException e) {
-      throw new RuntimeException("Failed getting path to resource " + name, e);
-    }
-  }
-
-  private static String createTemporaryCopy(String name, URL resource) {
-    try {
-      String filename = name.replaceFirst(".*/", "");
-      File tmpFile = File.createTempFile(filename, null);
-      tmpFile.deleteOnExit();
-      FileUtils.copyURLToFile(resource, tmpFile);
-      return tmpFile.getAbsolutePath();
-    } catch (IOException e1) {
-      throw new RuntimeException("Failed getting path to resource " + name, e1);
+      return ResourceUtils.createTempFileFromResource(classLoader, name).getAbsolutePath();
     }
   }
 }