Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/ReportUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/ReportUtils.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/ReportUtils.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/ReportUtils.java Mon May 15 21:10:27 2017 @@ -0,0 +1,63 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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.File; +import java.io.FilenameFilter; + +/** + * Provides utility methods for invoker report processing. + * + * @author Benjamin Bentmann + */ +class ReportUtils +{ + + private static class FileFilterOnlyXmlFile + implements FilenameFilter + { + + public boolean accept( File dir, String name ) + { + return name.startsWith( "BUILD-" ) && name.endsWith( ".xml" ); + } + + } + + /** + * Gets the paths to the invoker reports available in the specified directory. + * + * @param reportsDirectory The base directory where the invoker reports are located in, may be <code>null</code>. + * @return The paths to the invoker reports, can be empty but never <code>null</code>. + */ + public static File[] getReportFiles( File reportsDirectory ) + { + File[] reportFiles = + ( reportsDirectory != null ) ? reportsDirectory.listFiles( new FileFilterOnlyXmlFile() ) : null; + + if ( reportFiles == null ) + { + reportFiles = new File[0]; + } + + return reportFiles; + } + +}
Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/Selector.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/Selector.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/Selector.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/Selector.java Mon May 15 21:10:27 2017 @@ -0,0 +1,113 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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. + */ + +/** + * + * @author Robert Scholte + * + */ +class Selector +{ + static final int SELECTOR_MAVENVERSION = 1; + + static final int SELECTOR_JREVERSION = 2; + + static final int SELECTOR_OSFAMILY = 4; + + static final int SELECTOR_MULTI = 8; + + private final String actualMavenVersion; + + private final String actualJavaVersion; + + public Selector( String actualMavenVersion, String actualJavaVersion ) + { + this.actualMavenVersion = actualMavenVersion; + this.actualJavaVersion = actualJavaVersion; + } + + public int getSelection( InvokerProperties invokerProperties ) + { + if ( !invokerProperties.isSelectorDefined( 1 ) ) + { + return getGlobal( invokerProperties ); + } + + for ( int selectorIndex = 1;; selectorIndex++ ) + { + if ( selectorIndex > 1 && !invokerProperties.isSelectorDefined( selectorIndex ) ) + { + break; + } + + int selection = 0; + if ( !SelectorUtils.isMavenVersion( invokerProperties.getMavenVersion( selectorIndex ), + actualMavenVersion ) ) + { + selection |= SELECTOR_MAVENVERSION; + } + + if ( !SelectorUtils.isJreVersion( invokerProperties.getJreVersion( selectorIndex ), actualJavaVersion ) ) + { + selection |= SELECTOR_JREVERSION; + } + + if ( !SelectorUtils.isOsFamily( invokerProperties.getOsFamily( selectorIndex ) ) ) + { + selection |= SELECTOR_OSFAMILY; + } + + if ( selection == 0 ) + { + return 0; + } + } + return SELECTOR_MULTI; + } + + /** + * Determines whether selector conditions of the specified invoker properties match the current environment. + * + * @param invokerProperties The invoker properties to check, must not be <code>null</code>. + * @return <code>0</code> if the job corresponding to the properties should be run, otherwise a bitwise value + * representing the reason why it should be skipped. + */ + private int getGlobal( InvokerProperties invokerProperties ) + { + int selection = 0; + if ( !SelectorUtils.isMavenVersion( invokerProperties.getMavenVersion(), actualMavenVersion ) ) + { + selection |= SELECTOR_MAVENVERSION; + } + + if ( !SelectorUtils.isJreVersion( invokerProperties.getJreVersion(), actualJavaVersion.toString() ) ) + { + selection |= SELECTOR_JREVERSION; + } + + if ( !SelectorUtils.isOsFamily( invokerProperties.getOsFamily() ) ) + { + selection |= SELECTOR_OSFAMILY; + } + + return selection; + } +} Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SelectorUtils.java Mon May 15 21:10:27 2017 @@ -0,0 +1,283 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; + +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.Os; +import org.codehaus.plexus.util.StringUtils; + +/** + * Provides utility methods for selecting build jobs based on environmental conditions. + * + * @author Benjamin Bentmann + */ +class SelectorUtils +{ + + static void parseList( String list, Collection<String> includes, Collection<String> excludes ) + { + String[] tokens = ( list != null ) ? StringUtils.split( list, "," ) : new String[0]; + + for ( String token1 : tokens ) + { + String token = token1.trim(); + + if ( token.startsWith( "!" ) ) + { + excludes.add( token.substring( 1 ) ); + } + else + { + includes.add( token ); + } + } + } + + static boolean isOsFamily( String osSpec ) + { + List<String> includes = new ArrayList<String>(); + List<String> excludes = new ArrayList<String>(); + parseList( osSpec, includes, excludes ); + + return isOsFamily( includes, true ) && !isOsFamily( excludes, false ); + } + + static boolean isOsFamily( List<String> families, boolean defaultMatch ) + { + if ( families != null && !families.isEmpty() ) + { + for ( String family : families ) + { + if ( Os.isFamily( family ) ) + { + return true; + } + } + + return false; + } + else + { + return defaultMatch; + } + } + + /** + * Retrieves the current Maven version. + * + * @return The current Maven version. + */ + static String getMavenVersion() + { + try + { + // This relies on the fact that MavenProject is the in core classloader + // and that the core classloader is for the maven-core artifact + // and that should have a pom.properties file + // if this ever changes, we will have to revisit this code. + Properties properties = new Properties(); + // CHECKSTYLE_OFF: LineLength + properties.load( MavenProject.class.getClassLoader().getResourceAsStream( "META-INF/maven/org.apache.maven/maven-core/pom.properties" ) ); + // CHECKSTYLE_ON: LineLength + return StringUtils.trim( properties.getProperty( "version" ) ); + } + catch ( Exception e ) + { + return null; + } + } + + static String getMavenVersion( File mavenHome ) + { + File mavenLib = new File( mavenHome, "lib" ); + File[] jarFiles = mavenLib.listFiles( new FilenameFilter() + { + public boolean accept( File dir, String name ) + { + return name.endsWith( ".jar" ); + } + } ); + + for ( File file : jarFiles ) + { + try + { + @SuppressWarnings( "deprecation" ) + URL url = + new URL( "jar:" + file.toURL().toExternalForm() + + "!/META-INF/maven/org.apache.maven/maven-core/pom.properties" ); + + Properties properties = new Properties(); + properties.load( url.openStream() ); + String version = StringUtils.trim( properties.getProperty( "version" ) ); + if ( version != null ) + { + return version; + } + } + catch ( MalformedURLException e ) + { + // ignore + } + catch ( IOException e ) + { + // ignore + } + } + return null; + } + + static boolean isMavenVersion( String mavenSpec ) + { + return isMavenVersion( mavenSpec, getMavenVersion() ); + } + + static boolean isMavenVersion( String mavenSpec, String actualVersion ) + { + List<String> includes = new ArrayList<String>(); + List<String> excludes = new ArrayList<String>(); + parseList( mavenSpec, includes, excludes ); + + List<Integer> mavenVersionList = parseVersion( actualVersion ); + + return isJreVersion( mavenVersionList, includes, true ) && !isJreVersion( mavenVersionList, excludes, false ); + } + + static String getJreVersion() + { + return System.getProperty( "java.version", "" ); + } + + static String getJreVersion( File javaHome ) + { + // @todo detect actual version + return null; + } + + static boolean isJreVersion( String jreSpec ) + { + return isJreVersion( jreSpec, getJreVersion() ); + } + + static boolean isJreVersion( String jreSpec, String actualJreVersion ) + { + List<String> includes = new ArrayList<String>(); + List<String> excludes = new ArrayList<String>(); + parseList( jreSpec, includes, excludes ); + + List<Integer> jreVersion = parseVersion( actualJreVersion ); + + return isJreVersion( jreVersion, includes, true ) && !isJreVersion( jreVersion, excludes, false ); + } + + static boolean isJreVersion( List<Integer> jreVersion, List<String> versionPatterns, boolean defaultMatch ) + { + if ( versionPatterns != null && !versionPatterns.isEmpty() ) + { + for ( String versionPattern : versionPatterns ) + { + if ( isJreVersion( jreVersion, versionPattern ) ) + { + return true; + } + } + + return false; + } + else + { + return defaultMatch; + } + } + + static boolean isJreVersion( List<Integer> jreVersion, String versionPattern ) + { + List<Integer> checkVersion = parseVersion( versionPattern ); + + if ( versionPattern.endsWith( "+" ) ) + { + // 1.5+ <=> [1.5,) + return compareVersions( jreVersion, checkVersion ) >= 0; + } + else if ( versionPattern.endsWith( "-" ) ) + { + // 1.5- <=> (,1.5) + return compareVersions( jreVersion, checkVersion ) < 0; + } + else + { + // 1.5 <=> [1.5,1.6) + return checkVersion.size() <= jreVersion.size() + && checkVersion.equals( jreVersion.subList( 0, checkVersion.size() ) ); + } + } + + static List<Integer> parseVersion( String version ) + { + version = version.replaceAll( "[^0-9]", "." ); + + String[] tokens = StringUtils.split( version, "." ); + + List<Integer> numbers = new ArrayList<Integer>(); + + for ( String token : tokens ) + { + numbers.add( Integer.valueOf( token ) ); + } + + return numbers; + } + + static int compareVersions( List<Integer> version1, List<Integer> version2 ) + { + for ( Iterator<Integer> it1 = version1.iterator(), it2 = version2.iterator();; ) + { + if ( !it1.hasNext() ) + { + return it2.hasNext() ? -1 : 0; + } + if ( !it2.hasNext() ) + { + return it1.hasNext() ? 1 : 0; + } + + Integer num1 = it1.next(); + Integer num2 = it2.next(); + + int rel = num1.compareTo( num2 ); + if ( rel != 0 ) + { + return rel; + } + } + } + +} Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SystemPropertyPrinter.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SystemPropertyPrinter.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SystemPropertyPrinter.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/SystemPropertyPrinter.java Mon May 15 21:10:27 2017 @@ -0,0 +1,42 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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. + */ + +/** + * Print specified system properties to the output stream. Useful to detect certain values from a JVM different to the + * default JAVA_HOME. + * + * @author Robert Scholte + * @since 1.8 + */ +public class SystemPropertyPrinter +{ + /** + * @param args system properties to print + */ + public static void main( String[] args ) + { + for ( String arg : args ) + { + System.out.println( System.getProperty( arg, "" ) ); + } + } + +} Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugins/invoker/VerifyMojo.java Mon May 15 21:10:27 2017 @@ -0,0 +1,140 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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 org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Reader; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.IOException; + +/** + * Checks the results of maven-invoker-plugin based integration tests and fails the build if any tests failed. + * + * @author Olivier Lamy + * @since 1.4 + */ +@Mojo( name = "verify", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true ) +public class VerifyMojo + extends AbstractMojo +{ + + /** + * Flag used to suppress certain invocations. This is useful in tailoring the build using profiles. + * + * @since 1.1 + */ + @Parameter( property = "invoker.skip", defaultValue = "false" ) + private boolean skipInvocation; + + /** + * Base directory where all build reports are read from. + * + * @since 1.4 + */ + @Parameter( property = "invoker.reportsDirectory", defaultValue = "${project.build.directory}/invoker-reports" ) + private File reportsDirectory; + + /** + * A flag controlling whether failures of the sub builds should fail the main build, too. If set to + * <code>true</code>, the main build will proceed even if one or more sub builds failed. + * + * @since 1.3 + */ + @Parameter( property = "maven.test.failure.ignore", defaultValue = "false" ) + private boolean ignoreFailures; + + /** + * Flag used to suppress the summary output notifying of successes and failures. If set to <code>true</code>, the + * only indication of the build's success or failure will be the effect it has on the main build (if it fails, the + * main build should fail as well). + */ + @Parameter( defaultValue = "false" ) + private boolean suppressSummaries; + + /** + * Set this to <code>true</code> to cause a failure if there are no projects to invoke. + * + * @since 1.9 + */ + @Parameter( property = "invoker.failIfNoProjects" ) + private Boolean failIfNoProjects; + + /** + * Invokes Maven on the configured test projects. + * + * @throws org.apache.maven.plugin.MojoExecutionException If the goal encountered severe errors. + * @throws org.apache.maven.plugin.MojoFailureException If any of the Maven builds failed. + */ + public void execute() + throws MojoExecutionException, MojoFailureException + { + if ( skipInvocation ) + { + getLog().info( "Skipping invocation per configuration." + + " If this is incorrect, ensure the skipInvocation parameter is not set to true." ); + return; + } + + File[] reportFiles = ReportUtils.getReportFiles( reportsDirectory ); + if ( reportFiles.length <= 0 ) + { + if ( Boolean.TRUE.equals( failIfNoProjects ) ) + { + throw new MojoFailureException( "No projects to invoke!" ); + } + getLog().info( "No invoker report files found, nothing to check." ); + return; + } + + InvokerSession invokerSession = new InvokerSession(); + for ( File reportFile : reportFiles ) + { + try + { + BuildJobXpp3Reader reader = new BuildJobXpp3Reader(); + invokerSession.addJob( reader.read( ReaderFactory.newXmlReader( reportFile ) ) ); + } + catch ( XmlPullParserException e ) + { + throw new MojoExecutionException( "Failed to parse report file: " + reportFile, e ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Failed to read report file: " + reportFile, e ); + } + } + + if ( !suppressSummaries ) + { + invokerSession.logSummary( getLog(), ignoreFailures ); + } + + invokerSession.handleFailures( getLog(), ignoreFailures ); + } + +} Modified: maven/plugins/trunk/maven-invoker-plugin/src/main/mdo/invocation.mdo URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/mdo/invocation.mdo?rev=1795243&r1=1795242&r2=1795243&view=diff ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/mdo/invocation.mdo (original) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/mdo/invocation.mdo Mon May 15 21:10:27 2017 @@ -32,7 +32,7 @@ under the License. <defaults> <default> <key>package</key> - <value>org.apache.maven.plugin.invoker.model</value> + <value>org.apache.maven.plugins.invoker.model</value> </default> </defaults> <versionDefinition> Added: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/ExtendedMavenProjectStub.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/ExtendedMavenProjectStub.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/ExtendedMavenProjectStub.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/ExtendedMavenProjectStub.java Mon May 15 21:10:27 2017 @@ -0,0 +1,57 @@ +/* + * 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. + */ +package org.apache.maven.plugins.invoker; + +import org.apache.maven.model.Scm; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; + +import java.util.Properties; + +/** + * @author Olivier Lamy + * @since 22 nov. 07 + * @version $Id: ExtendedMavenProjectStub.java 1606207 2014-06-27 20:27:03Z khmarbaise $ + */ +public class ExtendedMavenProjectStub + extends MavenProjectStub +{ + private Scm scm; + + private Properties properties; + + public Scm getScm() + { + return scm; + } + + public void setScm( Scm scm ) + { + this.scm = scm; + } + + public Properties getProperties() + { + return properties; + } + + public void setProperties( Properties properties ) + { + this.properties = properties; + } +} Added: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InterpolationTest.java Mon May 15 21:10:27 2017 @@ -0,0 +1,129 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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.File; +import java.io.Reader; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.apache.maven.model.Scm; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.apache.maven.plugins.invoker.CompositeMap; +import org.apache.maven.plugins.invoker.InvokerMojo; +import org.apache.maven.settings.Settings; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.ReaderFactory; + +/** + * @author Olivier Lamy + * @since 22 nov. 07 + * @version $Id: InterpolationTest.java 1783819 2017-02-21 02:30:23Z schulte $ + */ +public class InterpolationTest + extends AbstractMojoTestCase +{ + + protected MavenProjectStub buildMavenProjectStub() + { + ExtendedMavenProjectStub project = new ExtendedMavenProjectStub(); + project.setVersion( "1.0-SNAPSHOT" ); + project.setArtifactId( "foo" ); + project.setGroupId( "bar" ); + Properties properties = new Properties(); + properties.put( "fooOnProject", "barOnProject" ); + project.setProperties( properties ); + Scm scm = new Scm(); + scm.setConnection( "http://blabla" ); + project.setScm( scm ); + return project; + } + + public void testCompositeMap() + throws Exception + { + + Properties properties = new Properties(); + properties.put( "foo", "bar" ); + properties.put( "version", "2.0-SNAPSHOT" ); + CompositeMap compositeMap = new CompositeMap( buildMavenProjectStub(), (Map) properties, false ); + assertEquals( "1.0-SNAPSHOT", compositeMap.get( "pom.version" ) ); + assertEquals( "bar", compositeMap.get( "foo" ) ); + assertEquals( "bar", compositeMap.get( "pom.groupId" ) ); + assertEquals( "http://blabla", compositeMap.get( "pom.scm.connection" ) ); + assertEquals( "barOnProject", compositeMap.get( "fooOnProject" ) ); + } + + public void testPomInterpolation() + throws Exception + { + Reader reader = null; + File interpolatedPomFile; + try + { + InvokerMojo invokerMojo = new InvokerMojo(); + setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() ); + setVariableValueToObject( invokerMojo, "settings", new Settings() ); + Properties properties = new Properties(); + properties.put( "foo", "bar" ); + properties.put( "version", "2.0-SNAPSHOT" ); + setVariableValueToObject( invokerMojo, "filterProperties", properties ); + String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation"; + + interpolatedPomFile = new File( getBasedir(), "target/interpolated-pom.xml" ); + invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile ); + reader = ReaderFactory.newXmlReader( interpolatedPomFile ); + String content = IOUtil.toString( reader ); + assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 ); + reader.close(); + reader = null; + // recreate it to test delete if exists before creation + invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile ); + reader = ReaderFactory.newXmlReader( interpolatedPomFile ); + content = IOUtil.toString( reader ); + assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 ); + reader.close(); + reader = null; + } + finally + { + IOUtil.close( reader ); + } + } + + public void testProfilesWithNoFile() + throws Exception + { + + InvokerMojo invokerMojo = new InvokerMojo(); + setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( "zloug" ) ); + setVariableValueToObject( invokerMojo, "settings", new Settings() ); + String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar + + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file"; + List<String> profiles = invokerMojo.getProfiles( new File( dirPath ) ); + assertTrue( profiles.contains( "zloug" ) ); + assertEquals( 1, profiles.size() ); + + } +} Added: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java Mon May 15 21:10:27 2017 @@ -0,0 +1,87 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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.File; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugins.invoker.model.BuildJob; + +/** + * @author Olivier Lamy + * @since 18 nov. 07 + * @version $Id: InvokerMojoTest.java 1731726 2016-02-22 19:34:37Z khmarbaise $ + */ +public class InvokerMojoTest + extends AbstractMojoTestCase +{ + + public void testSingleInvokerTest() + throws Exception + { + InvokerMojo invokerMojo = new InvokerMojo(); + String dirPath = getBasedir() + "/src/test/resources/unit"; + List<String> goals = invokerMojo.getGoals( new File( dirPath ) ); + assertEquals( 1, goals.size() ); + setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) ); + setVariableValueToObject( invokerMojo, "invokerTest", "*dummy*" ); + BuildJob[] poms = invokerMojo.getBuildJobs(); + assertEquals( 1, poms.length ); + } + + public void testMultiInvokerTest() + throws Exception + { + InvokerMojo invokerMojo = new InvokerMojo(); + String dirPath = getBasedir() + "/src/test/resources/unit"; + List<String> goals = invokerMojo.getGoals( new File( dirPath ) ); + assertEquals( 1, goals.size() ); + setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) ); + setVariableValueToObject( invokerMojo, "invokerTest", "*dummy*,*terpolatio*" ); + BuildJob[] poms = invokerMojo.getBuildJobs(); + assertEquals( 2, poms.length ); + } + + public void testFullPatternInvokerTest() + throws Exception + { + InvokerMojo invokerMojo = new InvokerMojo(); + String dirPath = getBasedir() + "/src/test/resources/unit"; + List<String> goals = invokerMojo.getGoals( new File( dirPath ) ); + assertEquals( 1, goals.size() ); + setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) ); + setVariableValueToObject( invokerMojo, "invokerTest", "*" ); + BuildJob[] poms = invokerMojo.getBuildJobs(); + assertEquals( 4, poms.length ); + } + + public void testAlreadyCloned() + throws Exception + { + assertFalse( AbstractInvokerMojo.alreadyCloned( "dir", Collections.<String>emptyList() ) ); + assertTrue( AbstractInvokerMojo.alreadyCloned( "dir", Collections.singletonList( "dir" ) ) ); + assertTrue( AbstractInvokerMojo.alreadyCloned( "dir" + File.separator + "sub", + Collections.singletonList( "dir" ) ) ); + assertFalse( AbstractInvokerMojo.alreadyCloned( "dirs", Collections.singletonList( "dir" ) ) ); + } + +} Added: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/InvokerPropertiesTest.java Mon May 15 21:10:27 2017 @@ -0,0 +1,295 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.Properties; + +import org.apache.maven.plugins.invoker.InvokerProperties; +import org.apache.maven.shared.invoker.DefaultInvocationRequest; +import org.apache.maven.shared.invoker.InvocationRequest; + +import junit.framework.TestCase; + +/** + * Tests the invoker properties facade. + * + * @author Benjamin Bentmann + * @version $Id: InvokerPropertiesTest.java 1779250 2017-01-17 20:20:02Z rfscholte $ + */ +public class InvokerPropertiesTest + extends TestCase +{ + + public void testConstructorNullSafe() + throws Exception + { + InvokerProperties facade = new InvokerProperties( null ); + assertNotNull( facade.getProperties() ); + } + + public void testGetInvokerProperty() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + assertNull( facade.get( "undefined-key", 0 ) ); + + props.setProperty( "key", "value" ); + assertEquals( "value", facade.get( "key", 1 ) ); + + props.setProperty( "key.1", "another-value" ); + assertEquals( "another-value", facade.get( "key", 1 ) ); + assertEquals( "value", facade.get( "key", 2 ) ); + } + + public void testGetJobName() + throws Exception + { + Properties props = new Properties(); + final String jobName = "Build Job name"; + props.put( "invoker.name", jobName ); + InvokerProperties facade = new InvokerProperties( props ); + + assertEquals( jobName, facade.getJobName() ); + } + + public void testIsExpectedResult() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + assertTrue( facade.isExpectedResult( 0, 0 ) ); + assertFalse( facade.isExpectedResult( 1, 0 ) ); + + props.setProperty( "invoker.buildResult", "success" ); + assertTrue( facade.isExpectedResult( 0, 0 ) ); + assertFalse( facade.isExpectedResult( 1, 0 ) ); + + props.setProperty( "invoker.buildResult", "failure" ); + assertFalse( facade.isExpectedResult( 0, 0 ) ); + assertTrue( facade.isExpectedResult( 1, 0 ) ); + } + + public void testConfigureRequestGoals() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setGoals( Collections.singletonList( "test" ) ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "test" ), request.getGoals() ); + + props.setProperty( "invoker.goals", "verify" ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "verify" ), request.getGoals() ); + + props.setProperty( "invoker.goals", " " ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[0] ), request.getGoals() ); + + props.setProperty( "invoker.goals", " clean , test verify " ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[] { "clean", "test", "verify" } ), request.getGoals() ); + + props.setProperty( "invoker.goals", "" ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[0] ), request.getGoals() ); + } + + public void testConfigureRequestProfiles() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setProfiles( Collections.singletonList( "test" ) ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "test" ), request.getProfiles() ); + + props.setProperty( "invoker.profiles", "verify" ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "verify" ), request.getProfiles() ); + + props.setProperty( "invoker.profiles", " " ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[0] ), request.getProfiles() ); + + props.setProperty( "invoker.profiles", " clean , test verify ," ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[] { "clean", "test", "verify" } ), request.getProfiles() ); + + props.setProperty( "invoker.profiles", "" ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[0] ), request.getProfiles() ); + } + + public void testConfigureRequestProject() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + File tempPom = File.createTempFile( "maven-invoker-plugin-test", ".pom" ); + File tempDir = tempPom.getParentFile(); + + request.setBaseDirectory( tempDir ); + facade.configureInvocation( request, 0 ); + assertEquals( tempDir, request.getBaseDirectory() ); + assertEquals( null, request.getPomFile() ); + + props.setProperty( "invoker.project", tempPom.getName() ); + request.setBaseDirectory( tempDir ); + facade.configureInvocation( request, 0 ); + assertEquals( tempDir, request.getBaseDirectory() ); + assertEquals( tempPom, request.getPomFile() ); + + props.setProperty( "invoker.project", "" ); + request.setBaseDirectory( tempDir ); + facade.configureInvocation( request, 0 ); + assertEquals( tempDir, request.getBaseDirectory() ); + assertEquals( null, request.getPomFile() ); + + tempPom.delete(); + } + + public void testConfigureRequestMavenOpts() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setMavenOpts( "default" ); + facade.configureInvocation( request, 0 ); + assertEquals( "default", request.getMavenOpts() ); + + props.setProperty( "invoker.mavenOpts", "-Xmx512m" ); + facade.configureInvocation( request, 0 ); + assertEquals( "-Xmx512m", request.getMavenOpts() ); + } + + public void testConfigureRequestFailureBehavior() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setReactorFailureBehavior( InvocationRequest.ReactorFailureBehavior.FailAtEnd ); + facade.configureInvocation( request, 0 ); + assertEquals( InvocationRequest.ReactorFailureBehavior.FailAtEnd, request.getReactorFailureBehavior() ); + + props.setProperty( "invoker.failureBehavior", InvocationRequest.ReactorFailureBehavior.FailNever.getLongOption() ); + facade.configureInvocation( request, 0 ); + assertEquals( "fail-never", request.getReactorFailureBehavior().getLongOption() ); + } + + public void testConfigureRequestRecursion() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setRecursive( true ); + facade.configureInvocation( request, 0 ); + assertTrue( request.isRecursive() ); + request.setRecursive( false ); + facade.configureInvocation( request, 0 ); + assertFalse( request.isRecursive() ); + + props.setProperty( "invoker.nonRecursive", "true" ); + facade.configureInvocation( request, 0 ); + assertFalse( request.isRecursive() ); + + props.setProperty( "invoker.nonRecursive", "false" ); + facade.configureInvocation( request, 0 ); + assertTrue( request.isRecursive() ); + } + + public void testConfigureRequestOffline() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setOffline( true ); + facade.configureInvocation( request, 0 ); + assertTrue( request.isOffline() ); + request.setOffline( false ); + facade.configureInvocation( request, 0 ); + assertFalse( request.isOffline() ); + + props.setProperty( "invoker.offline", "true" ); + facade.configureInvocation( request, 0 ); + assertTrue( request.isOffline() ); + + props.setProperty( "invoker.offline", "false" ); + facade.configureInvocation( request, 0 ); + assertFalse( request.isOffline() ); + } + + public void testIsInvocationDefined() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + assertFalse( facade.isInvocationDefined( 1 ) ); + + props.setProperty( "invoker.goals", "install" ); + assertFalse( facade.isInvocationDefined( 1 ) ); + + props.setProperty( "invoker.goals.2", "install" ); + assertFalse( facade.isInvocationDefined( 1 ) ); + assertTrue( facade.isInvocationDefined( 2 ) ); + assertFalse( facade.isInvocationDefined( 3 ) ); + } + + public void testIsSelectedDefined() + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + assertFalse( facade.isSelectorDefined( 1 ) ); + + props.setProperty( "invoker.java.version", "1.6+" ); + props.setProperty( "invoker.maven.version", "3.0+" ); + props.setProperty( "invoker.os.family", "windows" ); + assertFalse( facade.isSelectorDefined( 1 ) ); + + props.setProperty( "selector.2.java.version", "1.6+" ); + props.setProperty( "selector.3.maven.version", "3.0+" ); + props.setProperty( "selector.4.os.family", "windows" ); + assertFalse( facade.isSelectorDefined( 1 ) ); + assertTrue( facade.isSelectorDefined( 2 ) ); + assertTrue( facade.isSelectorDefined( 3 ) ); + assertTrue( facade.isSelectorDefined( 4 ) ); + assertFalse( facade.isSelectorDefined( 5 ) ); + } +} Added: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorTest.java Mon May 15 21:10:27 2017 @@ -0,0 +1,80 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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 static org.junit.Assert.assertEquals; +import java.util.Properties; + +import org.apache.maven.plugins.invoker.InvokerProperties; +import org.apache.maven.plugins.invoker.Selector; +import org.junit.Test; + +public class SelectorTest +{ + @Test + public void testGlobalMatch() + { + Selector selector = new Selector( "3.2.5", "1.7" ); + + Properties props = new Properties(); + props.setProperty( "invoker.maven.version", "3.0+" ); + InvokerProperties invokerProperties = new InvokerProperties( props ); + assertEquals( 0, selector.getSelection( invokerProperties ) ); + } + + @Test + public void testSelectorMatch() + { + Selector selector = new Selector( "3.2.5", "1.7" ); + + Properties props = new Properties(); + props.setProperty( "selector.1.maven.version", "3.0+" ); + InvokerProperties invokerProperties = new InvokerProperties( props ); + assertEquals( 0, selector.getSelection( invokerProperties ) ); + + props.setProperty( "selector.1.maven.version", "3.3.1+" ); + assertEquals( Selector.SELECTOR_MULTI, selector.getSelection( invokerProperties ) ); + } + + @Test + public void testSelectorWithGlobalMatch() + { + Selector selector = new Selector( "3.2.5", "1.7" ); + + Properties props = new Properties(); + // invoker.maven.version is used by all selectors + props.setProperty( "invoker.maven.version", "3.0+" ); + props.setProperty( "selector.1.java.version", "1.4+" ); + props.setProperty( "selector.2.os.family", "myos" ); + InvokerProperties invokerProperties = new InvokerProperties( props ); + assertEquals( 0, selector.getSelection( invokerProperties ) ); + + props.setProperty( "invoker.maven.version", "3.3.1+" ); + assertEquals( Selector.SELECTOR_MULTI, selector.getSelection( invokerProperties ) ); + + props.setProperty( "invoker.maven.version", "3.0+" ); + props.setProperty( "selector.1.maven.version", "3.3.1+" ); + assertEquals( Selector.SELECTOR_MULTI, selector.getSelection( invokerProperties ) ); + + props.setProperty( "selector.2.os.family", "!myos" ); + assertEquals( 0, selector.getSelection( invokerProperties ) ); + } + +} Added: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java?rev=1795243&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugins/invoker/SelectorUtilsTest.java Mon May 15 21:10:27 2017 @@ -0,0 +1,92 @@ +package org.apache.maven.plugins.invoker; + +/* + * 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.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.maven.plugins.invoker.SelectorUtils; + +import junit.framework.TestCase; + +/** + * Tests {@link SelectorUtils}. + * + * @author Benjamin Bentmann + */ +public class SelectorUtilsTest + extends TestCase +{ + + public void testParseList() + { + List<String> includes = new ArrayList<String>(); + List<String> excludes = new ArrayList<String>(); + + SelectorUtils.parseList( null, includes, excludes ); + + SelectorUtils.parseList( " 1.5, !1.4, 1.6+ ", includes, excludes ); + assertEquals( Arrays.asList( "1.5", "1.6+" ), includes ); + assertEquals( Arrays.asList( "1.4" ), excludes ); + } + + public void testParseVersion() + { + assertEquals( Arrays.asList( 1, 6, 0, 12 ), SelectorUtils.parseVersion( "1.6.0_12" ) ); + + assertEquals( Arrays.asList( 1, 6, 0, 12 ), SelectorUtils.parseVersion( "1.6.0_12+" ) ); + assertEquals( Arrays.asList( 1, 6, 0, 12 ), SelectorUtils.parseVersion( "1.6.0_12-" ) ); + } + + public void testCompareVersions() + { + assertTrue( SelectorUtils.compareVersions( Arrays.asList( 1, 6 ), Arrays.asList( 1, 6 ) ) == 0 ); + + assertTrue( SelectorUtils.compareVersions( Arrays.asList( 1, 5 ), Arrays.asList( 1, 6 ) ) < 0 ); + assertTrue( SelectorUtils.compareVersions( Arrays.asList( 1, 6 ), Arrays.asList( 1, 5 ) ) > 0 ); + + assertTrue( SelectorUtils.compareVersions( Arrays.asList( 1 ), Arrays.asList( 1, 6 ) ) < 0 ); + assertTrue( SelectorUtils.compareVersions( Arrays.asList( 1, 6 ), Arrays.asList( 1 ) ) > 0 ); + } + + public void testIsMatchingJre() + { + + assertFalse( SelectorUtils.isJreVersion( Arrays.asList( 1, 4, 2, 8 ), "1.5" ) ); + assertTrue( SelectorUtils.isJreVersion( Arrays.asList( 1, 5 ), "1.5" ) ); + assertTrue( SelectorUtils.isJreVersion( Arrays.asList( 1, 5, 9 ), "1.5" ) ); + assertFalse( SelectorUtils.isJreVersion( Arrays.asList( 1, 6 ), "1.5" ) ); + + assertFalse( SelectorUtils.isJreVersion( Arrays.asList( 1, 4, 2, 8 ), "1.5+" ) ); + assertTrue( SelectorUtils.isJreVersion( Arrays.asList( 1, 5 ), "1.5+" ) ); + assertTrue( SelectorUtils.isJreVersion( Arrays.asList( 1, 5, 9 ), "1.5+" ) ); + assertTrue( SelectorUtils.isJreVersion( Arrays.asList( 1, 6 ), "1.5+" ) ); + + assertTrue( SelectorUtils.isJreVersion( Arrays.asList( 1, 4, 2, 8 ), "1.5-" ) ); + assertFalse( SelectorUtils.isJreVersion( Arrays.asList( 1, 5 ), "1.5-" ) ); + assertFalse( SelectorUtils.isJreVersion( Arrays.asList( 1, 5, 9 ), "1.5-" ) ); + assertFalse( SelectorUtils.isJreVersion( Arrays.asList( 1, 6 ), "1.5-" ) ); + + assertTrue( SelectorUtils.isJreVersion( (String) null, "1.5" ) ); + assertTrue( SelectorUtils.isJreVersion( "", "1.5" ) ); + } + +}