/*
 * 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.sis.storage;

import java.io.StringReader;
import org.apache.sis.internal.storage.xml.StoreTest;
import org.apache.sis.test.DependsOn;
import org.apache.sis.test.TestCase;
import org.junit.Test;

import static org.junit.Assert.*;


/**
 * Tests {@link DataStores}.
 *
 * @author  Martin Desruisseaux (Geomatys)
 * @version 0.7
 * @since   0.4
 * @module
 */
@DependsOn(StoreTest.class)
public final strictfp class DataStoresTest extends TestCase {
    /**
     * Tests {@link DataStores#probeContentType(Object)}.
     *
     * @throws DataStoreException Should never happen.
     */
    @Test
    public void testProbeContentType() throws DataStoreException {
        final String type = DataStores.probeContentType(new StringReader(StoreTest.XML));
        assertEquals("application/vnd.iso.19139+xml", type);
    }

    /**
     * Tests {@link DataStores#open(Object)}.
     *
     * @throws DataStoreException Should never happen.
     */
    @Test
    public void testOpen() throws DataStoreException {
        final DataStore store = DataStores.open(new StringReader(StoreTest.XML));
        assertFalse(store.getMetadata().getContacts().isEmpty());
    }
}

@@ -857,16 +857,354 @@
 Reader;%0A
+import java.util.ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport java.util.concurrent.Callable;%0Aimport java.util.concurrent.CyclicBarrier;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.TimeUnit;%0A
 import o
@@ -2314,10 +2314,1773 @@
 ;%0A    %7D%0A
+%0A%0A    @Test%0A    public void datastore_registry_must_be_thread_safe() throws Exception %7B%0A        final ExecutorService exec = Executors.newFixedThreadPool(6);%0A        try %7B%0A            List%3CSet%3E allResults = new ArrayList%3C%3E();%0A            for (int i = 4; i %3C= 6; i++) allResults.addAll(collectProvidersConcurrently(i, exec));%0A%0A            for (int i = 1; i %3C allResults.size() - 1; i++) %7B%0A                assertEquals(%0A                        %22Index %22 + i,%0A                        allResults.get(i - 1),%0A                        allResults.get(i)%0A                );%0A            %7D%0A            exec.shutdown();%0A            exec.awaitTermination(1, TimeUnit.SECONDS);%0A        %7D finally %7B%0A            exec.shutdownNow();%0A        %7D%0A    %7D%0A%0A    private List%3CSet%3CClass%3E%3E collectProvidersConcurrently(int nbWorkers, final ExecutorService executor) throws Exception %7B%0A        final DataStoreRegistry dsr = new DataStoreRegistry(DataStoresTest.class.getClassLoader());%0A        final CyclicBarrier startSignal = new CyclicBarrier(nbWorkers);%0A        Callable%3CSet%3E collectProviderClasses = () -%3E %7B%0A            startSignal.await(1, TimeUnit.SECONDS);%0A            Set%3CClass%3E result = new HashSet%3C%3E();%0A            for (DataStoreProvider p : dsr.providers()) result.add(p.getClass());%0A            return result;%0A        %7D;%0A%0A        final List%3CFuture%3CSet%3E%3E tasks = new ArrayList%3C%3E(nbWorkers);%0A        for (int i = 0 ; i %3C nbWorkers ; i++) %7B%0A            tasks.add(executor.submit(collectProviderClasses));%0A        %7D%0A%0A        final List%3CSet%3CClass%3E%3E results = new ArrayList%3C%3E(nbWorkers);%0A        for (int i = 0 ; i %3C nbWorkers ; i++) %7B%0A            Set workerResult = tasks.get(i).get(2, TimeUnit.SECONDS);%0A            results.add(workerResult);%0A        %7D%0A%0A        return results;%0A    %7D%0A
 %7D%0A

add unit test to check concurrency of DataStores providers fetching