1   /* Copyright 2002-2022 CS GROUP
2    * Licensed to CS Systèmes d'Information (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.files.ccsds.ndm;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.CharArrayWriter;
21  import java.io.IOException;
22  import java.nio.charset.StandardCharsets;
23  
24  import org.junit.Assert;
25  import org.junit.Before;
26  import org.orekit.Utils;
27  import org.orekit.data.DataSource;
28  import org.orekit.files.ccsds.section.Header;
29  import org.orekit.files.ccsds.section.Segment;
30  import org.orekit.files.ccsds.utils.FileFormat;
31  import org.orekit.files.ccsds.utils.generation.Generator;
32  import org.orekit.files.ccsds.utils.generation.KvnGenerator;
33  import org.orekit.files.ccsds.utils.generation.MessageWriter;
34  import org.orekit.files.ccsds.utils.generation.XmlGenerator;
35  import org.orekit.files.ccsds.utils.lexical.MessageParser;
36  
37  public abstract class AbstractWriterTest<H extends Header, S extends Segment<?, ?>, F extends NdmConstituent<H, S>> {
38  
39      @Before
40      public void setUp() {
41          Utils.setDataRoot("regular-data");
42      }
43  
44      protected abstract MessageParser<F>       getParser();
45      protected abstract MessageWriter<H, S, F> getWriter();
46  
47      protected  void doTest(final String name) {
48          doTest(name, FileFormat.KVN, 60);
49          doTest(name, FileFormat.KVN,  0);
50          doTest(name, FileFormat.XML, 60);
51          doTest(name, FileFormat.XML,  0);
52      }
53  
54      protected  void doTest(final String name, final FileFormat format, final int unitsColumn) {
55          try {
56              final DataSource source1  = new DataSource(name, () -> getClass().getResourceAsStream(name));
57              final F          original = getParser().parseMessage(source1);
58  
59              // write the parsed file back to a characters array
60              final CharArrayWriter caw = new CharArrayWriter();
61              try (Generator generator = format == FileFormat.KVN ?
62                                         new KvnGenerator(caw, 25, "dummy.kvn", unitsColumn) :
63                                         new XmlGenerator(caw, XmlGenerator.DEFAULT_INDENT, "dummy.xml", unitsColumn > 0)) {
64                  getWriter().writeMessage(generator, original);
65              }
66  
67              // reparse the written file
68              final byte[]      bytes  = caw.toString().getBytes(StandardCharsets.UTF_8);
69              final DataSource source2 = new DataSource(name, () -> new ByteArrayInputStream(bytes));
70              final F          rebuilt = getParser().parseMessage(source2);
71  
72              NdmTestUtils.checkEquals(original, rebuilt);
73  
74          } catch (IOException ioe) {
75              Assert.fail(ioe.getLocalizedMessage());
76          }
77      }
78  
79  }