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.Test;
21  import org.orekit.errors.OrekitException;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.text.ParseException;
26  import java.util.regex.Pattern;
27  
28  public class ClasspathCrawlerTest {
29  
30      @Test
31      public void testNoElement() {
32          Assertions.assertThrows(OrekitException.class, () -> {
33              new ClasspathCrawler("inexistant-element").feed(Pattern.compile(".*"),
34                      new CountingLoader(),
35                      DataContext.getDefault().getDataProvidersManager());
36          });
37      }
38  
39      @Test
40      public void testNominal() {
41          CountingLoader crawler = new CountingLoader();
42          new ClasspathCrawler("regular-data/UTC-TAI.history",
43                               "regular-data/de405-ephemerides/unxp0000.405",
44                               "regular-data/de405-ephemerides/unxp0001.405",
45                               "regular-data/de406-ephemerides/unxp0000.406",
46                               "regular-data/Earth-orientation-parameters/monthly/bulletinb_IAU2000-216.txt",
47                               "no-data/dummy.txt").feed(Pattern.compile(".*"), crawler,
48                                                         DataContext.getDefault().getDataProvidersManager());
49          Assertions.assertEquals(6, crawler.getCount());
50      }
51  
52      @Test
53      public void testCompressed() {
54          CountingLoader crawler = new CountingLoader();
55          new ClasspathCrawler("compressed-data/UTC-TAI.history.gz",
56                               "compressed-data/eopc04_08_IAU2000.00.gz",
57                               "compressed-data/eopc04_08_IAU2000.02.gz").feed(Pattern.compile(".*eopc04.*"),
58                                                                               crawler,
59                                                                               DataContext.getDefault().getDataProvidersManager());
60          Assertions.assertEquals(2, crawler.getCount());
61      }
62  
63      @Test
64      public void testMultiZip() {
65          CountingLoader crawler = new CountingLoader();
66          new ClasspathCrawler("zipped-data/multizip.zip").feed(Pattern.compile(".*\\.txt$"),
67                                                                crawler,
68                                                                DataContext.getDefault().getDataProvidersManager());
69          Assertions.assertEquals(6, crawler.getCount());
70      }
71  
72      @Test
73      public void testIOException() {
74          Assertions.assertThrows(OrekitException.class, () -> {
75              try {
76                  new ClasspathCrawler("regular-data/UTC-TAI.history").feed(Pattern.compile(".*"), new IOExceptionLoader(),
77                          DataContext.getDefault().getDataProvidersManager());
78              } catch (OrekitException oe) {
79                  // expected behavior
80                  Assertions.assertNotNull(oe.getCause());
81                  Assertions.assertEquals(IOException.class, oe.getCause().getClass());
82                  Assertions.assertEquals("dummy error", oe.getMessage());
83                  throw oe;
84              }
85          });
86      }
87  
88      @Test
89      public void testParseException() {
90          Assertions.assertThrows(OrekitException.class, () -> {
91              try {
92                  new ClasspathCrawler("regular-data/UTC-TAI.history").feed(Pattern.compile(".*"), new ParseExceptionLoader(),
93                          DataContext.getDefault().getDataProvidersManager());
94              } catch (OrekitException oe) {
95                  // expected behavior
96                  Assertions.assertNotNull(oe.getCause());
97                  Assertions.assertEquals(ParseException.class, oe.getCause().getClass());
98                  Assertions.assertEquals("dummy error", oe.getMessage());
99                  throw oe;
100             }
101         });
102     }
103 
104     /**
105      * Check that only the file name portion is matched so that ^ works as expected with
106      * other crawlers. See #618.
107      */
108     @Test
109     public void testMatchesFileName618() {
110         CountingLoader crawler = new CountingLoader();
111         new ClasspathCrawler(
112                 "regular-data/UTC-TAI.history",
113                 "compressed-data/UTC-TAI.history.gz",
114                 "no-data/dummy.txt"
115         ).feed(
116                 Pattern.compile("^UTC-TAI.history$"),
117                 crawler,
118                 DataContext.getDefault().getDataProvidersManager());
119         Assertions.assertEquals(2, crawler.getCount());
120     }
121 
122     private static class CountingLoader implements DataLoader {
123         private int count = 0;
124         public boolean stillAcceptsData() {
125             return true;
126         }
127         public void loadData(InputStream input, String name) {
128             ++count;
129         }
130         public int getCount() {
131             return count;
132         }
133     }
134 
135     private static class IOExceptionLoader implements DataLoader {
136         public boolean stillAcceptsData() {
137             return true;
138         }
139         public void loadData(InputStream input, String name) throws IOException {
140             if (name.endsWith("UTC-TAI.history")) {
141                 throw new IOException("dummy error");
142             }
143         }
144     }
145 
146     private static class ParseExceptionLoader implements DataLoader {
147         public boolean stillAcceptsData() {
148             return true;
149         }
150         public void loadData(InputStream input, String name) throws ParseException {
151             if (name.endsWith("UTC-TAI.history")) {
152                 throw new ParseException("dummy error", 0);
153             }
154         }
155     }
156 
157 }