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 KvnGeneratorTest {
28  
29      @Test
30      public void testSection() throws IOException {
31          CharArrayWriter caw = new CharArrayWriter();
32          try (Generator generator = new KvnGenerator(caw, 10, "", 25)) {
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("CCSDS_ABC_VERSION = 99.0\n" +
39                                  "BLOCK_START\n" +
40                                  "KEY        = 1234567.8   [Hz]\n" + 
41                                  "BLOCK_STOP\n",
42                                  caw.toString());
43          }
44      }
45  
46      @Test
47      public void testCcsdsUnits() throws IOException {
48          CharArrayWriter caw = new CharArrayWriter();
49          try (Generator generator = new KvnGenerator(caw, 10, "", 25)) {
50              generator.writeEntry("KEY_1",    1234567.8,   Unit.parse("km.kg³/√s"), false);
51              generator.writeEntry("KEY_2",    1234567.8,   Unit.parse("n/a"),       false);
52              generator.writeEntry("KEY_3",    1234567.8,   Unit.parse("1"),         false);
53              generator.writeEntry("LOOOOONG", "1234567.8", null,                    false);
54              Assert.assertEquals("KEY_1      = 1234.5678   [km*kg**3/s**0.5]\n" +
55                                  "KEY_2      = 1234567.8\n" +
56                                  "KEY_3      = 1234567.8\n" +
57                                  "LOOOOONG   = 1234567.8\n",
58                                  caw.toString());
59          }
60      }
61  
62      @Test
63      public void testUnitsPadding() throws IOException {
64          CharArrayWriter caw = new CharArrayWriter();
65          try (Generator generator = new KvnGenerator(caw, 10, "", 20)) {
66              generator.writeEntry("KEY_1", 0.5 * FastMath.PI, Unit.parse("°"), false);
67              generator.writeEntry("KEY_2", FastMath.PI, Unit.parse("◦"), false);
68              generator.writeEntry("PERCENT", 0.25, Unit.parse("%"), false);
69              Assert.assertEquals("KEY_1      = 90.0   [deg]\n" +
70                                  "KEY_2      = 180.0  [deg]\n" +
71                                  "PERCENT    = 25.0   [%]\n",
72                                  caw.toString());
73          }
74      }
75  
76      @Test
77      public void testNoUnits() throws IOException {
78          CharArrayWriter caw = new CharArrayWriter();
79          try (Generator generator = new KvnGenerator(caw, 10, "", 0)) {
80              generator.writeEntry("KEY_1", 0.5 * FastMath.PI, Unit.parse("°"), false);
81              generator.writeEntry("KEY_2", FastMath.PI, Unit.parse("◦"), true);
82              Assert.assertEquals("KEY_1      = 90.0\n" +
83                                  "KEY_2      = 180.0\n", caw.toString());
84          }
85      }
86  
87  }