1 package org.djunits.value.vdouble.scalar;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.fail;
5
6 import java.lang.reflect.Constructor;
7 import java.lang.reflect.InvocationTargetException;
8 import java.lang.reflect.Method;
9
10 import org.djunits.unit.DurationUnit;
11 import org.djunits.unit.Unit;
12 import org.djunits.value.CLASSNAMES;
13 import org.djunits.value.base.Scalar;
14 import org.djunits.value.vdouble.scalar.base.DoubleScalar;
15 import org.junit.jupiter.api.Test;
16
17
18
19
20
21
22
23
24
25
26
27
28 public class DoubleValueOfStringOfTest
29 {
30
31
32
33 @Test
34 public final void durationValueOfTest()
35 {
36 Duration duration = new Duration(10.0, DurationUnit.MINUTE);
37 assertEquals("10.0000000 min", duration.toString());
38 assertEquals(duration, Duration.valueOf(duration.toString()));
39 }
40
41
42
43
44 @Test
45 public final void valueOfDoubleTest()
46 {
47 for (String className : CLASSNAMES.ALL_NODIM_LIST)
48 {
49
50 Class<?> scalarClass = null;
51 String classPath = "org.djunits.value.vdouble.scalar." + className;
52 try
53 {
54 scalarClass = Class.forName(classPath);
55 }
56 catch (ClassNotFoundException exception)
57 {
58 fail("Class not found for Scalar class " + classPath);
59 }
60
61
62 Method ofSIMethod = null;
63 try
64 {
65 ofSIMethod = scalarClass.getMethod("ofSI", double.class);
66 }
67 catch (NoSuchMethodException | SecurityException exception)
68 {
69 fail("Method ofSI not found for Scalar class " + classPath);
70 }
71 Scalar<?, ?> scalarSI = null;
72 try
73 {
74 scalarSI = (Scalar<?, ?>) ofSIMethod.invoke(scalarClass, Double.valueOf(10.0));
75 }
76 catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception)
77 {
78 fail("Method ofSI failed for Scalar class " + classPath);
79 }
80
81
82 Unit<?> unitSI = scalarSI.getDisplayUnit();
83
84
85 Constructor<?> constructScalar = null;
86 try
87 {
88 constructScalar = scalarClass.getConstructor(double.class, unitSI.getClass());
89 }
90 catch (NoSuchMethodException | SecurityException exception)
91 {
92 fail("Constructor for unit " + unitSI.getClass().getName() + " not found for Scalar class " + classPath);
93 }
94
95
96 for (Unit<?> unit : unitSI.getQuantity().getUnitsById().values())
97 {
98 DoubleScalar<?, ?> scalar = null;
99 try
100 {
101 scalar = (DoubleScalar<?, ?>) constructScalar.newInstance(Double.valueOf(1.0), unit);
102 assertEquals(1.0, scalar.getInUnit(), 0.01);
103 }
104 catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
105 | InstantiationException exception)
106 {
107 fail("Construct with unit " + unit.getClass().getName() + " failed for Scalar class " + classPath);
108 }
109 }
110 }
111 }
112 }