View Javadoc
1   package org.djunits.generator;
2   
3   import java.io.IOException;
4   import java.net.URISyntaxException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   /**
9    * GenerateCliConverters.java. <br>
10   * <br>
11   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
13   * source code and binary code of this software is proprietary information of Delft University of Technology.
14   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
15   */
16  public class GenerateCliConvertersTest
17  {
18      /** */
19      private static List<TestRecord> testRecords = new ArrayList<>();
20  
21      static
22      {
23          // @formatter:off
24          testRecords.add(new TestRecord("AbsoluteTemperature", 200.0, "AbsoluteTemperatureUnit.KELVIN", "200.0K", 
25                  100.0, "AbsoluteTemperatureUnit.DEGREE_CELSIUS", "100.0C"));
26          testRecords.add(new TestRecord("AbsorbedDose", 200.0, "AbsorbedDoseUnit.GRAY", "200Gy", 
27                  100.0, "AbsorbedDoseUnit.ERG_PER_GRAM", "100.0erg/g"));
28          testRecords.add(new TestRecord("Acceleration", 2.0, "AccelerationUnit.METER_PER_SECOND_2", "2.0m/s^2", 
29                  1.0, "AccelerationUnit.KM_PER_HOUR_2", "1.0km/h^2"));
30          testRecords.add(new TestRecord("AmountOfSubstance", 200.0, "AmountOfSubstanceUnit.MOLE", "200.0mol",
31                  10.0, "AmountOfSubstanceUnit.MILLIMOLE", "10mmol"));
32          testRecords.add(new TestRecord("Angle", 2.0, "AngleUnit.RADIAN", "2.0rad", 
33                  10.0, "AngleUnit.DEGREE", "10.0deg"));
34          testRecords.add(new TestRecord("AngularAcceleration", 2.0, "AngularAccelerationUnit.RADIAN_PER_SECOND_SQUARED", "2.0rad/s2", 
35                  10.0, "AngularAccelerationUnit.CENTESIMAL_ARCSECOND_PER_SECOND_SQUARED", "10.0c\\\"/sec2"));
36          testRecords.add(new TestRecord("AngularVelocity", 2.0, "AngularVelocityUnit.RADIAN_PER_SECOND", "2.0rad/s", 
37                  10.0, "AngularVelocityUnit.CENTESIMAL_ARCSECOND_PER_SECOND", "10.0c\\\"/sec"));
38          testRecords.add(new TestRecord("Area", 2.0, "AreaUnit.SQUARE_METER", "2.0m^2", 
39                  1.0, "AreaUnit.ACRE", "1.0ac"));
40          testRecords.add(new TestRecord("CatalyticActivity", 2.0, "CatalyticActivityUnit.KATAL", "2.0kat",
41                  1.0, "CatalyticActivityUnit.MILLIKATAL", "1.0mkat"));
42          testRecords.add(new TestRecord("Density", 2.0, "DensityUnit.KG_PER_METER_3", "2.0kg/m^3", 
43                  1.0, "DensityUnit.GRAM_PER_CENTIMETER_3", "1.0g/cm^3"));
44          testRecords.add(new TestRecord("Dimensionless", 2.0, "DimensionlessUnit.SI", "2.0", 
45                  1.0, "DimensionlessUnit.SI", "1.0"));
46          testRecords.add(new TestRecord("Direction", 2.0, "DirectionUnit.EAST_RADIAN", "2.0rad(E)", 
47                  1.0, "DirectionUnit.NORTH_DEGREE", "1.0deg(N)"));
48          testRecords.add(new TestRecord("Duration", 2.0, "DurationUnit.SECOND", "2.0s", 
49                  1.0, "DurationUnit.DAY", "1.0day"));
50          testRecords.add(new TestRecord("ElectricalCapacitance", 2.0, "ElectricalCapacitanceUnit.FARAD", "2.0F", 
51                  1.0, "ElectricalCapacitanceUnit.MICROFARAD", "1.0uF"));
52          testRecords.add(new TestRecord("ElectricalCharge", 2.0, "ElectricalChargeUnit.COULOMB", "2.0C", 
53                  1.0, "ElectricalChargeUnit.MILLIAMPERE_HOUR", "1.0mAh"));
54          testRecords.add(new TestRecord("ElectricalConductance", 2.0, "ElectricalConductanceUnit.SIEMENS", "2.0S", 
55                  1.0, "ElectricalConductanceUnit.MILLISIEMENS", "1.0mS"));
56          testRecords.add(new TestRecord("ElectricalCurrent", 2.0, "ElectricalCurrentUnit.AMPERE", "2.0A", 
57                  1.0, "ElectricalCurrentUnit.STATAMPERE", "1.0statA"));
58          testRecords.add(new TestRecord("ElectricalInductance", 2.0, "ElectricalInductanceUnit.HENRY", "2.0H", 
59                  1.0, "ElectricalInductanceUnit.MILLIHENRY", "1.0mH"));
60          testRecords.add(new TestRecord("ElectricalPotential", 2.0, "ElectricalPotentialUnit.VOLT", "2.0V", 
61                  1.0, "ElectricalPotentialUnit.ABVOLT", "1.0abV"));
62          testRecords.add(new TestRecord("ElectricalResistance", 2.0, "ElectricalResistanceUnit.OHM", "2.0ohm", 
63                  1.0, "ElectricalResistanceUnit.STATOHM", "1.0stohm"));
64          testRecords.add(new TestRecord("Energy", 2.0, "EnergyUnit.JOULE", "2.0J", 
65                  1.0, "EnergyUnit.KILOWATT_HOUR", "1.0kWh"));
66          testRecords.add(new TestRecord("EquivalentDose", 2.0, "EquivalentDoseUnit.SIEVERT", "2.0Sv", 
67                  1.0, "EquivalentDoseUnit.REM", "1.0rem"));
68          testRecords.add(new TestRecord("FlowMass", 2.0, "FlowMassUnit.KILOGRAM_PER_SECOND", "2.0kg/s", 
69                  1.0, "FlowMassUnit.POUND_PER_SECOND", "1.0lb/s"));
70          testRecords.add(new TestRecord("FlowVolume", 2.0, "FlowVolumeUnit.CUBIC_METER_PER_SECOND", "2.0m^3/s", 
71                  1.0, "FlowVolumeUnit.GALLON_US_PER_DAY", "1.0gal(US)/day"));
72          testRecords.add(new TestRecord("Force", 2.0, "ForceUnit.NEWTON", "2.0N", 
73                  1.0, "ForceUnit.KILOGRAM_FORCE", "1.0kgf"));
74          testRecords.add(new TestRecord("Frequency", 2.0, "FrequencyUnit.PER_SECOND", "2.0/s", 
75                  1.0, "FrequencyUnit.KILOHERTZ", "1.0kHz"));
76          testRecords.add(new TestRecord("Illuminance", 2.0, "IlluminanceUnit.LUX", "2.0lx", 
77                  1.0, "IlluminanceUnit.NOX", "1.0nx"));
78          testRecords.add(new TestRecord("Length", 2.0, "LengthUnit.METER", "2.0m", 
79                  1.0, "LengthUnit.INCH", "1.0in"));
80          testRecords.add(new TestRecord("LinearDensity", 2.0, "LinearDensityUnit.PER_METER", "2.0/m", 
81                  1.0, "LinearDensityUnit.PER_YARD", "1.0/yd"));
82          testRecords.add(new TestRecord("LuminousFlux", 2.0, "LuminousFluxUnit.LUMEN", "2.0lm", 
83                  1.0, "LuminousFluxUnit.LUMEN", "1.0srcd"));
84          testRecords.add(new TestRecord("LuminousIntensity", 2.0, "LuminousIntensityUnit.CANDELA", "2.0cd", 
85                  1.0, "LuminousIntensityUnit.SI", "1.0cd"));
86          testRecords.add(new TestRecord("MagneticFlux", 2.0, "MagneticFluxUnit.WEBER", "2.0Wb", 
87                  1.0, "MagneticFluxUnit.MAXWELL", "1.0Mx"));
88          testRecords.add(new TestRecord("MagneticFluxDensity", 2.0, "MagneticFluxDensityUnit.TESLA", "2.0T", 
89                  1.0, "MagneticFluxDensityUnit.GAUSS", "1.0G"));
90          testRecords.add(new TestRecord("Mass", 2.0, "MassUnit.KILOGRAM", "2.0kg", 
91                  1.0, "MassUnit.GIGAELECTRONVOLT", "1.0GeV"));
92          testRecords.add(new TestRecord("Momentum", 2.0, "MomentumUnit.SI", "2.0kgm/s", 
93                  1.0, "MomentumUnit.KILOGRAM_METER_PER_SECOND", "1.0kgm/sec"));
94          testRecords.add(new TestRecord("Position", 2.0, "PositionUnit.ASTRONOMICAL_UNIT", "2.0AU", 
95                  1.0, "PositionUnit.DECIMETER", "1.0dm"));
96          testRecords.add(new TestRecord("Power", 2.0, "PowerUnit.WATT", "2.0W", 
97                  1.0, "PowerUnit.FOOT_POUND_FORCE_PER_HOUR", "1.0ft.lbf/h"));
98          testRecords.add(new TestRecord("Pressure", 2.0, "PressureUnit.PASCAL", "2.0Pa", 
99                  1.0, "PressureUnit.MILLIMETER_MERCURY", "1.0mmHg"));
100         testRecords.add(new TestRecord("RadioActivity", 2.0, "RadioActivityUnit.BECQUEREL", "2.0Bq", 
101                 1.0, "RadioActivityUnit.CURIE", "1.0Ci"));
102         testRecords.add(new TestRecord("SolidAngle", 2.0, "SolidAngleUnit.STERADIAN", "2.0sr", 
103                 1.0, "SolidAngleUnit.SQUARE_DEGREE", "1.0sq.deg"));
104         testRecords.add(new TestRecord("Speed", 2.0, "SpeedUnit.METER_PER_SECOND", "2.0m/s", 
105                 1.0, "SpeedUnit.MILE_PER_HOUR", "1.0mi/h"));
106         testRecords.add(new TestRecord("Temperature", 2.0, "TemperatureUnit.KELVIN", "2.0K", 
107                 1.0, "TemperatureUnit.DEGREE_CELSIUS", "1.0degC"));
108         testRecords.add(new TestRecord("Time", 2.0, "TimeUnit.BASE_HOUR", "2.0h", 
109                 1.0, "TimeUnit.EPOCH_DAY", "1.0day"));
110         testRecords.add(new TestRecord("Torque", 2.0, "TorqueUnit.NEWTON_METER", "2.0N.m", 
111                 1.0, "TorqueUnit.POUND_FOOT", "1.0lbf.ft"));
112         testRecords.add(new TestRecord("Volume", 2.0, "VolumeUnit.CUBIC_METER", "2.0m^3", 
113                 1.0, "VolumeUnit.LITER", "1.0L"));
114         // @formatter:on
115     }
116 
117     /**
118      * @param args String[]; blank
119      * @throws IOException on i/o error
120      * @throws URISyntaxException on i/o error
121      */
122     public static void main(String[] args) throws IOException, URISyntaxException
123     {
124         /*-
125          * Create this:
126             @Option(names = {"--acceleration"}, description = "Acceleration", defaultValue = "10.0m/s^2")
127             protected Acceleration acceleration;
128          */
129 
130         for (TestRecord r : testRecords)
131         {
132             // @formatter:off
133             System.out.println("    /** */\n" + 
134                 "    @Option(names = {\"--" + r.type.toLowerCase() + "\"}, description = \"" + r.type + "\", defaultValue = \"" + r.value1 + "\")\n" + 
135                 "    protected " + r.type + " " + r.type.toLowerCase() + ";\n"); 
136             // @formatter:on
137         }
138 
139         System.out.println("\n\n");
140 
141         /*-
142          Create this:
143             args = new String[] {};
144             options = new Options();
145             CliUtil.execute(options, args);
146             assertEquals(new Acceleration(10.0, AccelerationUnit.METER_PER_SECOND_2), options.acceleration);
147             args = new String[] {"--acceleration", "30.0km/h^2"};
148             CliUtil.execute(options, args);
149             assertEquals(new Acceleration(30.0, AccelerationUnit.KM_PER_HOUR_2), options.acceleration);
150          */
151 
152         for (TestRecord r : testRecords)
153         {
154             // @formatter:off
155             System.out.println("args = new String[] {};");
156             System.out.println("options = new Options();");
157             System.out.println("CliUtil.execute(options, args);");
158             System.out.println("assertEquals(new " + r.type + "(" + r.amount1 + ", " + r.unit1 + "), options." + r.type.toLowerCase() + ");"); 
159             System.out.println("args = new String[] {\"--" + r.type.toLowerCase() + "\", \"" + r.value2 + "\"};"); 
160             System.out.println("CliUtil.execute(options, args);"); 
161             System.out.println("assertEquals(new " + r.type + "(" + r.amount2 + ", " + r.unit2 + "), options." + r.type.toLowerCase() + ");\n"); 
162             // @formatter:on
163         }
164     }
165 
166     /** */
167     private static class TestRecord
168     {
169         /** */
170         protected String type;
171 
172         /** */
173         protected double amount1;
174 
175         /** */
176         protected String unit1;
177 
178         /** */
179         protected String value1;
180 
181         /** */
182         protected double amount2;
183 
184         /** */
185         protected String unit2;
186 
187         /** */
188         protected String value2;
189 
190         /**
191          * @param type String; type, e.g. "Length"
192          * @param amount1 double; initial amount, e.g. 10.0
193          * @param unit1 String; initial unit, e.g. LengthUnit.METER
194          * @param value1 String; initial String value, e.g. "10.0m"
195          * @param amount2 double; changed amount, e.g. 5.0
196          * @param unit2 String; changed unit, e.g. LengthUnit.KILOMETER
197          * @param value2 String; changed String amount, e.g. 5.0km
198          */
199         public TestRecord(String type, double amount1, String unit1, String value1, double amount2, String unit2, String value2)
200         {
201             this.type = type;
202             this.amount1 = amount1;
203             this.unit1 = unit1;
204             this.value1 = value1;
205             this.amount2 = amount2;
206             this.unit2 = unit2;
207             this.value2 = value2;
208         }
209     }
210 }