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