|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tomcat.jni; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +/** |
| 23 | + * Diagnostic tests for build and environment configuration. |
| 24 | + * |
| 25 | + * These tests verify prerequisites and configuration WITHOUT loading the native |
| 26 | + * library, making them useful for diagnosing build issues. Run these first if |
| 27 | + * you're having trouble with tests. |
| 28 | + * |
| 29 | + * Run with: ant test -Dtest.name="**/TestBuildEnvironment.java" |
| 30 | + */ |
| 31 | +public class TestBuildEnvironment { |
| 32 | +
|
| 33 | + @Test |
| 34 | + public void testJavaVersion() { |
| 35 | + String javaVersion = System.getProperty("java.version"); |
| 36 | + Assert.assertNotNull("Java version should be available", javaVersion); |
| 37 | +
|
| 38 | + // Extract major version - handle different version formats |
| 39 | + int majorVersion; |
| 40 | + try { |
| 41 | + String[] parts = javaVersion.split("[.\\-_]"); |
| 42 | + if (parts[0].equals("1")) { |
| 43 | + // Java 8 and earlier: "1.8.0_xxx" |
| 44 | + majorVersion = Integer.parseInt(parts[1]); |
| 45 | + } else { |
| 46 | + // Java 9+: "9.x.x", "11.x.x", "21.x.x" |
| 47 | + // Also handles "21.0.1-ea" or "21-ea" |
| 48 | + majorVersion = Integer.parseInt(parts[0]); |
| 49 | + } |
| 50 | + } catch (NumberFormatException e) { |
| 51 | + Assert.fail("Unable to parse Java version: " + javaVersion + "\n" + |
| 52 | + "This is unexpected. Please report this issue.\n" + |
| 53 | + "java.version property: " + javaVersion); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + Assert.assertTrue("Java version should be 21 or higher, found: " + javaVersion + |
| 58 | + "\nSee TESTING.txt for installation instructions.", |
| 59 | + majorVersion >= 21); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testLibraryPath() { |
| 64 | + String libraryPath = System.getProperty("java.library.path"); |
| 65 | + Assert.assertNotNull("java.library.path should be set", libraryPath); |
| 66 | + |
| 67 | + System.out.println("java.library.path = " + libraryPath); |
| 68 | + |
| 69 | + // Check if native/.libs is in the path (expected for tests) |
| 70 | + boolean hasNativeLibs = libraryPath.contains("native/.libs") || |
| 71 | + libraryPath.contains("native\\.libs"); |
| 72 | + |
| 73 | + Assert.assertTrue("Library path should include native/.libs directory: " + libraryPath, |
| 74 | + hasNativeLibs); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testLibraryFileExists() { |
| 79 | + // Check if the native library file exists in java.library.path |
| 80 | + String libraryPath = System.getProperty("java.library.path"); |
| 81 | + String[] paths = libraryPath.split(java.io.File.pathSeparator); |
| 82 | + |
| 83 | + boolean foundLibrary = false; |
| 84 | + String foundName = null; |
| 85 | + |
| 86 | + String[] possibleNames = { |
| 87 | + System.mapLibraryName("tcnative-2"), |
| 88 | + System.mapLibraryName("libtcnative-2"), |
| 89 | + System.mapLibraryName("tcnative-1"), |
| 90 | + System.mapLibraryName("libtcnative-1") |
| 91 | + }; |
| 92 | + |
| 93 | + System.out.println("Searching for native library..."); |
| 94 | + for (String path : paths) { |
| 95 | + java.io.File dir = new java.io.File(path); |
| 96 | + if (dir.exists() && dir.isDirectory()) { |
| 97 | + for (String libName : possibleNames) { |
| 98 | + java.io.File lib = new java.io.File(dir, libName); |
| 99 | + if (lib.exists()) { |
| 100 | + foundLibrary = true; |
| 101 | + foundName = lib.getAbsolutePath(); |
| 102 | + System.out.println(" Found: " + foundName); |
| 103 | + System.out.println(" Size: " + lib.length() + " bytes"); |
| 104 | + System.out.println(" Readable: " + lib.canRead()); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + if (foundLibrary) break; |
| 110 | + } |
| 111 | + |
| 112 | + if (!foundLibrary) { |
| 113 | + System.err.println(" Not found in any of these paths:"); |
| 114 | + for (String path : paths) { |
| 115 | + System.err.println(" " + path); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + Assert.assertTrue("Native library file should exist in library path.\n" + |
| 120 | + "Searched for: " + java.util.Arrays.toString(possibleNames) + "\n" + |
| 121 | + "Library was not found. Build it with:\n" + |
| 122 | + " ./build-and-test.sh\n" + |
| 123 | + "Or see TESTING.txt for manual build instructions.", |
| 124 | + foundLibrary); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testSystemProperties() { |
| 129 | + // Print useful system properties for debugging |
| 130 | + System.out.println("=== System Properties ==="); |
| 131 | + System.out.println("os.name: " + System.getProperty("os.name")); |
| 132 | + System.out.println("os.arch: " + System.getProperty("os.arch")); |
| 133 | + System.out.println("os.version: " + System.getProperty("os.version")); |
| 134 | + System.out.println("java.version: " + System.getProperty("java.version")); |
| 135 | + System.out.println("java.home: " + System.getProperty("java.home")); |
| 136 | + System.out.println("user.dir: " + System.getProperty("user.dir")); |
| 137 | + System.out.println("========================"); |
| 138 | + |
| 139 | + Assert.assertTrue("This test always passes, it just prints info", true); |
| 140 | + } |
| 141 | +} |
0 commit comments