001package org.apache.maven.toolchain;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.toolchain.java.DefaultJavaToolChain;
023import org.apache.maven.toolchain.model.PersistedToolchains;
024import org.apache.maven.toolchain.model.ToolchainModel;
025import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
026import org.codehaus.plexus.logging.Logger;
027import org.junit.Before;
028import org.junit.Test;
029import org.mockito.Mock;
030import org.mockito.MockitoAnnotations;
031
032import java.io.InputStream;
033import java.util.Collections;
034
035import static org.junit.Assert.assertEquals;
036import static org.junit.Assert.assertFalse;
037import static org.junit.Assert.assertTrue;
038import static org.mockito.Mockito.verify;
039
040public class DefaultToolchainTest
041{
042    @Mock
043    private Logger logger;
044
045    private MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader();
046
047    @Before
048    public void setUp()
049        throws Exception
050    {
051        MockitoAnnotations.initMocks( this );
052    }
053
054    private DefaultToolchain newDefaultToolchain( ToolchainModel model )
055    {
056        return new DefaultToolchain( model, logger )
057        {
058            @Override
059            public String findTool( String toolName )
060            {
061                return null;
062            }
063        };
064    }
065
066    private DefaultToolchain newDefaultToolchain( ToolchainModel model, String type )
067    {
068        return new DefaultToolchain( model, type, logger )
069        {
070            @Override
071            public String findTool( String toolName )
072            {
073                return null;
074            }
075        };
076    }
077
078    @Test
079    public void testGetModel()
080    {
081        ToolchainModel model = new ToolchainModel();
082        DefaultToolchain toolchain = newDefaultToolchain( model );
083        assertEquals( model, toolchain.getModel() );
084    }
085
086    @Test
087    public void testGetType()
088    {
089        ToolchainModel model = new ToolchainModel();
090        DefaultToolchain toolchain = newDefaultToolchain( model, "TYPE" );
091        assertEquals( "TYPE", toolchain.getType() );
092
093        model.setType( "MODEL_TYPE" );
094        toolchain = newDefaultToolchain( model );
095        assertEquals( "MODEL_TYPE", toolchain.getType() );
096    }
097
098    @Test
099    public void testGetLogger()
100    {
101        ToolchainModel model = new ToolchainModel();
102        DefaultToolchain toolchain = newDefaultToolchain( model );
103        assertEquals( logger, toolchain.getLog() );
104    }
105
106    @Test
107    public void testMissingRequirementProperty()
108    {
109        ToolchainModel model = new ToolchainModel();
110        model.setType( "TYPE" );
111        DefaultToolchain toolchain = newDefaultToolchain( model );
112
113        assertFalse( toolchain.matchesRequirements( Collections.singletonMap( "name", "John Doe" ) ) );
114        verify( logger ).debug( "Toolchain type:TYPE{} is missing required property: name" );
115    }
116
117
118    @Test
119    public void testNonMatchingRequirementProperty()
120    {
121        ToolchainModel model = new ToolchainModel();
122        model.setType( "TYPE" );
123        DefaultToolchain toolchain = newDefaultToolchain( model );
124        toolchain.addProvideToken( "name", RequirementMatcherFactory.createExactMatcher( "Jane Doe" ) );
125
126        assertFalse( toolchain.matchesRequirements( Collections.singletonMap( "name", "John Doe" ) ) );
127        verify( logger ).debug( "Toolchain type:TYPE{name = Jane Doe} doesn't match required property: name" );
128    }
129
130
131    @Test
132    public void testEquals()
133        throws Exception
134    {
135        try ( InputStream jdksIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
136              InputStream jdksExtraIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks-extra.xml" ) )
137        {
138            PersistedToolchains jdks = reader.read( jdksIS );
139            PersistedToolchains jdksExtra = reader.read( jdksExtraIS );
140
141            DefaultToolchain tc1 = new DefaultJavaToolChain( jdks.getToolchains().get( 0 ), null );
142            DefaultToolchain tc2 = new DefaultJavaToolChain( jdksExtra.getToolchains().get( 0 ), null );
143
144            assertTrue( tc1.equals( tc1 ) );
145            assertFalse( tc1.equals( tc2 ) );
146            assertFalse( tc2.equals( tc1 ) );
147            assertTrue( tc2.equals( tc2 ) );
148        }
149    }
150}