-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestConfig.java
More file actions
284 lines (257 loc) · 12.9 KB
/
TestConfig.java
File metadata and controls
284 lines (257 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2007, 2008 Munagala V. Ramanath. All rights reserved.
// custom String option checker
class MyChkRelease implements Config.CheckVal<String> {
String value;
public String getValue() { return value; }
public void check( final String v ) throws Exception {
convertCheck( v );
}
public void convertCheck( final String v ) throws Exception {
if ( null == v ) {
value = null; return;
}
final String val = v.trim();
if ( !(val.equals( "gold" ) || val.equals( "beta" )
|| val.equals( "alpha" )) )
throw new Exception( "Invalid value" );
value = v;
}
} // MyChkString
// test Config class
public class TestConfig {
enum Options {HOST, PORT, NUM_PRODUCERS, NUM_QUEUES, NUM_MESSAGES,
RATE, SPEED, DEBUG, RELEASE};
enum Color {RED, BLUE};
public static void main( String args[] ) throws Exception {
// for testing:
// -- error cases, set ERROR to true and uncomment one error case at a
// time in the 'else' part below
// -- normal cases, set ERROR to false and comment out _ALL_ error cases
//
final boolean ERROR = false;
if ( !ERROR ) { // normal case
final Config config = new Config( Options.class,
Config.From.FROM_FILE,
Config.From.FROM_CMDLINE,
Config.From.DYNAMIC );
config.initParam( Options.HOST, "host", String.class,
"localhost",
new Config.CheckString(),
Config.Settable.DYNAMIC,
Config.Importance.OPTIONAL );
config.initParam( Options.PORT, "port", Integer.class,
5555,
new Config.Check<Integer>( 1024, 65535 ),
Config.Settable.STATIC,
Config.Importance.REQUIRED );
config.initParam( Options.DEBUG, "debug", Boolean.class,
true,
new Config.CheckBoolean(),
Config.Settable.DYNAMIC
// Importance defaults to OPTIONAL
);
config.initParam( Options.NUM_PRODUCERS, "producers", Byte.class,
new Byte( (byte)10 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
new Byte( (byte)100 ) ),
Config.Settable.STATIC );
config.initParam( Options.NUM_QUEUES, "queues", Short.class,
new Short( (short)20 ),
new Config.Check<Short>( Short.class ),
Config.Settable.STATIC );
config.initParam( Options.NUM_MESSAGES, "messages", Long.class,
new Long( 1000000L ),
new Config.Check<Long>( null,
new Long( 5000000L ) ),
Config.Settable.DYNAMIC );
config.initParam( Options.RATE, "rate", Float.class,
6.825f,
new Config.Check<Float>( Float.class ),
Config.Settable.DYNAMIC );
config.initParam( Options.SPEED, "speed", Double.class,
5.56825e22,
new Config.Check<Double>( Double.class ),
Config.Settable.DYNAMIC );
config.initParam( Options.RELEASE, "release", String.class,
"alpha",
new MyChkRelease(),
Config.Settable.STATIC,
Config.Importance.REQUIRED );
System.out.println( "After initialization:" );
config.dump();
// parse properties file
config.parseProp( "config.prop" );
System.out.println( "After parsing file:" );
config.dump();
// parse command line arguments
config.parseArgs( args );
System.out.println( "After parsing command line:" );
config.dump();
// test retrieval of individual options
final String host = config.get( Options.HOST );
final Integer port = config.get( Options.PORT );
final Boolean debug = config.get( Options.DEBUG );
final Byte producers = config.get( Options.NUM_PRODUCERS );
final Short queues = config.get( Options.NUM_QUEUES );
final Long messages = config.get( Options.NUM_MESSAGES );
final Float rate = config.get( Options.RATE );
final Double speed = config.get( Options.SPEED );
final String release = config.get( Options.RELEASE );
System.out.format( "Options.HOST --> %s%n"
+ "Options.PORT --> %s%n"
+ "Options.DEBUG --> %s%n"
+ "Options.NUM_PRODUCERS --> %s%n"
+ "Options.NUM_QUEUES --> %s%n"
+ "Options.NUM_MESSAGES --> %s%n"
+ "Options.RATE --> %f%n"
+ "Options.SPEED --> %e%n"
+ "Options.RELEASE --> %s%n",
host, port, debug, producers, queues, messages,
rate, speed, release );
// dynamic options
config.set( Options.NUM_MESSAGES, 99L );
System.out.println( "NUM_MESSAGES is now: "
+ config.get( Options.NUM_MESSAGES ) );
} else { // error cases
// compile time errors
// 1: initialize with a non-enum class
/*
final Config config = new Config( String.class );
*/
// 2: mismatched value and type
/*
final Config config = new Config( Options.class );
config.initParam( Options.HOST, "host", String.class,
1234,
new Config.CheckString(),
Config.Settable.DYNAMIC );
*/
// 3: mismatched checker object and type
/*
final Config config = new Config( Options.class );
config.initParam( Options.HOST, "host", String.class,
"localhost",
new Config.CheckBoolean(),
Config.Settable.DYNAMIC );
*/
// run time errors
// 1: initialize parameter with wrong enumeration
/*
final Config config = new Config( Options.class );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)22 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.DYNAMIC );
*/
// 2: dynamic option with static config
/*
final Config config = new Config( Options.class );
config.initParam( Options.PORT, "port", Byte.class,
new Byte ( (byte)22 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.DYNAMIC );
*/
// 3: attempt to initialize from file with configuration not
// initialized to permit it.
//
/*
final Config config = new Config( Options.class );
config.initParam( Options.PORT, "port", Byte.class,
new Byte ( (byte)22 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.STATIC );
config.parseProp( "config.prop" );
*/
// 4: attempt to initialize from commandline with configuration not
// initialized to permit it.
//
/*
final Config config = new Config( Color.class,
Config.From.FROM_FILE );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)22 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.STATIC );
config.initParam( Color.BLUE, "red", Boolean.class,
false,
new Config.CheckBoolean(),
Config.Settable.STATIC );
config.parseProp( "config.prop" );
config.parseArgs( args );
*/
// 5: attempt to initialize from command line before file
/*
final Config config = new Config( Color.class,
Config.From.FROM_FILE,
Config.From.FROM_CMDLINE );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)22 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.STATIC );
config.initParam( Color.BLUE, "red", Boolean.class,
false,
new Config.CheckBoolean(),
Config.Settable.STATIC );
config.parseArgs( args );
config.parseProp( "config.prop" );
*/
// 6: attempt to initialize with a value outside permitted range
/*
final Config config = new Config( Color.class,
Config.From.FROM_FILE );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)0 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.STATIC );
config.parseProp( "config.prop" );
*/
// 7: attempt to dynamically initialize a parameter that does not
// allow it
//
/*
final Config config = new Config( Color.class,
Config.From.DYNAMIC );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)8 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.STATIC );
config.set( Color.RED, (byte)99 );
*/
// 8: attempt to initialize same parameter more than once
//
/*
final Config config = new Config( Color.class,
Config.From.DYNAMIC );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)8 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.STATIC );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)9 ),
new Config.Check<Byte>( new Byte( (byte)2 ),
new Byte( (byte)16 ) ),
Config.Settable.STATIC );
*/
// 9: attempt to read from file before initializing all parameters
//
/*
final Config config = new Config( Color.class,
Config.From.FROM_FILE );
config.initParam( Color.RED, "red", Byte.class,
new Byte ( (byte)8 ),
new Config.Check<Byte>( new Byte( (byte)1 ),
Byte.MAX_VALUE ),
Config.Settable.STATIC );
config.parseProp( "config.prop" );
*/
} // if
} // main
}