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.toolchain;
20  
21  import java.util.Collections;
22  
23  import org.apache.maven.toolchain.java.DefaultJavaToolChain;
24  import org.apache.maven.toolchain.model.ToolchainModel;
25  import org.codehaus.plexus.util.xml.Xpp3Dom;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  import org.mockito.MockitoAnnotations;
29  import org.slf4j.Logger;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertFalse;
33  import static org.junit.jupiter.api.Assertions.assertNotEquals;
34  import static org.mockito.Mockito.mock;
35  import static org.mockito.Mockito.verify;
36  
37  class DefaultToolchainTest {
38      private final Logger logger = mock(Logger.class);
39  
40      @BeforeEach
41      void setUp() throws Exception {
42          MockitoAnnotations.initMocks(this);
43      }
44  
45      private DefaultToolchain newDefaultToolchain(ToolchainModel model) {
46          return new DefaultToolchain(model, logger) {
47              @Override
48              public String findTool(String toolName) {
49                  return null;
50              }
51          };
52      }
53  
54      private DefaultToolchain newDefaultToolchain(ToolchainModel model, String type) {
55          return new DefaultToolchain(model, type, logger) {
56              @Override
57              public String findTool(String toolName) {
58                  return null;
59              }
60          };
61      }
62  
63      @Test
64      void testGetModel() {
65          ToolchainModel model = new ToolchainModel();
66          DefaultToolchain toolchain = newDefaultToolchain(model);
67          assertEquals(model, toolchain.getModel());
68      }
69  
70      @Test
71      void testGetType() {
72          ToolchainModel model = new ToolchainModel();
73          DefaultToolchain toolchain = newDefaultToolchain(model, "TYPE");
74          assertEquals("TYPE", toolchain.getType());
75  
76          model.setType("MODEL_TYPE");
77          toolchain = newDefaultToolchain(model);
78          assertEquals("MODEL_TYPE", toolchain.getType());
79      }
80  
81      @Test
82      void testGetLogger() {
83          ToolchainModel model = new ToolchainModel();
84          DefaultToolchain toolchain = newDefaultToolchain(model);
85          assertEquals(logger, toolchain.getLog());
86      }
87  
88      @Test
89      void testMissingRequirementProperty() {
90          ToolchainModel model = new ToolchainModel();
91          model.setType("TYPE");
92          DefaultToolchain toolchain = newDefaultToolchain(model);
93  
94          assertFalse(toolchain.matchesRequirements(Collections.singletonMap("name", "John Doe")));
95          verify(logger).debug("Toolchain {} is missing required property: {}", toolchain, "name");
96      }
97  
98      @Test
99      void testNonMatchingRequirementProperty() {
100         ToolchainModel model = new ToolchainModel();
101         model.setType("TYPE");
102         DefaultToolchain toolchain = newDefaultToolchain(model);
103         toolchain.addProvideToken("name", RequirementMatcherFactory.createExactMatcher("Jane Doe"));
104 
105         assertFalse(toolchain.matchesRequirements(Collections.singletonMap("name", "John Doe")));
106         verify(logger).debug("Toolchain {} doesn't match required property: {}", toolchain, "name");
107     }
108 
109     @Test
110     void testEquals() {
111         ToolchainModel tm1 = new ToolchainModel();
112         tm1.setType("jdk");
113         tm1.addProvide("version", "1.5");
114         tm1.addProvide("vendor", "sun");
115         Xpp3Dom configuration1 = new Xpp3Dom("configuration");
116         Xpp3Dom jdkHome1 = new Xpp3Dom("jdkHome");
117         jdkHome1.setValue("${env.JAVA_HOME}");
118         configuration1.addChild(jdkHome1);
119         tm1.setConfiguration(configuration1);
120 
121         ToolchainModel tm2 = new ToolchainModel();
122         tm1.setType("jdk");
123         tm1.addProvide("version", "1.4");
124         tm1.addProvide("vendor", "sun");
125         Xpp3Dom configuration2 = new Xpp3Dom("configuration");
126         Xpp3Dom jdkHome2 = new Xpp3Dom("jdkHome");
127         jdkHome2.setValue("${env.JAVA_HOME}");
128         configuration2.addChild(jdkHome2);
129         tm2.setConfiguration(configuration2);
130 
131         DefaultToolchain tc1 = new DefaultJavaToolChain(tm1, null);
132         DefaultToolchain tc2 = new DefaultJavaToolChain(tm2, null);
133 
134         assertEquals(tc1, tc1);
135         assertNotEquals(tc1, tc2);
136         assertNotEquals(tc2, tc1);
137         assertEquals(tc2, tc2);
138     }
139 }