View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.classrealm;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.HashSet;
24  import java.util.List;
25  
26  import org.apache.maven.extension.internal.CoreExports;
27  import org.apache.maven.internal.impl.internal.DefaultCoreRealm;
28  import org.apache.maven.model.Model;
29  import org.codehaus.plexus.DefaultPlexusContainer;
30  import org.codehaus.plexus.PlexusContainer;
31  import org.codehaus.plexus.PlexusContainerException;
32  import org.codehaus.plexus.classworlds.realm.ClassRealm;
33  import org.eclipse.aether.artifact.Artifact;
34  import org.junit.jupiter.api.Test;
35  import org.mockito.InOrder;
36  import org.mockito.MockedStatic;
37  import org.mockito.Mockito;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import static org.hamcrest.MatcherAssert.assertThat;
42  import static org.hamcrest.Matchers.endsWith;
43  import static org.junit.jupiter.api.Assertions.assertEquals;
44  import static org.mockito.Mockito.calls;
45  import static org.mockito.Mockito.inOrder;
46  import static org.mockito.Mockito.mock;
47  import static org.mockito.Mockito.never;
48  import static org.mockito.Mockito.when;
49  
50  /**
51   * @author Sebastien Doyon
52   */
53  class DefaultClassRealmManagerTest {
54  
55      private DefaultClassRealmManager newDefaultClassRealmManager(PlexusContainer container) {
56          HashSet<String> exportedPackages = new HashSet<String>();
57          exportedPackages.add("group1:artifact1");
58  
59          return new DefaultClassRealmManager(
60                  new DefaultCoreRealm(container),
61                  new ArrayList<ClassRealmManagerDelegate>(),
62                  new CoreExports(new ClassRealm(null, "test", null), new HashSet<String>(), exportedPackages));
63      }
64  
65      private List<Artifact> newTestArtifactList() {
66          List<Artifact> artifacts = new ArrayList<Artifact>();
67  
68          Artifact artifact = mock(Artifact.class);
69          when(artifact.getFile()).thenReturn(new File(new File("local/repository"), "some/path"));
70          when(artifact.getGroupId()).thenReturn("group1");
71          when(artifact.getArtifactId()).thenReturn("artifact1");
72          when(artifact.getExtension()).thenReturn("ext");
73          when(artifact.getClassifier()).thenReturn("classifier1");
74          when(artifact.getVersion()).thenReturn("1");
75          artifacts.add(artifact);
76  
77          Artifact artifact2 = mock(Artifact.class);
78          when(artifact2.getFile()).thenReturn(null);
79          when(artifact2.getGroupId()).thenReturn("group1");
80          when(artifact2.getArtifactId()).thenReturn("artifact2");
81          when(artifact2.getExtension()).thenReturn("ext");
82          when(artifact2.getClassifier()).thenReturn("classifier1");
83          when(artifact2.getVersion()).thenReturn("1");
84          artifacts.add(artifact2);
85  
86          return artifacts;
87      }
88  
89      private Model newTestModel() {
90          Model model = new Model();
91          model.setGroupId("modelGroup1");
92          model.setArtifactId("modelArtifact1");
93          model.setVersion("modelVersion1");
94  
95          return model;
96      }
97  
98      @Test
99      void testDebugEnabled() throws PlexusContainerException {
100         Logger logger = mock(Logger.class);
101         when(logger.isDebugEnabled()).thenReturn(true);
102 
103         DefaultClassRealmManager classRealmManager;
104         ClassRealm classRealm;
105 
106         InOrder verifier = inOrder(logger);
107 
108         PlexusContainer container = new DefaultPlexusContainer();
109 
110         try (MockedStatic<LoggerFactory> mockedLoggerFactory = Mockito.mockStatic(LoggerFactory.class)) {
111             mockedLoggerFactory
112                     .when(() -> LoggerFactory.getLogger(DefaultClassRealmManager.class))
113                     .thenReturn(logger);
114 
115             classRealmManager = newDefaultClassRealmManager(container);
116             classRealm = classRealmManager.createProjectRealm(newTestModel(), newTestArtifactList());
117         }
118 
119         assertEquals(classRealmManager.getMavenApiRealm(), classRealm.getParentClassLoader());
120         assertEquals("project>modelGroup1:modelArtifact1:modelVersion1", classRealm.getId());
121         assertEquals(1, classRealm.getURLs().length);
122         assertThat(classRealm.getURLs()[0].getPath(), endsWith("local/repository/some/path"));
123 
124         verifier.verify(logger, calls(1)).debug("Importing foreign packages into class realm {}", "maven.api");
125         verifier.verify(logger, calls(1)).debug("  Imported: {} < {}", "group1:artifact1", "test");
126         verifier.verify(logger, calls(1)).debug("  Excluded: {}", "group1:artifact2:ext:classifier1:null");
127         verifier.verify(logger, calls(1))
128                 .debug("Populating class realm {}", "project>modelGroup1:modelArtifact1:modelVersion1");
129         verifier.verify(logger, calls(1)).debug("  Included: {}", "group1:artifact1:ext:classifier1:null");
130     }
131 
132     @Test
133     void testDebugDisabled() throws PlexusContainerException {
134         Logger logger = mock(Logger.class);
135         when(logger.isDebugEnabled()).thenReturn(false);
136 
137         DefaultClassRealmManager classRealmManager;
138         ClassRealm classRealm;
139 
140         InOrder verifier = inOrder(logger);
141 
142         PlexusContainer container = new DefaultPlexusContainer();
143 
144         try (MockedStatic<LoggerFactory> mockedLoggerFactory = Mockito.mockStatic(LoggerFactory.class)) {
145             mockedLoggerFactory
146                     .when(() -> LoggerFactory.getLogger(DefaultClassRealmManager.class))
147                     .thenReturn(logger);
148 
149             classRealmManager = newDefaultClassRealmManager(container);
150             classRealm = classRealmManager.createProjectRealm(newTestModel(), newTestArtifactList());
151         }
152 
153         assertEquals(classRealmManager.getMavenApiRealm(), classRealm.getParentClassLoader());
154         assertEquals("project>modelGroup1:modelArtifact1:modelVersion1", classRealm.getId());
155         assertEquals(1, classRealm.getURLs().length);
156         assertThat(classRealm.getURLs()[0].getPath(), endsWith("local/repository/some/path"));
157 
158         verifier.verify(logger, calls(1)).debug("Importing foreign packages into class realm {}", "maven.api");
159         verifier.verify(logger, calls(1)).debug("  Imported: {} < {}", "group1:artifact1", "test");
160         verifier.verify(logger, calls(1))
161                 .debug("Populating class realm {}", "project>modelGroup1:modelArtifact1:modelVersion1");
162         verifier.verify(logger, never()).debug("  Included: {}", "group1:artifact1:ext:classifier1:null");
163         verifier.verify(logger, never()).debug("  Excluded: {}", "group1:artifact2:ext:classifier1:null");
164     }
165 }