001package org.apache.maven.toolchain.building; 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.assertNotNull; 024import static org.mockito.Matchers.any; 025import static org.mockito.Matchers.anyMap; 026import static org.mockito.Mockito.when; 027 028import java.io.IOException; 029import java.io.InputStream; 030 031import org.apache.maven.building.StringSource; 032import org.apache.maven.toolchain.io.ToolchainsParseException; 033import org.apache.maven.toolchain.io.ToolchainsReader; 034import org.apache.maven.toolchain.model.PersistedToolchains; 035import org.apache.maven.toolchain.model.ToolchainModel; 036import org.junit.Before; 037import org.junit.Test; 038import org.mockito.InjectMocks; 039import org.mockito.Mock; 040import org.mockito.MockitoAnnotations; 041 042public class DefaultToolchainsBuilderTest 043{ 044 private static final String LS = System.getProperty( "line.separator" ); 045 046 @Mock 047 private ToolchainsReader toolchainsReader; 048 049 @InjectMocks 050 private DefaultToolchainsBuilder toolchainBuilder = new DefaultToolchainsBuilder(); 051 052 @Before 053 public void onSetup() 054 { 055 MockitoAnnotations.initMocks( this ); 056 } 057 058 @Test 059 public void testBuildEmptyRequest() 060 throws Exception 061 { 062 ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); 063 ToolchainsBuildingResult result = toolchainBuilder.build( request ); 064 assertNotNull( result.getEffectiveToolchains() ); 065 assertNotNull( result.getProblems() ); 066 assertEquals( 0, result.getProblems().size() ); 067 } 068 069 @Test 070 public void testBuildRequestWithUserToolchains() 071 throws Exception 072 { 073 ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); 074 request.setUserToolchainsSource( new StringSource( "" ) ); 075 076 PersistedToolchains userResult = new PersistedToolchains(); 077 ToolchainModel toolchain = new ToolchainModel(); 078 toolchain.setType( "TYPE" ); 079 toolchain.addProvide( "key", "user_value" ); 080 userResult.addToolchain( toolchain ); 081 when( toolchainsReader.read( any( InputStream.class ), anyMap() ) ).thenReturn( userResult ); 082 083 ToolchainsBuildingResult result = toolchainBuilder.build( request ); 084 assertNotNull( result.getEffectiveToolchains() ); 085 assertEquals( 1, result.getEffectiveToolchains().getToolchains().size() ); 086 assertEquals( "TYPE", result.getEffectiveToolchains().getToolchains().get(0).getType() ); 087 assertEquals( "user_value", result.getEffectiveToolchains().getToolchains().get(0).getProvides().getProperty( "key" ) ); 088 assertNotNull( result.getProblems() ); 089 assertEquals( 0, result.getProblems().size() ); 090 } 091 092 @Test 093 public void testBuildRequestWithGlobalToolchains() 094 throws Exception 095 { 096 ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); 097 request.setGlobalToolchainsSource( new StringSource( "" ) ); 098 099 PersistedToolchains globalResult = new PersistedToolchains(); 100 ToolchainModel toolchain = new ToolchainModel(); 101 toolchain.setType( "TYPE" ); 102 toolchain.addProvide( "key", "global_value" ); 103 globalResult.addToolchain( toolchain ); 104 when( toolchainsReader.read( any( InputStream.class ), anyMap() ) ).thenReturn( globalResult ); 105 106 ToolchainsBuildingResult result = toolchainBuilder.build( request ); 107 assertNotNull( result.getEffectiveToolchains() ); 108 assertEquals( 1, result.getEffectiveToolchains().getToolchains().size() ); 109 assertEquals( "TYPE", result.getEffectiveToolchains().getToolchains().get(0).getType() ); 110 assertEquals( "global_value", result.getEffectiveToolchains().getToolchains().get(0).getProvides().getProperty( "key" ) ); 111 assertNotNull( result.getProblems() ); 112 assertEquals( 0, result.getProblems().size() ); 113 } 114 115 @Test 116 public void testBuildRequestWithBothToolchains() 117 throws Exception 118 { 119 ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); 120 request.setGlobalToolchainsSource( new StringSource( "" ) ); 121 request.setUserToolchainsSource( new StringSource( "" ) ); 122 123 PersistedToolchains userResult = new PersistedToolchains(); 124 ToolchainModel userToolchain = new ToolchainModel(); 125 userToolchain.setType( "TYPE" ); 126 userToolchain.addProvide( "key", "user_value" ); 127 userResult.addToolchain( userToolchain ); 128 129 PersistedToolchains globalResult = new PersistedToolchains(); 130 ToolchainModel globalToolchain = new ToolchainModel(); 131 globalToolchain.setType( "TYPE" ); 132 globalToolchain.addProvide( "key", "global_value" ); 133 globalResult.addToolchain( globalToolchain ); 134 when( toolchainsReader.read( any( InputStream.class ), anyMap() ) ).thenReturn( globalResult ).thenReturn( userResult ); 135 136 ToolchainsBuildingResult result = toolchainBuilder.build( request ); 137 assertNotNull( result.getEffectiveToolchains() ); 138 assertEquals( 2, result.getEffectiveToolchains().getToolchains().size() ); 139 assertEquals( "TYPE", result.getEffectiveToolchains().getToolchains().get(0).getType() ); 140 assertEquals( "user_value", result.getEffectiveToolchains().getToolchains().get(0).getProvides().getProperty( "key" ) ); 141 assertEquals( "TYPE", result.getEffectiveToolchains().getToolchains().get(1).getType() ); 142 assertEquals( "global_value", result.getEffectiveToolchains().getToolchains().get(1).getProvides().getProperty( "key" ) ); 143 assertNotNull( result.getProblems() ); 144 assertEquals( 0, result.getProblems().size() ); 145 } 146 147 @Test 148 public void testStrictToolchainsParseException() throws Exception 149 { 150 ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); 151 request.setGlobalToolchainsSource( new StringSource( "" ) ); 152 ToolchainsParseException parseException = new ToolchainsParseException( "MESSAGE", 4, 2 ); 153 when( toolchainsReader.read( any( InputStream.class ), anyMap() ) ).thenThrow( parseException ); 154 155 try 156 { 157 toolchainBuilder.build( request ); 158 } 159 catch ( ToolchainsBuildingException e ) 160 { 161 assertEquals( "1 problem was encountered while building the effective toolchains" + LS + 162 "[FATAL] Non-parseable toolchains (memory): MESSAGE @ line 4, column 2" + LS, e.getMessage() ); 163 } 164 } 165 166 @Test 167 public void testIOException() throws Exception 168 { 169 ToolchainsBuildingRequest request = new DefaultToolchainsBuildingRequest(); 170 request.setGlobalToolchainsSource( new StringSource( "", "LOCATION" ) ); 171 IOException ioException = new IOException( "MESSAGE" ); 172 when( toolchainsReader.read( any( InputStream.class ), anyMap() ) ).thenThrow( ioException ); 173 174 try 175 { 176 toolchainBuilder.build( request ); 177 } 178 catch ( ToolchainsBuildingException e ) 179 { 180 assertEquals( "1 problem was encountered while building the effective toolchains" + LS + 181 "[FATAL] Non-readable toolchains LOCATION: MESSAGE" + LS, e.getMessage() ); 182 } 183 } 184 185}