/**
 * 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.jena.query.text;

import org.apache.jena.atlas.lib.StrUtils;
import org.apache.jena.ext.com.google.common.collect.Sets;
import org.apache.jena.query.ReadWrite;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.vocabulary.RDFS;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestDatasetConcurrencyWithConfigurableAnalyzer extends AbstractTestDatasetWithAnalyzer {
    @Override
    @Before
    public void before () {
        init(StrUtils.strjoinNL(
                "text:ConfigurableAnalyzer ;",
                "text:tokenizer text:WhitespaceTokenizer ;",
                "text:filters (text:ASCIIFoldingFilter text:LowerCaseFilter)"
        ));
        initDataset();
    }

    private void initDataset() {
        Model model = dataset.getDefaultModel();
        dataset.begin(ReadWrite.WRITE);
        for (int i = 0; i < 1000; i++) {
            String token = "Foo" + i;
            model.createResource(RESOURCE_BASE + token).addProperty(RDFS.label, token);
        }
        dataset.commit();
    }

    private boolean testOneQuery(int probe) {
        final String testName = "testConfigurableAnalyzerIsConcurrencySafe" + probe;
        String query = QUERY_PROLOG + "select ?s WHERE {?s text:query (rdfs:label 'foo" + probe + "' 10).}";
        try {
            doTestQuery(dataset, testName, query, Sets.newHashSet(RESOURCE_BASE + "Foo" + probe), 1);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Test
    public void testConfigurableAnalyzerIsConcurrencySafe () {
        final int parallelism = 8;
        ExecutorService executorService = Executors.newFixedThreadPool(parallelism);
        try {
            for (int i = 0; i < 20; i++) {
                Future<Boolean>[] results = (Future<Boolean>[]) new Future<?>[parallelism];
                for (int j = 0; j < parallelism; j++) {
                    final int probe = i;
                    results[j] = executorService.submit(() -> testOneQuery(probe));
                }
                for (int j = 0; j < parallelism; j++) {
                    Assert.assertTrue("Probe " + i + " failed", results[j].get());
                }
            }
        } catch (InterruptedException e) {
            // exit silently on interrupt
        } catch (ExecutionException e) {
            Assert.assertTrue("Concurrency exception: " + e.getMessage(), false);
        }
    }
}

@@ -834,24 +834,244 @@
 uery.text;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0A%0A
 import org.a
@@ -1358,177 +1358,8 @@
 t;%0A%0A
-import java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0A%0A
 publ
@@ -2821,16 +2821,21 @@
         
+List%3C
 Future%3CB
@@ -2841,18 +2841,17 @@
 Boolean%3E
-%5B%5D
+%3E
  results
@@ -2857,42 +2857,24 @@
 s = 
-(Future%3CBoolean%3E%5B%5D) new Future%3C?%3E%5B
+new ArrayList%3C%3E(
 para
@@ -2880,17 +2880,17 @@
 allelism
-%5D
+)
 ;%0A      
@@ -3011,14 +3011,13 @@
 ults
-%5Bj%5D = 
+.add(
 exec
@@ -3061,16 +3061,18 @@
 (probe))
+);
 ;%0A      
@@ -3214,11 +3214,15 @@
 ults
-%5Bj%5D
+.get(j)
 .get

Fix warning