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.model.Model;
28  import org.codehaus.plexus.DefaultPlexusContainer;
29  import org.codehaus.plexus.PlexusContainer;
30  import org.codehaus.plexus.PlexusContainerException;
31  import org.codehaus.plexus.classworlds.realm.ClassRealm;
32  import org.eclipse.aether.artifact.Artifact;
33  import org.junit.jupiter.api.Test;
34  import org.mockito.InOrder;
35  import org.mockito.MockedStatic;
36  import org.mockito.Mockito;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  import static org.hamcrest.MatcherAssert.assertThat;
41  import static org.hamcrest.Matchers.endsWith;
42  import static org.junit.jupiter.api.Assertions.assertEquals;
43  import static org.mockito.Mockito.calls;
44  import static org.mockito.Mockito.inOrder;
45  import static org.mockito.Mockito.mock;
46  import static org.mockito.Mockito.never;
47  import static org.mockito.Mockito.when;
48  
49  /**
50   * @author Sebastien Doyon
51   */
52  class DefaultClassRealmManagerTest {
53  
54      private DefaultClassRealmManager newDefaultClassRealmManager(PlexusContainer container) {
55          HashSet<String> exportedPackages = new HashSet<String>();
56          exportedPackages.add("group1:artifact1");
57  
58          return new DefaultClassRealmManager(
59                  container,
60                  new ArrayList<ClassRealmManagerDelegate>(),
61                  new CoreExports(new ClassRealm(null, "test", null), new HashSet<String>(), exportedPackages));
62      }
63  
64      private List<Artifact> newTestArtifactList() {
65          List<Artifact> artifacts = new ArrayList<Artifact>();
66  
67          Artifact artifact = mock(Artifact.class);
68          when(artifact.getFile()).thenReturn(new File(new File("local/repository"), "some/path"));
69          when(artifact.getGroupId()).thenReturn("group1");
70          when(artifact.getArtifactId()).thenReturn("artifact1");
71          when(artifact.getExtension()).thenReturn("ext");
72          when(artifact.getClassifier()).thenReturn("classifier1");
73          when(artifact.getVersion()).thenReturn("1");
74          artifacts.add(artifact);
75  
76          Artifact artifact2 = mock(Artifact.class);
77          when(artifact2.getFile()).thenReturn(null);
78          when(artifact2.getGroupId()).thenReturn("group1");
79          when(artifact2.getArtifactId()).thenReturn("artifact2");
80          when(artifact2.getExtension()).thenReturn("ext");
81          when(artifact2.getClassifier()).thenReturn("classifier1");
82          when(artifact2.getVersion()).thenReturn("1");
83          artifacts.add(artifact2);
84  
85          return artifacts;
86      }
87  
88      private Model newTestModel() {
89          Model model = new Model();
90          model.setGroupId("modelGroup1");
91          model.setArtifactId("modelArtifact1");
92          model.setVersion("modelVersion1");
93  
94          return model;
95      }
96  
97      @Test
98      void testDebugEnabled() throws PlexusContainerException {
99          Logger logger = mock(Logger.class);
100         when(logger.isDebugEnabled()).thenReturn(true);
101 
102         DefaultClassRealmManager classRealmManager;
103         ClassRealm classRealm;
104 
105         InOrder verifier = inOrder(logger);
106 
107         PlexusContainer container = new DefaultPlexusContainer();
108 
109         try (MockedStatic<LoggerFactory> mockedLoggerFactory = Mockito.mockStatic(LoggerFactory.class)) {
110             mockedLoggerFactory
111                     .when(() -> LoggerFactory.getLogger(DefaultClassRealmManager.class))
112                     .thenReturn(logger);
113 
114             classRealmManager = newDefaultClassRealmManager(container);
115             classRealm = classRealmManager.createProjectRealm(newTestModel(), newTestArtifactList());
116         }
117 
118         assertEquals(classRealmManager.getMavenApiRealm(), classRealm.getParentClassLoader());
119         assertEquals("project>modelGroup1:modelArtifact1:modelVersion1", classRealm.getId());
120         assertEquals(1, classRealm.getURLs().length);
121         assertThat(classRealm.getURLs()[0].getPath(), endsWith("local/repository/some/path"));
122 
123         verifier.verify(logger, calls(1)).debug("Importing foreign packages into class realm {}", "maven.api");
124         verifier.verify(logger, calls(1)).debug("  Imported: {} < {}", "group1:artifact1", "test");
125         verifier.verify(logger, calls(1)).debug("  Excluded: {}", "group1:artifact2:ext:classifier1:null");
126         verifier.verify(logger, calls(1))
127                 .debug("Populating class realm {}", "project>modelGroup1:modelArtifact1:modelVersion1");
128         verifier.verify(logger, calls(1)).debug("  Included: {}", "group1:artifact1:ext:classifier1:null");
129     }
130 
131     @Test
132     void testDebugDisabled() throws PlexusContainerException {
133         Logger logger = mock(Logger.class);
134         when(logger.isDebugEnabled()).thenReturn(false);
135 
136         DefaultClassRealmManager classRealmManager;
137         ClassRealm classRealm;
138 
139         InOrder verifier = inOrder(logger);
140 
141         PlexusContainer container = new DefaultPlexusContainer();
142 
143         try (MockedStatic<LoggerFactory> mockedLoggerFactory = Mockito.mockStatic(LoggerFactory.class)) {
144             mockedLoggerFactory
145                     .when(() -> LoggerFactory.getLogger(DefaultClassRealmManager.class))
146                     .thenReturn(logger);
147 
148             classRealmManager = newDefaultClassRealmManager(container);
149             classRealm = classRealmManager.createProjectRealm(newTestModel(), newTestArtifactList());
150         }
151 
152         assertEquals(classRealmManager.getMavenApiRealm(), classRealm.getParentClassLoader());
153         assertEquals("project>modelGroup1:modelArtifact1:modelVersion1", classRealm.getId());
154         assertEquals(1, classRealm.getURLs().length);
155         assertThat(classRealm.getURLs()[0].getPath(), endsWith("local/repository/some/path"));
156 
157         verifier.verify(logger, calls(1)).debug("Importing foreign packages into class realm {}", "maven.api");
158         verifier.verify(logger, calls(1)).debug("  Imported: {} < {}", "group1:artifact1", "test");
159         verifier.verify(logger, calls(1))
160                 .debug("Populating class realm {}", "project>modelGroup1:modelArtifact1:modelVersion1");
161         verifier.verify(logger, never()).debug("  Included: {}", "group1:artifact1:ext:classifier1:null");
162         verifier.verify(logger, never()).debug("  Excluded: {}", "group1:artifact2:ext:classifier1:null");
163     }
164 }