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.io.InputStream;
22  import java.util.Collections;
23  
24  import org.apache.maven.toolchain.java.DefaultJavaToolChain;
25  import org.apache.maven.toolchain.model.PersistedToolchains;
26  import org.apache.maven.toolchain.model.ToolchainModel;
27  import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
28  import org.codehaus.plexus.logging.Logger;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.mockito.Mock;
32  import org.mockito.MockitoAnnotations;
33  
34  import static org.junit.Assert.assertEquals;
35  import static org.junit.Assert.assertFalse;
36  import static org.junit.Assert.assertNotEquals;
37  import static org.mockito.Mockito.verify;
38  
39  public class DefaultToolchainTest {
40      @Mock
41      private Logger logger;
42  
43      private MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader();
44  
45      @Before
46      public void setUp() throws Exception {
47          MockitoAnnotations.initMocks(this);
48      }
49  
50      private DefaultToolchain newDefaultToolchain(ToolchainModel model) {
51          return new DefaultToolchain(model, logger) {
52              @Override
53              public String findTool(String toolName) {
54                  return null;
55              }
56          };
57      }
58  
59      private DefaultToolchain newDefaultToolchain(ToolchainModel model, String type) {
60          return new DefaultToolchain(model, type, logger) {
61              @Override
62              public String findTool(String toolName) {
63                  return null;
64              }
65          };
66      }
67  
68      @Test
69      public void testGetModel() {
70          ToolchainModel model = new ToolchainModel();
71          DefaultToolchain toolchain = newDefaultToolchain(model);
72          assertEquals(model, toolchain.getModel());
73      }
74  
75      @Test
76      public void testGetType() {
77          ToolchainModel model = new ToolchainModel();
78          DefaultToolchain toolchain = newDefaultToolchain(model, "TYPE");
79          assertEquals("TYPE", toolchain.getType());
80  
81          model.setType("MODEL_TYPE");
82          toolchain = newDefaultToolchain(model);
83          assertEquals("MODEL_TYPE", toolchain.getType());
84      }
85  
86      @Test
87      public void testGetLogger() {
88          ToolchainModel model = new ToolchainModel();
89          DefaultToolchain toolchain = newDefaultToolchain(model);
90          assertEquals(logger, toolchain.getLog());
91      }
92  
93      @Test
94      public void testMissingRequirementProperty() {
95          ToolchainModel model = new ToolchainModel();
96          model.setType("TYPE");
97          DefaultToolchain toolchain = newDefaultToolchain(model);
98  
99          assertFalse(toolchain.matchesRequirements(Collections.singletonMap("name", "John Doe")));
100         verify(logger).debug("Toolchain type:TYPE{} is missing required property: name");
101     }
102 
103     @Test
104     public void testNonMatchingRequirementProperty() {
105         ToolchainModel model = new ToolchainModel();
106         model.setType("TYPE");
107         DefaultToolchain toolchain = newDefaultToolchain(model);
108         toolchain.addProvideToken("name", RequirementMatcherFactory.createExactMatcher("Jane Doe"));
109 
110         assertFalse(toolchain.matchesRequirements(Collections.singletonMap("name", "John Doe")));
111         verify(logger).debug("Toolchain type:TYPE{name = Jane Doe} doesn't match required property: name");
112     }
113 
114     @Test
115     public void testEquals() throws Exception {
116         try (InputStream jdksIS = ToolchainModel.class.getResourceAsStream("toolchains-jdks.xml");
117                 InputStream jdksExtraIS = ToolchainModel.class.getResourceAsStream("toolchains-jdks-extra.xml")) {
118             PersistedToolchains jdks = reader.read(jdksIS);
119             PersistedToolchains jdksExtra = reader.read(jdksExtraIS);
120 
121             DefaultToolchain tc1 = new DefaultJavaToolChain(jdks.getToolchains().get(0), null);
122             DefaultToolchain tc2 =
123                     new DefaultJavaToolChain(jdksExtra.getToolchains().get(0), null);
124 
125             assertEquals(tc1, tc1);
126             assertNotEquals(tc1, tc2);
127             assertNotEquals(tc2, tc1);
128             assertEquals(tc2, tc2);
129         }
130     }
131 }