<commit_before>/* Copyright 2002-2012 CS Systèmes d'Information
 * Licensed to CS Systèmes d'Information (CS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * CS 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.orekit.data;


import java.io.File;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.math3.exception.util.DummyLocalizable;
import org.junit.Assert;
import org.junit.Test;
import org.orekit.errors.OrekitException;

public class DataProvidersManagerTest {

    @Test
    public void testDefaultConfiguration() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        Assert.assertFalse(DataProvidersManager.getInstance().isSupported(new DirectoryCrawler(new File(getPath("regular-data")))));
        Assert.assertTrue(DataProvidersManager.getInstance().feed(".*", crawler));
        Assert.assertEquals(18, crawler.getCount());
    }

    @Test
    public void testLoadMonitoring() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        DataProvidersManager manager = DataProvidersManager.getInstance();
        manager.clearProviders();
        manager.clearLoadedDataNames();
        Assert.assertFalse(manager.isSupported(new DirectoryCrawler(new File(getPath("regular-data")))));
        Assert.assertEquals(0, manager.getLoadedDataNames().size());
        CountingLoader tleCounter = new CountingLoader(false);
        Assert.assertTrue(manager.feed(".*\\.tle$", tleCounter));
        Assert.assertEquals(3, tleCounter.getCount());
        Assert.assertEquals(3, manager.getLoadedDataNames().size());
        CountingLoader de405Counter = new CountingLoader(false);
        Assert.assertTrue(manager.feed(".*\\.405$", de405Counter));
        Assert.assertEquals(4, de405Counter.getCount());
        Assert.assertEquals(7, manager.getLoadedDataNames().size());
        manager.clearLoadedDataNames();
        Assert.assertEquals(0, manager.getLoadedDataNames().size());
    }

    @Test
    public void testLoadFailure() {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        DataProvidersManager.getInstance().clearProviders();
        CountingLoader crawler = new CountingLoader(true);
        try {
            DataProvidersManager.getInstance().feed(".*", crawler);
            Assert.fail("an exception should have been thrown");
        } catch (OrekitException oe) {
            // expected
        }
        Assert.assertEquals(18, crawler.getCount());
    }

    @Test
    public void testEmptyProperty() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, "");
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
        Assert.assertEquals(0, crawler.getCount());
    }

    @Test(expected=OrekitException.class)
    public void testInexistentDirectory() throws OrekitException {
        File inexistent = new File(getPath("regular-data"), "inexistent");
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, inexistent.getAbsolutePath());
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
    }

    @Test(expected=OrekitException.class)
    public void testInexistentZipArchive() throws OrekitException {
        File inexistent = new File(getPath("regular-data"), "inexistent.zip");
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, inexistent.getAbsolutePath());
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
    }

    @Test(expected=OrekitException.class)
    public void testNeitherDirectoryNorZip() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data/UTC-TAI.history"));
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
    }

    @Test
    public void testListModification() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager manager = DataProvidersManager.getInstance();
        manager.clearProviders();
        Assert.assertFalse(manager.isSupported(new DirectoryCrawler(new File(getPath("regular-data")))));
        Assert.assertTrue(manager.feed(".*", crawler));
        Assert.assertTrue(crawler.getCount() > 0);
        List<DataProvider> providers = manager.getProviders();
        Assert.assertEquals(1, providers.size());
        for (DataProvider provider : providers) {
            Assert.assertTrue(manager.isSupported(provider));
        }
        Assert.assertNotNull(manager.removeProvider(providers.get(0)));
        Assert.assertEquals(0, manager.getProviders().size());
        DataProvider provider = new DataProvider() {
            private static final long serialVersionUID = -5312255682914297696L;
            public boolean feed(Pattern supported, DataLoader visitor) throws OrekitException {
                return true;
            }
        };
        manager.addProvider(provider);
        Assert.assertEquals(1, manager.getProviders().size());
        manager.addProvider(provider);
        Assert.assertEquals(2, manager.getProviders().size());
        Assert.assertNotNull(manager.removeProvider(provider));
        Assert.assertEquals(1, manager.getProviders().size());
        Assert.assertNull(manager.removeProvider(new DataProvider() {
            private static final long serialVersionUID = 6368246625696570910L;<commit_msg>Fixed warnings.<commit_after>/* Copyright 2002-2012 CS Systèmes d'Information
 * Licensed to CS Systèmes d'Information (CS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * CS 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.orekit.data;


import java.io.File;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.math3.exception.util.DummyLocalizable;
import org.junit.Assert;
import org.junit.Test;
import org.orekit.errors.OrekitException;

public class DataProvidersManagerTest {

    @Test
    public void testDefaultConfiguration() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        Assert.assertFalse(DataProvidersManager.getInstance().isSupported(new DirectoryCrawler(new File(getPath("regular-data")))));
        Assert.assertTrue(DataProvidersManager.getInstance().feed(".*", crawler));
        Assert.assertEquals(18, crawler.getCount());
    }

    @Test
    public void testLoadMonitoring() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        DataProvidersManager manager = DataProvidersManager.getInstance();
        manager.clearProviders();
        manager.clearLoadedDataNames();
        Assert.assertFalse(manager.isSupported(new DirectoryCrawler(new File(getPath("regular-data")))));
        Assert.assertEquals(0, manager.getLoadedDataNames().size());
        CountingLoader tleCounter = new CountingLoader(false);
        Assert.assertTrue(manager.feed(".*\\.tle$", tleCounter));
        Assert.assertEquals(3, tleCounter.getCount());
        Assert.assertEquals(3, manager.getLoadedDataNames().size());
        CountingLoader de405Counter = new CountingLoader(false);
        Assert.assertTrue(manager.feed(".*\\.405$", de405Counter));
        Assert.assertEquals(4, de405Counter.getCount());
        Assert.assertEquals(7, manager.getLoadedDataNames().size());
        manager.clearLoadedDataNames();
        Assert.assertEquals(0, manager.getLoadedDataNames().size());
    }

    @Test
    public void testLoadFailure() {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        DataProvidersManager.getInstance().clearProviders();
        CountingLoader crawler = new CountingLoader(true);
        try {
            DataProvidersManager.getInstance().feed(".*", crawler);
            Assert.fail("an exception should have been thrown");
        } catch (OrekitException oe) {
            // expected
        }
        Assert.assertEquals(18, crawler.getCount());
    }

    @Test
    public void testEmptyProperty() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, "");
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
        Assert.assertEquals(0, crawler.getCount());
    }

    @Test(expected=OrekitException.class)
    public void testInexistentDirectory() throws OrekitException {
        File inexistent = new File(getPath("regular-data"), "inexistent");
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, inexistent.getAbsolutePath());
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
    }

    @Test(expected=OrekitException.class)
    public void testInexistentZipArchive() throws OrekitException {
        File inexistent = new File(getPath("regular-data"), "inexistent.zip");
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, inexistent.getAbsolutePath());
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
    }

    @Test(expected=OrekitException.class)
    public void testNeitherDirectoryNorZip() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data/UTC-TAI.history"));
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager.getInstance().clearProviders();
        DataProvidersManager.getInstance().feed(".*", crawler);
    }

    @Test
    public void testListModification() throws OrekitException {
        System.setProperty(DataProvidersManager.OREKIT_DATA_PATH, getPath("regular-data"));
        CountingLoader crawler = new CountingLoader(false);
        DataProvidersManager manager = DataProvidersManager.getInstance();
        manager.clearProviders();
        Assert.assertFalse(manager.isSupported(new DirectoryCrawler(new File(getPath("regular-data")))));
        Assert.assertTrue(manager.feed(".*", crawler));
        Assert.assertTrue(crawler.getCount() > 0);
        List<DataProvider> providers = manager.getProviders();
        Assert.assertEquals(1, providers.size());
        for (DataProvider provider : providers) {
            Assert.assertTrue(manager.isSupported(provider));
        }
        Assert.assertNotNull(manager.removeProvider(providers.get(0)));
        Assert.assertEquals(0, manager.getProviders().size());
        DataProvider provider = new DataProvider() {
            public boolean feed(Pattern supported, DataLoader visitor) throws OrekitException {
                return true;
            }
        };
        manager.addProvider(provider);
        Assert.assertEquals(1, manager.getProviders().size());
        manager.addProvider(provider);
        Assert.assertEquals(2, manager.getProviders().size());
        Assert.assertNotNull(manager.removeProvider(provider));
        Assert.assertEquals(1, manager.getProviders().size());
        Assert.assertNull(manager.removeProvider(new DataProvider() {
{'fraction_non_alphanumeric': 0.10987717878943885, 'fraction_numerical': 0.009119658748253292, 'mean_word_length': 3.8564285714285713, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1562272', 'n_tokens_mistral': 3608, 'n_tokens_neox': 3504, 'n_words': 728}