SOLR-11606: Disable tests automatically if Mockito does not work with Java runtime (Java 10)

This commit is contained in:
Uwe Schindler 2017-11-04 19:37:28 +01:00
parent d501ecd2d1
commit 3ab1a07662
22 changed files with 103 additions and 14 deletions

View File

@ -186,20 +186,26 @@
}
def checkLicenseHeaderPrecedes = { f, description, contentPattern, commentPattern, text, ratDocument ->
def contentMatcher = contentPattern.matcher(text);
if (contentMatcher.find()) {
def contentStartPos = contentMatcher.start();
def commentMatcher = commentPattern.matcher(text);
while (commentMatcher.find()) {
if (isLicense(commentMatcher, ratDocument)) {
if (commentMatcher.start() < contentStartPos) {
break; // This file is all good, so break loop: license header precedes 'description' definition
} else {
reportViolation(f, description+' declaration precedes license header');
}
def contentMatcher = contentPattern.matcher(text);
if (contentMatcher.find()) {
def contentStartPos = contentMatcher.start();
def commentMatcher = commentPattern.matcher(text);
while (commentMatcher.find()) {
if (isLicense(commentMatcher, ratDocument)) {
if (commentMatcher.start() < contentStartPos) {
break; // This file is all good, so break loop: license header precedes 'description' definition
} else {
reportViolation(f, description+' declaration precedes license header');
}
}
}
}
}
def checkMockitoAssume = { f, text ->
if (text.contains("mockito") && !text.contains("assumeWorkingMockito()")) {
reportViolation(f, 'File uses Mockito but has no assumeWorkingMockito() call');
}
}
def checkForUnescapedSymbolSubstitutions = { f, text ->
@ -265,18 +271,21 @@
ratDocument.getMetaData().value(MetaData.RAT_URL_LICENSE_FAMILY_NAME)));
}
}
if (f.toString().endsWith('.java')) {
if (f.name.endsWith('.java')) {
if (text.contains('org.slf4j.LoggerFactory')) {
if (!validLoggerPattern.matcher(text).find()) {
reportViolation(f, 'invalid logging pattern [not private static final, uses static class name]');
}
}
checkLicenseHeaderPrecedes(f, 'package', packagePattern, javaCommentPattern, text, ratDocument);
if (f.name.contains("Test")) {
checkMockitoAssume(f, text);
}
}
if (f.toString().endsWith('.xml') || f.toString().endsWith('.xml.template')) {
if (f.name.endsWith('.xml') || f.name.endsWith('.xml.template')) {
checkLicenseHeaderPrecedes(f, '<tag>', xmlTagPattern, xmlCommentPattern, text, ratDocument);
}
if (f.toString().endsWith('.adoc')) {
if (f.name.endsWith('.adoc')) {
checkForUnescapedSymbolSubstitutions(f, text);
}
};

View File

@ -139,6 +139,9 @@ Other Changes
* SOLR-11603: Remove unused (public) LTRScoringModel.hasParams() method. (Christine Poerschke)
* SOLR-11606: Disable tests automatically if Mockito does not work with Java runtime (Java 10).
(Uwe Schindler)
================== 7.1.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

View File

@ -40,6 +40,7 @@ import org.apache.solr.handler.dataimport.JdbcDataSource.ResultSetIterator;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
@ -68,6 +69,11 @@ public class TestJdbcDataSource extends AbstractDataImportHandlerTestCase {
String sysProp = System.getProperty("java.naming.factory.initial");
@BeforeClass
public static void beforeClass() {
assumeWorkingMockito();
}
@Override
@Before
public void setUp() throws Exception {

View File

@ -53,6 +53,7 @@ public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
assumeWorkingMockito();
initCore("solrconfig.xml","schema-reversed.xml");
}

View File

@ -62,6 +62,8 @@ public class AssignTest extends SolrTestCaseJ4 {
@Test
public void testAssignNode() throws Exception {
assumeWorkingMockito();
SolrZkClient zkClient = mock(SolrZkClient.class);
Map<String, byte[]> zkClientData = new HashMap<>();
when(zkClient.setData(anyString(), any(), anyInt(), anyBoolean())).then(invocation -> {

View File

@ -122,6 +122,8 @@ public class OverseerCollectionConfigSetProcessorTest extends SolrTestCaseJ4 {
@BeforeClass
public static void setUpOnce() throws Exception {
assumeWorkingMockito();
workQueueMock = mock(OverseerTaskQueue.class);
runningMapMock = mock(DistributedMap.class);
completedMapMock = mock(DistributedMap.class);

View File

@ -238,6 +238,7 @@ public class OverseerTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
assumeWorkingMockito();
initCore();
}

View File

@ -27,10 +27,17 @@ import org.apache.solr.common.cloud.ImplicitDocRouter;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkNodeProps;
import org.apache.solr.common.util.Utils;
import org.junit.BeforeClass;
import static org.mockito.Mockito.*;
public class TestClusterStateMutator extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() {
assumeWorkingMockito();
}
public void testCreateCollection() throws Exception {
ClusterState clusterState = new ClusterState(-1, Collections.<String>emptySet(), Collections.<String, DocCollection>emptyMap());
DistribStateManager mockStateManager = mock(DistribStateManager.class);

View File

@ -23,6 +23,7 @@ import java.util.Map;
import com.google.common.collect.Sets;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.cloud.rule.ImplicitSnitch;
@ -134,6 +135,8 @@ public class ImplicitSnitchTest extends LuceneTestCase {
@Test
public void testGetTags_withAllHostNameRequestedTags_returns_all_Tags() throws Exception {
SolrTestCaseJ4.assumeWorkingMockito();
String node = "serv01.dc01.london.uk.apache.org:8983_solr";
SnitchContext context = new ServerSnitchContext(null, node, new HashMap<>(),null);
@ -153,6 +156,8 @@ public class ImplicitSnitchTest extends LuceneTestCase {
@Test
public void testGetTags_withHostNameRequestedTag_ip3_returns_1_tag() throws Exception {
SolrTestCaseJ4.assumeWorkingMockito();
String node = "serv01.dc01.london.uk.apache.org:8983_solr";
SnitchContext context = new ServerSnitchContext(null, node, new HashMap<>(),null);

View File

@ -25,8 +25,10 @@ import java.nio.charset.Charset;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.io.IOUtils;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.mockito.Mockito.*;
@ -54,6 +56,11 @@ public class BlobRepositoryMockingTest {
boolean blobFetched = false;
String blobKey = "";
@BeforeClass
public static void beforeClass() {
SolrTestCaseJ4.assumeWorkingMockito();
}
@Before
public void setUp() throws IllegalAccessException, NoSuchFieldException {
blobFetched = false;

View File

@ -101,6 +101,8 @@ public class CoreSorterTest extends SolrTestCaseJ4 {
}
private CoreContainer getMockContainer() {
assumeWorkingMockito();
CoreContainer mockCC = mock(CoreContainer.class);
ZkController mockZKC = mock(ZkController.class);
ClusterState mockClusterState = mock(ClusterState.class);

View File

@ -70,6 +70,8 @@ public class TestCoreAdminApis extends SolrTestCaseJ4 {
}
public static CoreContainer getCoreContainerMock(final Map<String, Object[]> in,Map<String,Object> out ) {
assumeWorkingMockito();
CoreContainer mockCC = mock(CoreContainer.class);
when(mockCC.create(any(String.class), any(Path.class) , any(Map.class), anyBoolean())).thenAnswer(invocationOnMock -> {
in.put("create", invocationOnMock.getArguments());

View File

@ -45,6 +45,8 @@ import static org.mockito.Mockito.*;
public class SolrGangliaReporterTest extends SolrTestCaseJ4 {
@Test
public void testReporter() throws Exception {
assumeWorkingMockito();
Path home = Paths.get(TEST_HOME());
// define these properties, they are used in solrconfig.xml
System.setProperty("solr.test.sys.prop1", "propone");

View File

@ -17,6 +17,7 @@
package org.apache.solr.schema;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.schema.ZkIndexSchemaReader.SchemaWatcher;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher.Event.EventType;
@ -35,6 +36,8 @@ public class SchemaWatcherTest {
@Before
public void setUp() throws Exception {
SolrTestCaseJ4.assumeWorkingMockito();
mockSchemaReader = mock(ZkIndexSchemaReader.class);
schemaWatcher = new SchemaWatcher(mockSchemaReader);
}

View File

@ -135,6 +135,7 @@ public class TestManagedSchemaThreadSafety extends SolrTestCaseJ4 {
}
private ZkController createZkController(SolrZkClient client) throws KeeperException, InterruptedException {
assumeWorkingMockito();
CoreContainer mockAlwaysUpCoreContainer = mock(CoreContainer.class,
Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));

View File

@ -70,6 +70,8 @@ public class TestPKIAuthenticationPlugin extends SolrTestCaseJ4 {
}
public void test() throws Exception {
assumeWorkingMockito();
AtomicReference<Principal> principal = new AtomicReference<>();
String nodeName = "node_x_233";

View File

@ -62,6 +62,7 @@ public class SolrRequestParserTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
assumeWorkingMockito();
initCore("solrconfig.xml", "schema.xml");
parser = new SolrRequestParsers( h.getCore().getSolrConfig() );
}

View File

@ -104,6 +104,8 @@ public class ClassificationUpdateProcessorFactoryTest extends SolrTestCaseJ4 {
@Test
public void init_unsupportedFilterQuery_shouldThrowExceptionWithDetailedMessage() {
assumeWorkingMockito();
UpdateRequestProcessor mockProcessor = mock(UpdateRequestProcessor.class);
SolrQueryRequest mockRequest = mock(SolrQueryRequest.class);
SolrQueryResponse mockResponse = mock(SolrQueryResponse.class);

View File

@ -61,6 +61,7 @@ public class ClassificationUpdateProcessorTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
assumeWorkingMockito();
System.setProperty("enable.update.log", "false");
initCore("solrconfig-classification.xml", "schema-classification.xml");
}

View File

@ -26,6 +26,7 @@ import java.io.IOException;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
@ -34,6 +35,7 @@ import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.AddUpdateCommand;
import org.apache.solr.update.processor.SkipExistingDocumentsProcessorFactory.SkipExistingDocumentsUpdateProcessor;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
@ -42,6 +44,11 @@ public class SkipExistingDocumentsProcessorFactoryTest {
private BytesRef docId = new BytesRef();
private SolrQueryRequest defaultRequest = new LocalSolrQueryRequest(null, new NamedList());
@BeforeClass
public static void beforeClass() {
SolrTestCaseJ4.assumeWorkingMockito();
}
// Tests for logic in the factory
@Test(expected=SolrException.class)

View File

@ -36,12 +36,19 @@ import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.util.NamedList;
import org.junit.BeforeClass;
import static org.mockito.Mockito.*;
import static java.nio.charset.StandardCharsets.UTF_8;
public class CloudSolrClientCacheTest extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() {
assumeWorkingMockito();
}
public void testCaching() throws Exception {
String collName = "gettingstarted";
Set<String> livenodes = new HashSet<>();

View File

@ -31,6 +31,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.ServerSocket;
@ -60,9 +61,11 @@ import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import com.carrotsearch.randomizedtesting.RandomizedContext;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.TraceFormatting;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
import org.apache.commons.io.FileUtils;
import org.apache.http.client.HttpClient;
import org.apache.lucene.analysis.MockAnalyzer;
@ -328,6 +331,19 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
StartupLoggingUtils.changeLogLevel(initialRootLogLevel);
}
/** Assumes that Mockito/Bytebuddy is available and can be used to mock classes (e.g., fails if Java version is too new). */
public static void assumeWorkingMockito() {
// we use reflection here, because we do not have ByteBuddy/Mockito in all modules and the test framework!
try {
Class.forName("net.bytebuddy.ClassFileVersion").getMethod("ofThisVm").invoke(null);
} catch (InvocationTargetException e) {
RandomizedTest.assumeNoException("SOLR-11606: ByteBuddy used by Mockito is not working with this JVM version.",
e.getTargetException());
} catch (ReflectiveOperationException e) {
fail("ByteBuddy and Mockito are not available on classpath: " + e.toString());
}
}
/**
* @return null if ok else error message
*/