Skip to content

Commit

Permalink
Merge branch 'master' into MNG-6957
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Oct 30, 2020
2 parents b9607f0 + e7eef9d commit b6f0b12
Show file tree
Hide file tree
Showing 25 changed files with 660 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Consumer;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLInputFactory;
Expand All @@ -37,19 +38,21 @@
import org.apache.maven.model.building.AbstractModelSourceTransformer;
import org.apache.maven.model.building.DefaultBuildPomXMLFilterFactory;
import org.apache.maven.model.building.TransformerContext;
import org.apache.maven.xml.Factories;
import org.apache.maven.xml.internal.DefaultConsumerPomXMLFilterFactory;
import org.apache.maven.xml.sax.filter.AbstractSAXFilter;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;

class ConsumerModelSourceTransformer extends AbstractModelSourceTransformer
{
@Override
protected AbstractSAXFilter getSAXFilter( Path pomFile, TransformerContext context )
protected AbstractSAXFilter getSAXFilter( Path pomFile,
TransformerContext context,
Consumer<LexicalHandler> lexicalHandlerConsumer )
throws TransformerConfigurationException, SAXException, ParserConfigurationException
{
return new DefaultConsumerPomXMLFilterFactory( new DefaultBuildPomXMLFilterFactory( context,
true ) ).get( pomFile );
lexicalHandlerConsumer, true ) ).get( pomFile );
}

