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