[MNG-7063] Add unittest to ensure rereading a pom won't return the cached Model

This commit is contained in:
rfscholte 2021-02-14 16:56:25 +01:00
parent 619973b91c
commit 84a1ff2d5c
3 changed files with 105 additions and 73 deletions

View File

@ -19,71 +19,45 @@ package org.apache.maven.project;
* under the License. * under the License.
*/ */
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.codehaus.plexus.util.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.maven.test.PlexusExtension.getTestFile; import static org.apache.maven.test.PlexusExtension.getTestFile;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class DefaultMavenProjectBuilderTest public class DefaultMavenProjectBuilderTest
extends AbstractMavenProjectTestCase extends AbstractMavenProjectTestCase
{ {
@TempDir
private List<File> filesToDelete = new ArrayList<>(); File localRepoDir;
private File localRepoDir; // only use by reread()
@TempDir
Path projectRoot;
@Override @Override
@BeforeEach @BeforeEach
public void setUp() public void setUp()
throws Exception throws Exception
{ {
super.setUp();
projectBuilder = getContainer().lookup( ProjectBuilder.class ); projectBuilder = getContainer().lookup( ProjectBuilder.class );
localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() );
localRepoDir.mkdirs();
filesToDelete.add( localRepoDir );
}
@AfterEach
public void tearDown()
throws Exception
{
if ( !filesToDelete.isEmpty() )
{
for ( File file : filesToDelete )
{
if ( file.exists() )
{
if ( file.isDirectory() )
{
FileUtils.deleteDirectory( file );
}
else
{
file.delete();
}
}
}
}
} }
protected MavenProject getProject( Artifact pom, boolean allowStub ) protected MavenProject getProject( Artifact pom, boolean allowStub )
@ -135,7 +109,7 @@ public class DefaultMavenProjectBuilderTest
ProjectBuildingException.class, ProjectBuildingException.class,
() -> getProject( f1 ), () -> getProject( f1 ),
"Expected to fail for future versions" ); "Expected to fail for future versions" );
assertContains( "Building this project requires a newer version of Maven", e.getMessage() ); assertThat( e.getMessage(), containsString( "Building this project requires a newer version of Maven" ) );
} }
@Test @Test
@ -150,7 +124,7 @@ public class DefaultMavenProjectBuilderTest
ProjectBuildingException.class, ProjectBuildingException.class,
() -> getProject( f1 ), () -> getProject( f1 ),
"Expected to fail for past versions" ); "Expected to fail for past versions" );
assertContains( "Building this project requires an older version of Maven", e.getMessage() ); assertThat( e.getMessage(), containsString( "Building this project requires an older version of Maven" ) );
} }
@Test @Test
@ -163,17 +137,7 @@ public class DefaultMavenProjectBuilderTest
ProjectBuildingException.class, ProjectBuildingException.class,
() -> getProject( f1 ), () -> getProject( f1 ),
"Expected to fail for future versions" ); "Expected to fail for future versions" );
assertContains( "Building this project requires a newer version of Maven", e.getMessage() ); assertThat( e.getMessage(), containsString( "Building this project requires a newer version of Maven" ) );
}
private void assertContains( String expected, String actual )
{
if ( actual == null || !actual.contains( expected ) )
{
fail( "Expected: a string containing " + expected + "\nActual: " + ( actual == null
? "null"
: "'" + actual + "'" ) );
}
} }
@Test @Test
@ -204,16 +168,6 @@ public class DefaultMavenProjectBuilderTest
return repositorySystem.createLocalRepository( getLocalRepositoryPath() ); return repositorySystem.createLocalRepository( getLocalRepositoryPath() );
} }
public void xtestLoop()
throws Exception
{
while ( true )
{
File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
getProject( f1 );
}
}
@Test @Test
public void testPartialResultUponBadDependencyDeclaration() public void testPartialResultUponBadDependencyDeclaration()
throws Exception throws Exception
@ -273,7 +227,6 @@ public class DefaultMavenProjectBuilderTest
ProjectBuildingException.class, ProjectBuildingException.class,
() -> getProject( f1 ), () -> getProject( f1 ),
"Expected 'ProjectBuildingException' not thrown." ); "Expected 'ProjectBuildingException' not thrown." );
assertNotNull( e.getMessage() );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
} }
@ -293,7 +246,6 @@ public class DefaultMavenProjectBuilderTest
ProjectBuildingException.class, ProjectBuildingException.class,
() -> getProject( f1 ), () -> getProject( f1 ),
"Expected 'ProjectBuildingException' not thrown." ); "Expected 'ProjectBuildingException' not thrown." );
assertNotNull( e.getMessage() );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
} }
@ -333,7 +285,6 @@ public class DefaultMavenProjectBuilderTest
ProjectBuildingException.class, ProjectBuildingException.class,
() -> getProjectFromRemoteRepository( f1 ), () -> getProjectFromRemoteRepository( f1 ),
"Expected 'ProjectBuildingException' not thrown." ); "Expected 'ProjectBuildingException' not thrown." );
assertNotNull( e.getMessage() );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
} }
@ -353,8 +304,36 @@ public class DefaultMavenProjectBuilderTest
ProjectBuildingException.class, ProjectBuildingException.class,
() -> getProjectFromRemoteRepository( f1 ), () -> getProjectFromRemoteRepository( f1 ),
"Expected 'ProjectBuildingException' not thrown." ); "Expected 'ProjectBuildingException' not thrown." );
assertNotNull( e.getMessage() );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) ); assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
} }
/**
* Ensure that when re-reading a pom, it should not use the cached Model
*
* @throws Exception
*/
@Test
public void rereadPom_mng7063() throws Exception
{
final Path pom = projectRoot.resolve( "pom.xml" );
try ( InputStream pomResource =
DefaultMavenProjectBuilderTest.class.getResourceAsStream( "/projects/reread/pom1.xml" ) )
{
Files.copy( pomResource, pom, StandardCopyOption.REPLACE_EXISTING );
}
MavenProject project = getProject( pom.toFile() );
assertThat( project.getName(), is( "aid" ) ); // inherited from artifactId
try ( InputStream pomResource =
DefaultMavenProjectBuilderTest.class.getResourceAsStream( "/projects/reread/pom2.xml" ) )
{
Files.copy( pomResource, pom, StandardCopyOption.REPLACE_EXISTING );
}
project = getProject( pom.toFile() );
assertThat( project.getName(), is( "PROJECT NAME" ) );
}
} }

View File

@ -0,0 +1,26 @@
<?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>
<modelVersion>4.0.0</modelVersion>
<groupId>gid</groupId>
<artifactId>aid</artifactId>
<version>1.0</version>
</project>

View File

@ -0,0 +1,27 @@
<?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>
<modelVersion>4.0.0</modelVersion>
<groupId>gid</groupId>
<artifactId>aid</artifactId>
<version>1.0</version>
<name>PROJECT NAME</name>
</project>