1   /* Copyright 2002-2022 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.data;
18  
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.text.ParseException;
23  import java.util.regex.Pattern;
24  
25  import org.junit.Assert;
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.orekit.errors.OrekitException;
29  
30  public abstract class AbstractListCrawlerTest<T> {
31  
32      protected abstract T input(String resource);
33  
34      protected abstract AbstractListCrawler<T> build(String... inputs);
35  
36      @Before
37      public void setUp() {
38          // Clear any filters that another test may have left
39          DataContext.getDefault().getDataProvidersManager().resetFiltersToDefault();
40      }
41  
42      @Test
43      public void local() {
44          CountingLoader crawler = new CountingLoader();
45          AbstractListCrawler<T> nc = build("regular-data/UTC-TAI.history",
46                                            "regular-data/de405-ephemerides/unxp0000.405",
47                                            "regular-data/de405-ephemerides/unxp0001.405",
48                                            "regular-data/de406-ephemerides/unxp0000.406");
49          Assert.assertEquals(4, nc.getInputs().size());
50          nc.addInput(input("regular-data/Earth-orientation-parameters/monthly/bulletinb_IAU2000-216.txt"));
51          Assert.assertEquals(5, nc.getInputs().size());
52          nc.feed(Pattern.compile(".*"), crawler, DataContext.getDefault().getDataProvidersManager());
53          Assert.assertEquals(5, crawler.getCount());
54      }
55  
56      @Test
57      public void compressed() {
58          CountingLoader crawler = new CountingLoader();
59          AbstractListCrawler<T> nc = build();
60          nc.addInput(input("compressed-data/UTC-TAI.history.gz"));
61          nc.addInput(input("compressed-data/eopc04_08_IAU2000.00.gz"));
62          nc.addInput(input("compressed-data/eopc04_08_IAU2000.02.gz"));
63          nc.feed(Pattern.compile("^eopc04.*"), crawler,
64                  DataContext.getDefault().getDataProvidersManager());
65          Assert.assertEquals(2, crawler.getCount());
66      }
67  
68      @Test
69      public void multiZip() {
70          CountingLoader crawler = new CountingLoader();
71          build("zipped-data/multizip.zip").feed(Pattern.compile(".*\\.txt$"), crawler,
72                                                 DataContext.getDefault().getDataProvidersManager());
73          Assert.assertEquals(6, crawler.getCount());
74      }
75  
76      @Test(expected=OrekitException.class)
77      public void ioException() {
78          try {
79              build("regular-data/UTC-TAI.history").feed(Pattern.compile(".*"), new IOExceptionLoader(),
80                                                         DataContext.getDefault().getDataProvidersManager());
81          } catch (OrekitException oe) {
82              // expected behavior
83              Assert.assertNotNull(oe.getCause());
84              Assert.assertEquals(IOException.class, oe.getCause().getClass());
85              Assert.assertEquals("dummy error", oe.getMessage());
86              throw oe;
87          }
88      }
89  
90      @Test(expected=OrekitException.class)
91      public void parseException() {
92          try {
93              build("regular-data/UTC-TAI.history").feed(Pattern.compile(".*"), new ParseExceptionLoader(),
94                                                         DataContext.getDefault().getDataProvidersManager());
95          } catch (OrekitException oe) {
96              // expected behavior
97              Assert.assertNotNull(oe.getCause());
98              Assert.assertEquals(ParseException.class, oe.getCause().getClass());
99              Assert.assertEquals("dummy error", oe.getMessage());
100             throw oe;
101         }
102     }
103 
104     protected static class CountingLoader implements DataLoader {
105         private int count = 0;
106         public boolean stillAcceptsData() {
107             return true;
108         }
109         public void loadData(InputStream input, String name) {
110             ++count;
111         }
112         public int getCount() {
113             return count;
114         }
115     }
116 
117     private static class IOExceptionLoader implements DataLoader {
118         public boolean stillAcceptsData() {
119             return true;
120         }
121         public void loadData(InputStream input, String name) throws IOException {
122             if (name.endsWith("UTC-TAI.history")) {
123                 throw new IOException("dummy error");
124             }
125         }
126     }
127 
128     private static class ParseExceptionLoader implements DataLoader {
129         public boolean stillAcceptsData() {
130             return true;
131         }
132         public void loadData(InputStream input, String name) throws ParseException {
133             if (name.endsWith("UTC-TAI.history")) {
134                 throw new ParseException("dummy error", 0);
135             }
136         }
137     }
138 
139 }