Skip to content

Commit

Permalink
[SUREFIRE-1842] - NPE at end of successful test run
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelboyles authored and Tibor17 committed Sep 22, 2020
1 parent 5b09e94 commit 8a7b2e6
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.surefire.api.util.RunOrderCalculator;
import org.apache.maven.surefire.api.util.ScanResult;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -68,6 +69,7 @@ public interface ProviderParameters
*
* @return A RunOrderCalculator
*/
@Nullable
RunOrderCalculator getRunOrderCalculator();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.List;
import java.util.Set;

import org.apache.maven.surefire.api.testset.TestSetFailedException;

import static java.lang.Math.max;

/**
Expand Down Expand Up @@ -55,7 +53,6 @@ public TestsToRun( Set<Class<?>> locatedClasses )
}

public static TestsToRun fromClass( Class<?> clazz )
throws TestSetFailedException
{
return new TestsToRun( Collections.<Class<?>>singleton( clazz ) );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.apache.maven.surefire.api.booter;

/*
* 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.surefire.api.testset.DirectoryScannerParameters;
import org.junit.Test;

import java.io.File;
import java.util.Collections;

import static org.junit.Assert.assertNull;

/**
* @author Michael Boyles
*/
public class BaseProviderFactoryTest
{
@Test
public void runOrderCalculatorIsNullIfNotSet()
{
BaseProviderFactory factory = new BaseProviderFactory( true );
factory.setDirectoryScannerParameters ( getDirectoryScannerParameters() );

assertNull( factory.getRunOrderCalculator() );
}

private DirectoryScannerParameters getDirectoryScannerParameters()
{
return new DirectoryScannerParameters(
new File( "/fake/file" ),
Collections.<String>emptyList(),
Collections.<String>emptyList(),
Collections.<String>emptyList(),
false,
"ALPHABETICAL"
);
}
}
5 changes: 5 additions & 0 deletions surefire-providers/surefire-junit47/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<version>1.0-1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private Filter createJUnit48Filter()
private TestsToRun scanClassPath()
{
TestsToRun scanned = scanResult.applyFilter( scannerFilter, testClassLoader );
return runOrderCalculator.orderTestClasses( scanned );
return runOrderCalculator == null
? scanned : runOrderCalculator.orderTestClasses( scanned );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.apache.maven.surefire.junitcore;

/*
* 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.surefire.api.provider.ProviderParameters;
import org.apache.maven.surefire.api.util.ScanResult;
import org.apache.maven.surefire.api.util.ScannerFilter;
import org.apache.maven.surefire.api.util.TestsToRun;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Iterator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Michael Boyles
*/
@RunWith( MockitoJUnitRunner.class )
public class JUnitCoreProviderTest
{
@Mock
private ProviderParameters providerParameters;
@Mock
private ScanResult scanResult;

@Test
public void doNotSortTestsIfNoRunOrderProvider()
{
Mockito.when( providerParameters.getScanResult() ).thenReturn( scanResult );
Mockito.when( scanResult.applyFilter( Mockito.any( ScannerFilter.class ), Mockito.any( ClassLoader.class ) ) )
.thenReturn( TestsToRun.fromClass( String.class ) );

JUnitCoreProvider provider = new JUnitCoreProvider( providerParameters );
Iterator<Class<?>> suites = provider.getSuites().iterator();

assertTrue( suites.hasNext() );
assertEquals( String.class, suites.next() );
assertFalse( suites.hasNext() );
}
}

0 comments on commit 8a7b2e6

Please sign in to comment.