36
36
import java .util .Random ;
37
37
38
38
import org .junit .jupiter .api .Test ;
39
+ import org .junit .jupiter .api .function .Executable ;
39
40
40
- import static org .junit .jupiter .api .Assertions .assertEquals ;
41
+ import static org .junit .jupiter .api .Assertions .* ;
41
42
42
43
public class OptionsTest {
43
44
44
- @ Test
45
- public void testBasic () {
46
- String [] args = new String [] {"--deeper-string" ,"How low can you go?" ,
47
- "--deep-string" , "double bass" ,
48
- "--enum" , "THINGS" ,
49
- "--output-string" , "ringstay putoutay" ,
50
- "--seed" , "42" ,
51
- "--baz" , "X,Y,Z" ,
52
- "--output-file" , "/tmp/output.txt" ,
53
- "--input-file" , "/tmp/input.txt" ,
54
- "--pi" , "3.1415927" };
55
- TestOptions options = new TestOptions ();
45
+ @ Test
46
+ public void testBasic () {
47
+ String [] args = new String [] {"--deeper-string" ,"How low can you go?" ,
48
+ "--deep-string" , "double bass" ,
49
+ "--enum" , "THINGS" ,
50
+ "--output-string" , "ringstay putoutay" ,
51
+ "--seed" , "42" ,
52
+ "--baz" , "X,Y,Z" ,
53
+ "--output-file" , "/tmp/output.txt" ,
54
+ "--input-file" , "/tmp/input.txt" ,
55
+ "--pi" , "3.1415927" };
56
+ TestOptions options = new TestOptions ();
56
57
ConfigurationManager cm = new ConfigurationManager (args ,options );
57
58
58
59
assertEquals (3.1415927d , options .pi , 0.00001 );
@@ -69,18 +70,18 @@ public void testBasic() {
69
70
String [] unparsedArgs = cm .getUnnamedArguments ();
70
71
assertEquals (0 , unparsedArgs .length );
71
72
72
- }
73
-
74
- @ Test
75
- public void testBackSlash () {
73
+ }
74
+
75
+ @ Test
76
+ public void testBackSlash () {
76
77
String deeperStringValue = ConfigurationManager .IS_WINDOWS ? "\\ s+" : "\\ \\ s+" ;
77
- String [] args = new String [] {"--deeper-string" , deeperStringValue };
78
- TestOptions options = new TestOptions ();
78
+ String [] args = new String [] {"--deeper-string" , deeperStringValue };
79
+ TestOptions options = new TestOptions ();
79
80
ConfigurationManager cm = new ConfigurationManager (args ,options );
80
81
assertEquals ("\\ s+" , options .bar .deepOptions .deeperOptions .deeperString );
81
82
cm .close ();
82
83
83
- }
84
+ }
84
85
85
86
public static class CommaOptions implements Options {
86
87
@ Option (longName ="my-chars" ,usage ="The characters." )
@@ -99,6 +100,58 @@ public void testComma() {
99
100
assertEquals ('b' , options .myChars [2 ]);
100
101
assertEquals ('c' , options .myChars [3 ]);
101
102
}
103
+
104
+ private static class PrivateClassOptions implements Options {
105
+ @ Option (charName ='s' ,longName ="something" ,usage ="Provide something" )
106
+ public String s ;
107
+ }
108
+
109
+ public static class PrivateConstructorOptions implements Options {
110
+ private PrivateConstructorOptions () {
111
+ }
112
+
113
+ @ Option (charName ='s' ,longName ="something" ,usage ="Provide something" )
114
+ public String s ;
115
+ }
116
+
117
+ public static class NoDefaultConstructorOptions implements Options {
118
+ public NoDefaultConstructorOptions (String aValue ) {
119
+ }
120
+
121
+ @ Option (charName ='s' ,longName ="something" ,usage ="Provide something" )
122
+ public String s ;
123
+ }
124
+
125
+ @ Test
126
+ public void testAccess () {
127
+ PrivateClassOptions one = new PrivateClassOptions ();
128
+ Exception e = assertThrows (ArgumentException .class , new Executable () {
129
+ @ Override
130
+ public void execute () throws Throwable {
131
+ ConfigurationManager cm = new ConfigurationManager (new String []{"-s" , "donkey" }, one );
132
+ }
133
+ });
134
+ assertTrue (e .getMessage ().contains ("must be public" ));
135
+
136
+ PrivateConstructorOptions two = new PrivateConstructorOptions ();
137
+ e = assertThrows (ArgumentException .class , new Executable () {
138
+ @ Override
139
+ public void execute () throws Throwable {
140
+ ConfigurationManager cm = new ConfigurationManager (new String []{"-s" , "donkey" }, two );
141
+ }
142
+ });
143
+ assertTrue (e .getMessage ().contains ("default constructor must be public" ));
144
+
145
+ NoDefaultConstructorOptions three = new NoDefaultConstructorOptions ("donkey" );
146
+ e = assertThrows (ArgumentException .class , new Executable () {
147
+ @ Override
148
+ public void execute () throws Throwable {
149
+ ConfigurationManager cm = new ConfigurationManager (new String []{"-s" , "donkey" }, three );
150
+ }
151
+ });
152
+ assertTrue (e .getMessage ().contains ("no default constructor" ));
153
+
154
+ }
102
155
}
103
156
104
157
class TestOptions implements Options {
0 commit comments