|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Contributors to the Eclipse Foundation. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.ejb.deployment.node; |
| 18 | + |
| 19 | +import com.sun.enterprise.deployment.RunAsIdentityDescriptor; |
| 20 | + |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 24 | + |
| 25 | +import org.glassfish.ejb.deployment.EjbTagNames; |
| 26 | +import org.glassfish.ejb.deployment.descriptor.DummyEjbDescriptor; |
| 27 | +import org.glassfish.ejb.deployment.descriptor.EjbDescriptor; |
| 28 | +import org.glassfish.hk2.api.ServiceLocator; |
| 29 | +import org.glassfish.hk2.api.ServiceLocatorFactory; |
| 30 | +import org.glassfish.internal.api.Globals; |
| 31 | +import org.junit.jupiter.api.AfterAll; |
| 32 | +import org.junit.jupiter.api.Assertions; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.params.ParameterizedTest; |
| 35 | +import org.junit.jupiter.params.provider.Arguments; |
| 36 | +import org.junit.jupiter.params.provider.MethodSource; |
| 37 | +import org.w3c.dom.Element; |
| 38 | + |
| 39 | + |
| 40 | +public class EjbNodeTest { |
| 41 | + |
| 42 | + private static final String TEST_LOCATOR_NAME = EjbNodeTest.class.getName(); |
| 43 | + private static ServiceLocator previousHabitat; |
| 44 | + |
| 45 | + @BeforeAll |
| 46 | + static void setupServiceLocator() { |
| 47 | + previousHabitat = Globals.getDefaultHabitat(); |
| 48 | + Globals.setDefaultHabitat(ServiceLocatorFactory.getInstance().create(TEST_LOCATOR_NAME)); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterAll |
| 52 | + static void teardownServiceLocator() { |
| 53 | + Globals.setDefaultHabitat(previousHabitat); |
| 54 | + ServiceLocatorFactory.getInstance().destroy(TEST_LOCATOR_NAME); |
| 55 | + } |
| 56 | + |
| 57 | + /** Tests all meaningful branches of {@link EjbNode#writeSecurityIdentityDescriptor}. */ |
| 58 | + @ParameterizedTest |
| 59 | + @MethodSource("securityIdentityWriteScenarios") |
| 60 | + void writeSecurityIdentityDescriptor(Boolean usesCallerIdentity, boolean withRunAs, int expectedSecurityIdentityCount) throws Exception { |
| 61 | + DummyEjbDescriptor descriptor = new DummyEjbDescriptor(); |
| 62 | + if (withRunAs) { |
| 63 | + // setRunAsIdentity requires false first (EjbBeanDescriptor constraint). |
| 64 | + descriptor.setUsesCallerIdentity(false); |
| 65 | + descriptor.setRunAsIdentity(new RunAsIdentityDescriptor("test-role")); |
| 66 | + } |
| 67 | + if (usesCallerIdentity != null) { |
| 68 | + descriptor.setUsesCallerIdentity(usesCallerIdentity); |
| 69 | + } |
| 70 | + |
| 71 | + Element parent = createParentElement(); |
| 72 | + TestEjbNode ejbNode = new TestEjbNode(descriptor); |
| 73 | + ejbNode.writeSecurityIdentityDescriptor(parent, descriptor); |
| 74 | + |
| 75 | + Assertions.assertEquals(expectedSecurityIdentityCount, parent.getElementsByTagName(EjbTagNames.SECURITY_IDENTITY).getLength()); |
| 76 | + } |
| 77 | + |
| 78 | + private static Stream<Arguments> securityIdentityWriteScenarios() { |
| 79 | + return Stream.of( |
| 80 | + Arguments.of(null, false, 0), |
| 81 | + Arguments.of(null, true, 1), |
| 82 | + Arguments.of(false, false, 0), |
| 83 | + Arguments.of(false, true, 1), |
| 84 | + Arguments.of(true, false, 1), |
| 85 | + Arguments.of(true, true, 1) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + private static Element createParentElement() throws Exception { |
| 90 | + var document = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder().newDocument(); |
| 91 | + Element parent = document.createElement("ejb"); |
| 92 | + document.appendChild(parent); |
| 93 | + return parent; |
| 94 | + } |
| 95 | + |
| 96 | + /** Minimal concrete subclass of {@link EjbNode} for testing protected methods. */ |
| 97 | + private static class TestEjbNode extends EjbNode<EjbDescriptor> { |
| 98 | + |
| 99 | + private final EjbDescriptor descriptor; |
| 100 | + |
| 101 | + TestEjbNode(EjbDescriptor descriptor) { |
| 102 | + this.descriptor = descriptor; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public EjbDescriptor getEjbDescriptor() { |
| 107 | + return descriptor; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments