Author: ggregory
Date: Tue Jun 21 22:34:33 2016
New Revision: 1749617

URL: http://svn.apache.org/viewvc?rev=1749617&view=rev
Log:
On Windows, test whatever JRE and JDK are registered in the Windows Registry.

Modified:
    commons/proper/bcel/trunk/pom.xml
    
commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java

Modified: commons/proper/bcel/trunk/pom.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/bcel/trunk/pom.xml?rev=1749617&r1=1749616&r2=1749617&view=diff
==============================================================================
--- commons/proper/bcel/trunk/pom.xml [UTF-8] (original)
+++ commons/proper/bcel/trunk/pom.xml [UTF-8] Tue Jun 21 22:34:33 2016
@@ -414,6 +414,24 @@
       <version>4.12</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>net.java.dev.jna</groupId>
+      <artifactId>jna</artifactId>
+      <version>4.2.2</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>net.java.dev.jna</groupId>
+      <artifactId>jna-platform</artifactId>
+      <version>4.2.2</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.4</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <profiles>

Modified: 
commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java?rev=1749617&r1=1749616&r2=1749617&view=diff
==============================================================================
--- 
commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java
 (original)
+++ 
commons/proper/bcel/trunk/src/test/java/org/apache/bcel/generic/JDKGenericDumpTestCase.java
 Tue Jun 21 22:34:33 2016
@@ -17,15 +17,19 @@
 
 package org.apache.bcel.generic;
 
+import static com.sun.jna.platform.win32.WinReg.HKEY_LOCAL_MACHINE;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.FileFilter;
 import java.io.InputStream;
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
@@ -33,39 +37,76 @@ import org.apache.bcel.classfile.ClassPa
 import org.apache.bcel.classfile.Code;
 import org.apache.bcel.classfile.JavaClass;
 import org.apache.bcel.classfile.Method;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.SystemUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;
 
+import com.sun.jna.platform.win32.Advapi32Util;
+
 /**
- * Test that the generic dump() methods work on the JDK classes
- * Reads each class into an instruction list and then dumps
+ * Test that the generic dump() methods work on the JDK classes Reads each 
class into an instruction list and then dumps
  * the instructions. The output bytes should be the same as the input.
  */
 @RunWith(Parameterized.class)
 public class JDKGenericDumpTestCase {
 
+    private static final String KEY_JDK = "SOFTWARE\\JavaSoft\\Java 
Development Kit";
+
+    private static final String KEY_JRE = "SOFTWARE\\JavaSoft\\Java Runtime 
Environment";
+
     @Parameters(name = "{0}")
-    public static Collection<Object[]> data() {
-        return Arrays.asList(findJavaHomes());
+    public static Collection<String> data() {
+        return findJavaHomes();
     }
 
-    private static Object[][] findJavaHomes() {
-        return new Object[][] { { System.getProperty("java.home") } };
+    private static Set<String> findJavaHomes() {
+        if (SystemUtils.IS_OS_WINDOWS) {
+            return findJavaHomesOnWindows();
+        }
+        final Set<String> javaHomes = new HashSet<>(1);
+        javaHomes.add(System.getProperty("java.home"));
+        return javaHomes;
+    }
+
+    private static Set<String> findJavaHomesOnWindows() {
+        Set<String> javaHomes = new HashSet<>();
+        final String[] jreKeys = 
Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, KEY_JRE);
+        javaHomes = findJavaHomesOnWindows(jreKeys);
+        final String[] jdkKeys = 
Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, KEY_JDK);
+        javaHomes.addAll(findJavaHomesOnWindows(jdkKeys));
+        return javaHomes;
+    }
+
+    private static Set<String> findJavaHomesOnWindows(final String[] keys) {
+        final Set<String> javaHomes = new HashSet<>(keys.length);
+        for (final String key : keys) {
+            final String javaHome = 
Advapi32Util.registryGetStringValue(HKEY_LOCAL_MACHINE, KEY_JRE + "\\" + key,
+                    "JavaHome");
+            if (StringUtils.isNoneBlank(javaHome)) {
+                if (new File(javaHome).exists()) {
+                    javaHomes.add(javaHome);
+                }
+            }
+        }
+        return javaHomes;
     }
 
     public JDKGenericDumpTestCase(final String javaHome) {
         this.javaHome = javaHome;
     }
 
-    private String javaHome;
+    private final String javaHome;
 
     @Test
     public void testJDKjars() throws Exception {
         final File[] jars = listJDKjars();
-        for (final File file : jars) {
-            testJar(file);
+        if (jars != null) {
+            for (final File file : jars) {
+                testJar(file);
+            }
         }
     }
 


Reply via email to