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