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.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URISyntaxException;
27  import java.net.URL;
28  import java.text.ParseException;
29  import java.util.regex.Pattern;
30  
31  public class DirectoryCrawlerTest {
32  
33      @Test
34      public void testNoDirectory() throws URISyntaxException {
35          Assertions.assertThrows(OrekitException.class, () -> {
36              File existing = new File(getClass().getClassLoader().getResource("regular-data").toURI().getPath());
37              File inexistent = new File(existing.getParent(), "inexistant-directory");
38              new DirectoryCrawler(inexistent).feed(Pattern.compile(".*"), new CountingLoader(),
39                      DataContext.getDefault().getDataProvidersManager());
40          });
41     }
42  
43      @Test
44      public void testNotADirectory() throws URISyntaxException {
45          Assertions.assertThrows(OrekitException.class, () -> {
46              URL url =
47                      DirectoryCrawlerTest.class.getClassLoader().getResource("regular-data/UTC-TAI.history");
48              new DirectoryCrawler(new File(url.toURI().getPath())).feed(Pattern.compile(".*"), new CountingLoader(),
49                      DataContext.getDefault().getDataProvidersManager());
50          });
51      }
52  
53      @Test
54      public void testNominal() throws URISyntaxException {
55          URL url =
56              DirectoryCrawlerTest.class.getClassLoader().getResource("regular-data");
57          CountingLoader crawler = new CountingLoader();
58          new DirectoryCrawler(new File(url.toURI().getPath())).feed(Pattern.compile(".*"), crawler,
59                                                                     DataContext.getDefault().getDataProvidersManager());
60          Assertions.assertTrue(crawler.getCount() > 0);
61      }
62  
63      @Test
64      public void testCompressed() throws URISyntaxException {
65          URL url =
66              DirectoryCrawlerTest.class.getClassLoader().getResource("compressed-data");
67          CountingLoader crawler = new CountingLoader();
68          new DirectoryCrawler(new File(url.toURI().getPath())).feed(Pattern.compile(".*"), crawler,
69                                                                     DataContext.getDefault().getDataProvidersManager());
70          Assertions.assertTrue(crawler.getCount() > 0);
71      }
72  
73      @Test
74      public void testMultiZipClasspath() throws URISyntaxException {
75          URL url =
76              DirectoryCrawlerTest.class.getClassLoader().getResource("zipped-data/multizip.zip");
77          File parent = new File(url.toURI().getPath()).getParentFile();
78          CountingLoader crawler = new CountingLoader();
79          new DirectoryCrawler(parent).feed(Pattern.compile(".*\\.txt$"), crawler,
80                                            DataContext.getDefault().getDataProvidersManager());
81          Assertions.assertEquals(7, crawler.getCount());
82      }
83  
84      @Test
85      public void testIOException() throws URISyntaxException {
86          Assertions.assertThrows(OrekitException.class, () -> {
87              URL url =
88                      DirectoryCrawlerTest.class.getClassLoader().getResource("regular-data");
89              try {
90                  new DirectoryCrawler(new File(url.toURI().getPath())).feed(Pattern.compile(".*"), new IOExceptionLoader(),
91                          DataContext.getDefault().getDataProvidersManager());
92              } catch (OrekitException oe) {
93                  // expected behavior
94                  Assertions.assertNotNull(oe.getCause());
95                  Assertions.assertEquals(IOException.class, oe.getCause().getClass());
96                  Assertions.assertEquals("dummy error", oe.getMessage());
97                  throw oe;
98              }
99          });
100     }
101 
102     @Test
103     public void testParseException() throws URISyntaxException {
104         Assertions.assertThrows(OrekitException.class, () -> {
105             URL url =
106                     DirectoryCrawlerTest.class.getClassLoader().getResource("regular-data");
107             try {
108                 new DirectoryCrawler(new File(url.toURI().getPath())).feed(Pattern.compile(".*"), new ParseExceptionLoader(),
109                         DataContext.getDefault().getDataProvidersManager());
110             } catch (OrekitException oe) {
111                 // expected behavior
112                 Assertions.assertNotNull(oe.getCause());
113                 Assertions.assertEquals(ParseException.class, oe.getCause().getClass());
114                 Assertions.assertEquals("dummy error", oe.getMessage());
115                 throw oe;
116             }
117         });
118     }
119 
120     private static class CountingLoader implements DataLoader {
121         private int count = 0;
122         public boolean stillAcceptsData() {
123             return true;
124         }
125         public void loadData(InputStream input, String name) {
126             ++count;
127         }
128         public int getCount() {
129             return count;
130         }
131     }
132 
133     private static class IOExceptionLoader implements DataLoader {
134         public boolean stillAcceptsData() {
135             return true;
136         }
137         public void loadData(InputStream input, String name) throws IOException {
138             if (name.endsWith("UTC-TAI.history")) {
139                 throw new IOException("dummy error");
140             }
141         }
142     }
143 
144     private static class ParseExceptionLoader implements DataLoader {
145         public boolean stillAcceptsData() {
146             return true;
147         }
148         public void loadData(InputStream input, String name) throws ParseException {
149             if (name.endsWith("UTC-TAI.history")) {
150                 throw new ParseException("dummy error", 0);
151             }
152         }
153     }
154 
155 }