/**
Expand All @@ -66,8 +69,7 @@ protected TransformerHandler getTransformerHandler( Path pomFile )
{
final TransformerHandler transformerHandler;

final SAXTransformerFactory transformerFactory =
(SAXTransformerFactory) Factories.newTransformerFactory();
final SAXTransformerFactory transformerFactory = getTransformerFactory();

// Keep same encoding+version
try ( InputStream input = Files.newInputStream( pomFile ) )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.apache.maven.internal.aether;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.maven.model.Model;
import org.apache.maven.model.building.TransformerContext;
import org.junit.Test;
import org.xmlunit.assertj.XmlAssert;

public class ConsumerModelSourceTransformerTest
{
private ConsumerModelSourceTransformer transformer = new ConsumerModelSourceTransformer();

@Test
public void transform() throws Exception
{
Path beforePomFile = Paths.get( "src/test/resources/projects/transform/before.pom").toAbsolutePath();
Path afterPomFile = Paths.get( "src/test/resources/projects/transform/after.pom").toAbsolutePath();

try( InputStream expected = Files.newInputStream( afterPomFile );
InputStream result = transformer.transform( beforePomFile, new NoTransformerContext() ) )
{
XmlAssert.assertThat( result ).and( expected ).areIdentical();
}
}

private static class NoTransformerContext implements TransformerContext
{
@Override
public String getUserProperty( String key )
{
return null;
}

@Override
public Model getRawModel( String groupId, String artifactId )
throws IllegalStateException
{
return null;
}

@Override
public Model getRawModel( Path p )
{
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;

import java.io.File;
import java.nio.file.Files;
Expand Down Expand Up @@ -91,15 +92,11 @@ public void testVersionlessManagedDependency()
ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
configuration.setRepositorySession( mavenSession.getRepositorySession() );

try
{
lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
fail();
}
catch ( ProjectBuildingException e )
{
// this is expected
}
ProjectBuildingException e = assertThrows( ProjectBuildingException.class,
() -> lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration ) );
assertThat( e.getMessage(),
containsString( "[ERROR] 'dependencies.dependency.version' for org.apache.maven.its:a:jar is missing. "
+ "@ line 9, column 17" ) );
}

public void testResolveDependencies()
Expand All @@ -115,7 +112,9 @@ public void testResolveDependencies()
ProjectBuildingResult result = lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration );
assertEquals( 1, result.getProject().getArtifacts().size() );
// multi projects build entry point
List<ProjectBuildingResult> results = lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false, configuration );
List<ProjectBuildingResult> results =
lookup( org.apache.maven.project.ProjectBuilder.class ).build( Collections.singletonList( pomFile ), false,
configuration );
assertEquals( 1, results.size() );
MavenProject mavenProject = results.get( 0 ).getProject();
assertEquals( 1, mavenProject.getArtifacts().size() );
Expand Down Expand Up @@ -192,32 +191,25 @@ public void testReadErroneousMavenProjectContainsReference()
lookup( org.apache.maven.project.ProjectBuilder.class );

// single project build entry point
try
{
projectBuilder.build( pomFile, configuration );
}
catch ( ProjectBuildingException ex )
{
assertEquals( 1, ex.getResults().size() );
MavenProject project = ex.getResults().get( 0 ).getProject();
assertNotNull( project );
assertEquals( "testArtifactMissingVersion", project.getArtifactId() );
assertEquals( pomFile, project.getFile() );
}
ProjectBuildingException ex1 =
assertThrows( ProjectBuildingException.class, () -> projectBuilder.build( pomFile, configuration ) );

assertEquals( 1, ex1.getResults().size() );
MavenProject project1 = ex1.getResults().get( 0 ).getProject();
assertNotNull( project1 );
assertEquals( "testArtifactMissingVersion", project1.getArtifactId() );
assertEquals( pomFile, project1.getFile() );

// multi projects build entry point
try
{
projectBuilder.build( Collections.singletonList( pomFile ), false, configuration );
}
catch ( ProjectBuildingException ex )
{
assertEquals( 1, ex.getResults().size() );
MavenProject project = ex.getResults().get( 0 ).getProject();
assertNotNull( project );
assertEquals( "testArtifactMissingVersion", project.getArtifactId() );
assertEquals( pomFile, project.getFile() );
}
ProjectBuildingException ex2 =
assertThrows( ProjectBuildingException.class,
() -> projectBuilder.build( Collections.singletonList( pomFile ), false, configuration ) );

assertEquals( 1, ex2.getResults().size() );
MavenProject project2 = ex2.getResults().get( 0 ).getProject();
assertNotNull( project2 );
assertEquals( "testArtifactMissingVersion", project2.getArtifactId() );
assertEquals( pomFile, project2.getFile() );
}

public void testReadInvalidPom()
Expand All @@ -232,27 +224,17 @@ public void testReadInvalidPom()
lookup( org.apache.maven.project.ProjectBuilder.class );

// single project build entry point
try
{
projectBuilder.build( pomFile, configuration );
}
catch ( Exception ex )
{
assertThat( ex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) );
}
Exception ex = assertThrows( Exception.class, () -> projectBuilder.build( pomFile, configuration ) );
assertThat( ex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) );

// multi projects build entry point
try
{
projectBuilder.build( Collections.singletonList( pomFile ), false, configuration );
}
catch ( ProjectBuildingException ex )
{
assertEquals( 1, ex.getResults().size() );
assertNotNull( ex.getResults().get( 0 ).getPomFile() );
assertThat( ex.getResults().get( 0 ).getProblems().size(), greaterThan( 0 ) );
assertThat( ex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) );
}
ProjectBuildingException pex =
assertThrows( ProjectBuildingException.class,
() -> projectBuilder.build( Collections.singletonList( pomFile ), false, configuration ) );
assertEquals( 1, pex.getResults().size() );
assertNotNull( pex.getResults().get( 0 ).getPomFile() );
assertThat( pex.getResults().get( 0 ).getProblems().size(), greaterThan( 0 ) );
assertThat( pex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) );
}

public void testReadParentAndChildWithRegularVersionSetParentFile()
Expand Down
83 changes: 83 additions & 0 deletions maven-core/src/test/resources/projects/transform/after.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>



<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source> 1.5 </source>
<target xml:space="preserve"> 1.5 </target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>test</id>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</profile>
<profile>
<id>file</id>
<activation>
<file>
<exists>simple.xml</exists>
</file>
</activation>
<properties>
<profile.file>activated</profile.file>
</properties>
</profile>
</profiles>
</project>
Loading

0 comments on commit b6f0b12

Please sign in to comment.