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 static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertFalse;
024import static org.junit.Assert.assertTrue;
025import static org.mockito.Mockito.verify;
026
027import java.io.InputStream;
028import java.util.Collections;
029
030import org.apache.maven.toolchain.java.DefaultJavaToolChain;
031import org.apache.maven.toolchain.model.PersistedToolchains;
032import org.apache.maven.toolchain.model.ToolchainModel;
033import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
034import org.codehaus.plexus.logging.Logger;
035import org.codehaus.plexus.util.IOUtil;
036import org.junit.Before;
037import org.junit.Test;
038import org.mockito.Mock;
039import org.mockito.MockitoAnnotations;
040
041public class DefaultToolchainTest
042{
043    @Mock
044    private Logger logger;
045    
046    private MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader();
047    
048    @Before
049    public void setUp() 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() throws Exception
133    {
134        InputStream jdksIS = null;
135        InputStream jdksExtraIS = null;
136        try
137        {
138            jdksIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
139            jdksExtraIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks-extra.xml" );
140
141            PersistedToolchains jdks = reader.read( jdksIS );
142            PersistedToolchains jdksExtra = reader.read( jdksExtraIS );
143
144            DefaultToolchain tc1 = new DefaultJavaToolChain( jdks.getToolchains().get( 0 ), null );
145            DefaultToolchain tc2 = new DefaultJavaToolChain( jdksExtra.getToolchains().get( 0 ), null );
146
147            assertTrue( tc1.equals( tc1 ) );
148            assertFalse( tc1.equals( tc2 ) );
149            assertFalse( tc2.equals( tc1 ) );
150            assertTrue( tc2.equals( tc2 ) );
151        }
152        finally
153        {
154            IOUtil.close( jdksIS );
155            IOUtil.close( jdksExtraIS );
156        }
157    }
158}