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