diff --git a/.gitignore b/.gitignore index 9ae45758..6a8ca091 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ lib query.ser datanucleus.txt tck.txt +*.claude diff --git a/api/src/main/java/javax/jdo/LegacyJava.java b/api/src/main/java/javax/jdo/LegacyJava.java index a708acbd..bec99cf2 100644 --- a/api/src/main/java/javax/jdo/LegacyJava.java +++ b/api/src/main/java/javax/jdo/LegacyJava.java @@ -109,7 +109,7 @@ public static class SecurityManager { public void checkPermission(JDOPermission permission) { try { - checkPermissionMethod.invoke(null, permission); + checkPermissionMethod.invoke(sm, permission); } catch (IllegalAccessException | InvocationTargetException e) { throw new JDOFatalInternalException(e.getMessage()); } @@ -134,8 +134,8 @@ public void updateSecurityManager(Object sm) { */ private static boolean initIsSecurityDeprecated() { try { - Method getSecurityManager = System.class.getMethod("getSecurityManager"); - return getSecurityManager.isAnnotationPresent(Deprecated.class); + System.class.getMethod("getSecurityManager"); + return false; } catch (NoSuchMethodException e) { return true; } diff --git a/api/src/test/java/javax/jdo/LegacyJavaTest.java b/api/src/test/java/javax/jdo/LegacyJavaTest.java new file mode 100644 index 00000000..0c744bcc --- /dev/null +++ b/api/src/test/java/javax/jdo/LegacyJavaTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.jdo; + +import static org.junit.jupiter.api.Assertions.*; + +import java.security.Permission; +import javax.jdo.spi.JDOImplHelper; +import javax.jdo.spi.JDOPermission; +import org.junit.jupiter.api.Test; + +public class LegacyJavaTest { + + @Test + public void testJDOImplHelper() { + // Try without security manager + assertNotNull(JDOImplHelper.getInstance()); + + if (LegacyJava.isSecurityManagerDeprecated()) { + return; + } + SecurityManager oldSecMgr = System.getSecurityManager(); + try { + System.setSecurityManager(new MySecurityManager(JDOPermission.GET_METADATA)); + } catch (UnsupportedOperationException e) { + // Running 24, SecurityManager is present but disabled by default. + return; + } + + try { + // Try with security manager + assertThrows(JDOFatalInternalException.class, JDOImplHelper::getInstance); + } finally { + System.setSecurityManager(oldSecMgr); + } + } + + public class MySecurityManager extends SecurityManager { + private final Permission invalidPermission; + + public MySecurityManager(JDOPermission invalidPermission) { + this.invalidPermission = invalidPermission; + } + + @Override + public void checkPermission(Permission perm) { + if (perm == invalidPermission) + throw new SecurityException("Permission not given: " + perm.getName()); + } + } +} diff --git a/tck/src/main/java/org/apache/jdo/tck/api/persistencemanagerfactory/CloseWithoutPermissionThrowsSecurityException.java b/tck/src/main/java/org/apache/jdo/tck/api/persistencemanagerfactory/CloseWithoutPermissionThrowsSecurityException.java index d88c1c0d..fb9c3bf4 100644 --- a/tck/src/main/java/org/apache/jdo/tck/api/persistencemanagerfactory/CloseWithoutPermissionThrowsSecurityException.java +++ b/tck/src/main/java/org/apache/jdo/tck/api/persistencemanagerfactory/CloseWithoutPermissionThrowsSecurityException.java @@ -59,8 +59,9 @@ private void closeWithMySecurityManager(PersistenceManagerFactory pmf) { SecurityManager oldSecMgr = System.getSecurityManager(); try { System.setSecurityManager(new MySecurityManager()); - } catch (SecurityException se) { - // running with the TCK SecurityManager; don't run this test + } catch (SecurityException | UnsupportedOperationException se) { + // SecurityException: running with the TCK SecurityManager; don't run this test + // UnsupportedOperationException: running Java 24, SecurityManager is present but disabled return; }