1   /* Copyright 2002-2025 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  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.errors.OrekitException;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.text.ParseException;
27  import java.util.regex.Pattern;
28  
29  public abstract class AbstractListCrawlerTest<T> {
30  
31      protected abstract T input(String resource);
32  
33      protected abstract AbstractListCrawler<T> build(String... inputs);
34  
35      @BeforeEach
36      public void setUp() {
37          // Clear any filters that another test may have left
38          DataContext.getDefault().getDataProvidersManager().resetFiltersToDefault();
39      }
40  
41      @Test
42      public void local() {
43          CountingLoader crawler = new CountingLoader();
44          AbstractListCrawler<T> nc = build("regular-data/UTC-TAI.history",
45                                            "regular-data/de405-ephemerides/unxp0000.405",
46                                            "regular-data/de405-ephemerides/unxp0001.405",
47                                            "regular-data/de406-ephemerides/unxp0000.406");
48          Assertions.assertEquals(4, nc.getInputs().size());
49          nc.addInput(input("regular-data/Earth-orientation-parameters/monthly/bulletinb_IAU2000-216.txt"));
50          Assertions.assertEquals(5, nc.getInputs().size());
51          nc.feed(Pattern.compile(".*"), crawler, DataContext.getDefault().getDataProvidersManager());
52          Assertions.assertEquals(5, crawler.getCount());
53      }
54  
55      @Test
56      public void compressed() {
57          CountingLoader crawler = new CountingLoader();
58          AbstractListCrawler<T> nc = build();
59          nc.addInput(input("compressed-data/UTC-TAI.history.gz"));
60          nc.addInput(input("compressed-data/eopc04_08_IAU2000.00.gz"));
61          nc.addInput(input("compressed-data/eopc04_08_IAU2000.02.gz"));
62          nc.feed(Pattern.compile("^eopc04.*"), crawler,
63                  DataContext.getDefault().getDataProvidersManager());
64          Assertions.assertEquals(2, crawler.getCount());
65      }
66  
67      @Test
68      public void multiZip() {
69          CountingLoader crawler = new CountingLoader();
70          build("zipped-data/multizip.zip").feed(Pattern.compile(".*\\.txt$"), crawler,
71                                                 DataContext.getDefault().getDataProvidersManager());
72          Assertions.assertEquals(6, crawler.getCount());
73      }
74  
75      @Test
76      public void ioException() {
77          Assertions.assertThrows(OrekitException.class, () -> {
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                  Assertions.assertNotNull(oe.getCause());
84                  Assertions.assertEquals(IOException.class, oe.getCause().getClass());
85                  Assertions.assertEquals("dummy error", oe.getMessage());
86                  throw oe;
87              }
88          });
89      }
90  
91      @Test
92      public void parseException() {
93          Assertions.assertThrows(OrekitException.class, () -> {
94              try {
95                  build("regular-data/UTC-TAI.history").feed(Pattern.compile(".*"), new ParseExceptionLoader(),
96                          DataContext.getDefault().getDataProvidersManager());
97              } catch (OrekitException oe) {
98                  // expected behavior
99                  Assertions.assertNotNull(oe.getCause());
100                 Assertions.assertEquals(ParseException.class, oe.getCause().getClass());
101                 Assertions.assertEquals("dummy error", oe.getMessage());
102                 throw oe;
103             }
104         });
105     }
106 
107     protected static class CountingLoader implements DataLoader {
108         private int count = 0;
109         public boolean stillAcceptsData() {
110             return true;
111         }
112         public void loadData(InputStream input, String name) {
113             ++count;
114         }
115         public int getCount() {
116             return count;
117         }
118     }
119 
120     private static class IOExceptionLoader implements DataLoader {
121         public boolean stillAcceptsData() {
122             return true;
123         }
124         public void loadData(InputStream input, String name) throws IOException {
125             if (name.endsWith("UTC-TAI.history")) {
126                 throw new IOException("dummy error");
127             }
128         }
129     }
130 
131     private static class ParseExceptionLoader implements DataLoader {
132         public boolean stillAcceptsData() {
133             return true;
134         }
135         public void loadData(InputStream input, String name) throws ParseException {
136             if (name.endsWith("UTC-TAI.history")) {
137                 throw new ParseException("dummy error", 0);
138             }
139         }
140     }
141 
142 }