-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update classloader in the add-library command - unit tests
- Loading branch information
Showing
6 changed files
with
304 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
nucleus/core/kernel/src/test/assembly/assembly-commonClassLoader.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd"> | ||
[...] | ||
<dependencySets> | ||
<dependencySet> | ||
<includes> | ||
<include>*:war</include> | ||
</includes> | ||
</dependencySet> | ||
</dependencySets> | ||
[...] | ||
</assembly> |
191 changes: 191 additions & 0 deletions
191
...e/kernel/src/test/java/com/sun/enterprise/v3/server/CommonClassLoaderServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package com.sun.enterprise.v3.server; | ||
|
||
import com.sun.enterprise.module.bootstrap.StartupContext; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.nio.file.Path; | ||
|
||
import org.glassfish.api.admin.RuntimeType; | ||
import org.glassfish.api.admin.ServerEnvironment; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
public class CommonClassLoaderServiceImplTest { | ||
|
||
private int loadClassCalls; | ||
private int getResourceCalls; | ||
|
||
CommonClassLoaderServiceImpl commonCLService; | ||
MockServerEnvironment serverEnv; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
commonCLService = new CommonClassLoaderServiceImpl(); | ||
commonCLService.acls = new APIClassLoaderServiceImpl() { | ||
@Override | ||
public ClassLoader getAPIClassLoader() { | ||
return new URLClassLoader(new URL[0], null); | ||
} | ||
|
||
}; | ||
serverEnv = new MockServerEnvironment(); | ||
commonCLService.env = serverEnv; | ||
} | ||
|
||
@Test | ||
public void testAddingUrlWithNoInitialUrls() throws MalformedURLException, ClassNotFoundException { | ||
commonCLService.postConstruct(); | ||
|
||
final String classesPath = "target/test-additional-classes/"; | ||
commonCLService.addToClassPath(new File(classesPath).toURI().toURL()); | ||
|
||
// we need to retrieve the classloader after adding URLs, otherwise we | ||
// would get its parent because of an optimization in the service | ||
final ClassLoader commonClassLoader = commonCLService.getCommonClassLoader(); | ||
commonClassLoader.loadClass(CommonClassLoaderServiceImplTestAdditionalClass.class.getName()); | ||
assertThat(commonCLService.getCommonClassPath(), containsString(new File(classesPath).getAbsolutePath())); | ||
} | ||
|
||
@Test | ||
public void testAddingUrlWithInitialUrl() throws MalformedURLException, ClassNotFoundException { | ||
final String domainDir = "target/test-domain"; | ||
serverEnv.setInstanceRoot(new File(domainDir)); | ||
commonCLService.postConstruct(); | ||
// the classloader should already be the one we want, initialized with classes in domain/lib/classes | ||
final ClassLoader commonClassLoader = commonCLService.getCommonClassLoader(); | ||
|
||
final String classesPath = "target/test-additional-classes/"; | ||
commonCLService.addToClassPath(new File(classesPath).toURI().toURL()); | ||
|
||
commonClassLoader.loadClass(CommonClassLoaderServiceImplTestAdditionalClass.class.getName()); | ||
commonClassLoader.loadClass(CommonClassLoaderServiceImplTestDomainClass.class.getName()); | ||
assertThat(commonCLService.getCommonClassPath(), containsString(new File(classesPath).getAbsolutePath())); | ||
assertThat(commonCLService.getCommonClassPath(), containsString(Path.of(domainDir,"lib","classes").toAbsolutePath().toString())); | ||
} | ||
|
||
static class MockServerEnvironment implements ServerEnvironment { | ||
|
||
File instanceRoot; | ||
|
||
@Override | ||
public File getInstanceRoot() { | ||
return instanceRoot; | ||
} | ||
|
||
public void setInstanceRoot(File instanceRoot) { | ||
this.instanceRoot = instanceRoot; | ||
} | ||
|
||
@Override | ||
public StartupContext getStartupContext() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public File getConfigDirPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getLibPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getApplicationRepositoryPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getApplicationStubPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getApplicationCompileJspPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getApplicationGeneratedXMLPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getApplicationEJBStubPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getApplicationPolicyFilePath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getApplicationAltDDPath() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getMasterPasswordFile() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getJKS() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public File getTrustStore() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public Status getStatus() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public RuntimeType getRuntimeType() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public String getInstanceName() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public boolean isInstance() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
@Override | ||
public boolean isDas() { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
|
||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...st/java/com/sun/enterprise/v3/server/CommonClassLoaderServiceImplTestAdditionalClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package com.sun.enterprise.v3.server; | ||
|
||
/** | ||
* To be used in CommonClassLoaderServiceImplTest | ||
* @author Ondro Mihalyi | ||
*/ | ||
public class CommonClassLoaderServiceImplTestAdditionalClass { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...c/test/java/com/sun/enterprise/v3/server/CommonClassLoaderServiceImplTestDomainClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
package com.sun.enterprise.v3.server; | ||
|
||
/** | ||
* To be used in CommonClassLoaderServiceImplTest | ||
* @author Ondro Mihalyi | ||
*/ | ||
public class CommonClassLoaderServiceImplTestDomainClass { | ||
|
||
} |