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