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.files.ccsds.utils.generation;
18  
19  import org.hipparchus.util.FastMath;
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.utils.Constants;
23  import org.orekit.utils.units.Unit;
24  
25  import java.io.CharArrayWriter;
26  import java.io.IOException;
27  
28  public class XmlGeneratorTest {
29  
30      @Test
31      public void testSections() throws IOException {
32          CharArrayWriter caw = new CharArrayWriter();
33          try (Generator generator = new XmlGenerator(caw, 2, "", Constants.JULIAN_DAY, true, null)) {
34              generator.startMessage("abc", "CCSDS_ABC_VERSION", 99.0);
35              generator.enterSection("BLOCK");
36              generator.writeEntry("KEY", 1234567.8, Unit.parse("Hz"), false);
37              generator.exitSection();
38              generator.endMessage("abc");
39              Assertions.assertEquals(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n" +
40                                                "<abc id=\"CCSDS_ABC_VERSION\" version=\"99.0\">%n" +
41                                                "  <BLOCK>%n" +
42                                                "    <KEY units=\"Hz\">1234567.8</KEY>%n" +
43                                                "  </BLOCK>%n" +
44                                                "</abc>%n"),
45                                  caw.toString());
46  
47          }
48      }
49  
50      @Test
51      public void testCcsdsUnits() throws IOException {
52          CharArrayWriter caw = new CharArrayWriter();
53          try (Generator generator = new XmlGenerator(caw, 2, "", Constants.JULIAN_DAY, true, "nowhere")) {
54              generator.writeEntry("KEY_1",    1234567.8,   Unit.parse("km.kg³/√s"), false);
55              generator.writeEntry("KEY_2",    1234567.8,   Unit.parse("n/a"),       false);
56              generator.writeEntry("KEY_3",    1234567.8,   Unit.parse("1"),         false);
57              generator.writeEntry("LOOOOONG", "1234567.8", null,                    false);
58              Assertions.assertEquals(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n" +
59                                                "<KEY_1 units=\"km*kg**3/s**0.5\">1234.5678</KEY_1>%n" +
60                                                "<KEY_2>1234567.8</KEY_2>%n" +
61                                                "<KEY_3>1234567.8</KEY_3>%n" +
62                                                "<LOOOOONG>1234567.8</LOOOOONG>%n"),
63                                  caw.toString());
64          }
65      }
66  
67      @Test
68      public void testUnitsPadding() throws IOException {
69          CharArrayWriter caw = new CharArrayWriter();
70          try (Generator generator = new XmlGenerator(caw, 2, "", Constants.JULIAN_DAY, true, "nowhere")) {
71              generator.writeEntry("KEY_1", 0.5 * FastMath.PI, Unit.parse("°"), false);
72              generator.writeEntry("KEY_2", FastMath.PI, Unit.parse("◦"), false);
73              generator.writeEntry("PERCENT", 0.25, Unit.parse("%"), false);
74              Assertions.assertEquals(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n" +
75                                                "<KEY_1 units=\"deg\">90.0</KEY_1>%n" +
76                                                "<KEY_2 units=\"deg\">180.0</KEY_2>%n" +
77                                                "<PERCENT units=\"%%\">25.0</PERCENT>%n"),
78                                  caw.toString());
79          }
80      }
81  
82      @Test
83      public void testNoUnits() throws IOException {
84          CharArrayWriter caw = new CharArrayWriter();
85          try (Generator generator = new XmlGenerator(caw, 2, "", Constants.JULIAN_DAY, false, "nowhere")) {
86              generator.writeEntry("KEY_1", 0.5 * FastMath.PI, Unit.parse("°"), false);
87              generator.writeEntry("KEY_2", FastMath.PI, Unit.parse("◦"), true);
88              Assertions.assertEquals(String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n" +
89                                                "<KEY_1>90.0</KEY_1>%n" +
90                                                "<KEY_2>180.0</KEY_2>%n"),
91                                  caw.toString());
92          }
93      }
94  
95  }