1
//*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
10
* Sun Industry Standards Source License Version 1.2
11
* =================================================
12
* The contents of this file are subject to the Sun Industry Standards
13
* Source License Version 1.2 (the "License"); You may not use this file
14
* except in compliance with the License. You may obtain a copy of the
15
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
17
* Software provided under this License is provided on an "AS IS" basis,
18
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
19
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
20
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
21
* See the License for the specific provisions governing your rights and
22
* obligations concerning the Software.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
33
package com.sun.grid.jgdi.util.shell.editor;
35
import java.util.Arrays;
36
import java.util.Collections;
37
import java.util.Comparator;
38
import junit.framework.*;
39
import com.sun.grid.jgdi.configuration.*;
40
import java.util.ArrayList;
41
import java.util.HashMap;
42
import java.util.Iterator;
43
import java.util.List;
49
public class GEObjectEditorTest extends TestCase {
50
private TestGEObject geObj;
52
private static String HGRP_1 = "@hostgroupA";
53
private static String HGRP_2 = "@hostgroupB";
57
private String[] passStringOnlyValues =
64
private String[] passStringValues =
71
private String[] failStringValues =
73
"value with spaces", //" " is a separator => 3 values
74
"\"long quote\"" //" " is a separator => 2 values
78
private String[] passLongValues =
85
String.valueOf(Long.MAX_VALUE),
86
//TODO LP All negative values should probably fail?
87
String.valueOf(Long.MIN_VALUE)
89
private String[] failLongValues =
97
"10000000000000000000000000000000.11",
102
private String[] passDoubleValues =
105
" 004.61656516516516514456564654564564654654654654465654564564 ",
106
"123456789123456789.12345687",
108
String.valueOf(Double.MAX_VALUE),
109
//TODO LP All negative values should probably fail?
110
String.valueOf(Double.MIN_VALUE)
112
private String[] failDoubleValues =
122
"90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.11,123321"
126
private static Map passStringListValues;
128
passStringListValues = new HashMap();
129
passStringListValues.put("*/.\\ nb`3432'", Arrays.asList(new Object[] {"*/.\\", "nb`3432'"}));
130
passStringListValues.put(" 12 fds", Arrays.asList(new Object[] {"12", "fds"}));
131
passStringListValues.put(" 12 fds", Arrays.asList(new Object[] {"12", "fds"}));
132
passStringListValues.put(",aa aaa", Arrays.asList(new Object[] {"aa", "aaa"}));//TODO LP Or should this be ",aa" "aaa"?
133
passStringListValues.put("aaa,bb b", Arrays.asList(new Object[] {"aaa", "bb", "b"}));
134
passStringListValues.put("\"quote\"", Arrays.asList(new Object[] {"\"quote\""}));
135
passStringListValues.put("value with spaces", Arrays.asList(new Object[] {"value", "with", "spaces"}));
136
passStringListValues.put("\"long quote\"", Arrays.asList(new Object[] {"\"long", "quote\""}));
137
passStringListValues.put("a=b c=d", Arrays.asList(new Object[] {"a=b", "c=d"}));
138
passStringListValues.put("a=b=,c", Arrays.asList(new Object[] {"a=b=", "c"}));
139
passStringListValues.put("a b \n\n", Arrays.asList(new Object[] {"a", "b"}));
142
private static Map failStringListValues;
144
failStringListValues = new HashMap();
145
failStringListValues.put("a b \n c", "Expected at least 2 tokens name and value got: \"c\"");
149
private static Map passStringMapValues;
151
passStringMapValues = new HashMap();
152
HashMap temp = new HashMap();
153
temp.put("@/", "val");
154
passStringMapValues.put("val", temp);
155
passStringMapValues.put("val ", temp);
156
passStringMapValues.put("[@/=val]", temp);
157
passStringMapValues.put("[@/ = val ] ", temp);
159
temp = new HashMap();
160
temp.put("@/", "val");
161
temp.put(HGRP_1, "hval");
162
passStringMapValues.put("val,["+HGRP_1+"=hval]", temp);
163
passStringMapValues.put("val ["+HGRP_1+" = hval]", temp);
164
passStringMapValues.put("[@/ = val] , ["+HGRP_1+" = hval]", temp);
165
passStringMapValues.put("[@/=val] ["+HGRP_1+"= hval]", temp);
167
temp = new HashMap();
168
temp.put("@/", "val");
169
temp.put(HGRP_1, "hval");
170
temp.put(HGRP_2, "hval2");
171
passStringMapValues.put("val,["+HGRP_1+"= hval],["+HGRP_2+"= hval2]", temp);
172
passStringMapValues.put("val ["+HGRP_1+"=hval] ["+HGRP_2+"=hval2]", temp);
173
passStringMapValues.put("val ["+HGRP_1+"=hval],["+HGRP_2+"=hval2]", temp);
174
passStringMapValues.put("val,["+HGRP_1+"=hval] ["+HGRP_2+"=hval2]", temp);
175
passStringMapValues.put("[@/=val],["+HGRP_1+"=hval],["+HGRP_2+"=hval2]", temp);
176
passStringMapValues.put("[@/ =val] ["+HGRP_1+"=hval] ["+HGRP_2+" = hval2]", temp);
177
passStringMapValues.put("[@/= val] ["+HGRP_1+" = hval],["+HGRP_2+"=hval2]", temp);
178
passStringMapValues.put("[@/ = val],["+HGRP_1+"=hval] ["+HGRP_2+"=hval2]", temp);
181
private static Map failStringMapValues;
183
failStringMapValues = new HashMap();
184
failStringMapValues.put("val val2", "Expected [key=val] elements got: [@/=val val2]");
185
failStringMapValues.put("val,val2", "Expected [key=val] elements got: [@/=val val2]");
186
failStringMapValues.put("val = val2", "Expected [key=val] elements got: [@/=val = val2]");
187
failStringMapValues.put("[@/=val val2]", "Expected [key=val] elements got: [@/=val val2]");
188
failStringMapValues.put("[@/=val,val2 ] ", "Expected [key=val] elements got: [@/=val val2]");
189
failStringMapValues.put("[@/=val = val2 ] ", "Expected [key=val] elements got: [@/=val = val2]");
191
failStringMapValues.put("val,["+HGRP_1+"=hval=cont]", "Expected [key=val] elements got: ["+HGRP_1+"=hval=cont]");
192
failStringMapValues.put("val=cont ["+HGRP_1+"=hval]", "Expected [key=val] elements got: [@/=val=cont]");
193
failStringMapValues.put("[@/=val],["+HGRP_1+"=hval=cont]", "Expected [key=val] elements got: ["+HGRP_1+"=hval=cont]");
194
failStringMapValues.put("[@/=val=cont] ["+HGRP_1+" hval]", "Expected [key=val] elements got: [@/=val=cont]");
198
private static Map passStringMapListValues;
200
passStringMapListValues = new HashMap();
201
HashMap keyMap = new HashMap();
202
HashMap valueMap = new HashMap();
203
valueMap.put("key1", "val1");
204
valueMap.put("key2", "val2");
205
valueMap.put("key3", "val3");
206
keyMap.put("@/", valueMap);
207
passStringMapListValues.put("key1=val1 key2=val2 key3=val3", keyMap);
208
passStringMapListValues.put("key1=val1,key2=val2,key3=val3", keyMap);
209
passStringMapListValues.put("key1=val1 , key2=val2 ,key3=val3", keyMap);
210
passStringMapListValues.put("key1=val1 key2=val2 , key3=val3", keyMap);
211
passStringMapListValues.put("key1=val1 ,,,, key2=val2 key3=val3", keyMap);
212
passStringMapListValues.put("[@/=key1=val1 key2=val2 key3=val3]", keyMap);
213
passStringMapListValues.put(" [ @/ = key1=val1, key2=val2 key3=val3 ]", keyMap);
214
passStringMapListValues.put("[@/ =key1=val1, key2=val2 , key3=val3\t]", keyMap);
215
passStringMapListValues.put("[\t@/\t=\tkey1=val1 key2=val2 , key3=val3\t]", keyMap);
216
passStringMapListValues.put("[@/=key1=val1 ,,,, key2=val2 , key3=val3]", keyMap);
218
/* "key=val1 val2 val3,["+HGRP_1+"=key=hval1 hval2 hval3]",
219
"key=val1,val2,val3,["+HGRP_1+"=key=hval1,hval2 hval3]",
220
"key=val1 val2,val3,["+HGRP_1+"=key=hval1 hval2,hval3]",
221
"key=val1 , val2, val3,["+HGRP_1+"=key=hval1 , hval2, hval3]",
222
"key=val1 , val2, ,, , val3,["+HGRP_1+"=key=hval1 , hval2, ,, , ,hval3]",
223
"key=val1 , val2, val3 ["+HGRP_1+"=key=hval1 , hval2, hval3]",
225
"[@/=key=val1 val2 val3]",
226
"[@/=key=val1,val2,val3]",
227
"[@/=key=val1 val2,val3]",
228
"[@/=key=val1 , val2, val3]",
230
"[@/=key=val1 val2 val3],["+HGRP_1+"=key=hval1 hval2 hval3]",
231
"[@/=key=val1,val2,val3],["+HGRP_1+"=key=hval1,hval2 hval3]",
232
"[@/=key=val1 val2,val3],["+HGRP_1+"=key=hval1 hval2,hval3]",
233
"[@/=key=val1 , val2, val3],["+HGRP_1+"=key=hval1 , hval2, hval3]",
234
"[@/=key=val1 , val2, val3,],,[,"+HGRP_1+"=key=hval1, ,, hval2, hval3],",
235
"[@/=key=val1 , val2, val3] ["+HGRP_1+"=key=hval1 , hval2, hval3]",
237
private static Map failStringMapListValues;
239
failStringMapListValues = new HashMap();
240
//failStringMapListValues.put("key val1 val2 val3",""); //Should this really fail
241
failStringMapListValues.put("key=val1,val2,val3=val4", "Expected all elements to be of a same kind. First elements were a Map current element \"val2\" is not.");
242
failStringMapListValues.put("key=val1,val2,val3=val4", "Expected all elements to be of a same kind. First elements were a Map current element \"val2\" is not.");
243
failStringMapListValues.put("key=val1 val2[,val3,", "Expected all elements to be of a same kind. First elements were a Map current element \"val2\" is not.");
244
failStringMapListValues.put("key=val1 , [val2], val3", "Expected [key=val1 val2] elements got: [val2]");
245
failStringMapListValues.put("key=val1 , [val2, val3]", "Expected [key=val1 val2] elements got: [val2 val3]");
248
/* "key=val1=val2 val3,["+HGRP_1+"=key=hval1 hval2= hval3]",
249
"key=val1,val2,val3,["+HGRP_1+"=key=hval1,hval2 hval3]",
250
"key=val1 [val2,val3],["+HGRP_1+"=key=hval1 hval2,hval3]",
251
"key=val1 , val2, val3,[["+HGRP_1+"=key=hval1 , hval2, hval3]]",
252
"key=val1 , val2, val3 ["+HGRP_1+"=key=hval1 , [hval2=hval3]]",
254
"[@/=key=val1 val2=val3]",
256
"[@/=key=val1 val2 val3],["+HGRP_1+"=key=hval1 [hval2 hval3]]",
257
"[@/=key=val1,val2,val3],["+HGRP_1+"=key=hval1,[hval2=hval3]]",
258
"[@/=key=val1 val2,val3],["+HGRP_1+"=key=hval1 hval2,hval3]",
259
"[[@/=key=val1 , val2, val3],["+HGRP_1+"=key=hval1 , hval2, hval3]]",
260
"[[@/=key=val1 , val2, val3] ["+HGRP_1+"=key=hval1 , hval2, hval3]",
264
public GEObjectEditorTest(String testName) {
268
protected synchronized void setUp() throws Exception {
269
geObj = new TestGEObject();
270
Util.addDescriptor(geObj.getClass(), new TestGEObjectDescriptor());
273
protected void tearDown() throws Exception {
278
private void updateSimpleValue(String testName, String[] values, String propertyName, boolean shouldFail) {
279
for (int i=0; i< values.length; i++) {
280
String str = values[i];
281
System.out.print(testName+"(\""+str+"\")");
283
GEObjectEditor.updateObjectWithText(geObj,propertyName+" "+str);
285
System.out.println(" - FAILED");
286
fail("Expecting failure for "+testName+"(\""+str+"\"), but test passed.");
288
simpleAssert(propertyName, str, geObj);
289
System.out.println(" - OK");
290
} catch (Exception ex) {
292
System.out.println(" - FAILED");
293
fail("Got unexpected exception "+ex.getMessage());
295
System.out.println(" - OK "+ex.getMessage());
300
private void simpleAssert(String propertyName, String str, TestGEObject geObj) {
301
if (propertyName.equals("name")) {
302
assertEquals("Setting value \""+str+"\" failed.", str.trim(), geObj.getName());
303
} else if (propertyName.equals("long")) {
304
assertEquals("Setting value \""+str+"\" failed.", Long.parseLong(str.trim()), geObj.getLong());
305
} else if (propertyName.equals("double")) {
306
assertEquals("Setting value \""+str+"\" failed.", Double.parseDouble(str.trim()), geObj.getDouble(),0);
307
} else if (propertyName.equals("string_list")) {
308
assertEquals("Setting value \""+str+"\" failed.", 1, geObj.getStringList().size());
309
assertEquals("Setting value \""+str+"\" failed.", str.trim(), (String)geObj.getStringList().get(0));
310
} else if (propertyName.equals("string_map")) {
311
assertEquals(1, geObj.getStringMapKeys().size());
312
assertEquals("Setting value \""+str+"\" failed.", str.trim(), geObj.getStringMap("@/"));
313
} else if (propertyName.equals("string_map_list")) {
314
assertEquals(1, geObj.getStringMapListKeys().size());
315
assertEquals(1, geObj.getStringMapListList("@/").size());
316
assertEquals("Setting value \""+str+"\" failed.", str.trim(), (String)geObj.getStringMapListList("@/").get(0));
318
throw new IllegalArgumentException("simpleAssert: Unknown attribute \""+propertyName+"\"");
323
public void testPassString() {
324
updateSimpleValue("testPassString", passStringOnlyValues, "name", false);
325
updateSimpleValue("testPassString", passStringValues, "name", false);
328
public void testPassStringWithLongValues() {
329
updateSimpleValue("testPassStringWithLongValues", passLongValues, "name", false);
332
public void testPassStringWithDoubleValues() {
333
updateSimpleValue("testPassStringWithDoubleValues", passDoubleValues, "name", false);
336
public void testFailString() {
337
for (int i=0; i< failStringValues.length; i++) {
338
String str = failStringValues[i];
339
System.out.print("testFailString(\""+str+"\") - ");
341
GEObjectEditor.updateObjectWithText(geObj,"name "+str);
342
System.out.println("FAILED");
343
fail("Setting value \""+str+"\" should have failed.");
344
} catch (IllegalArgumentException ex) {
345
System.out.println("OK "+ex.getMessage());
352
public void testPassLong() {
353
updateSimpleValue("testPassLong", passLongValues, "long", false);
356
public void testFailLongWithDouble() {
357
updateSimpleValue("testFailLongWithDouble", passDoubleValues, "long", true);
360
public void testFailLongWithString() {
361
updateSimpleValue("testFailLongWithString", passStringValues, "long", true);
364
public void testFailLong() {
365
for (int i=0; i< failLongValues.length; i++) {
366
String str = failLongValues[i];
367
System.out.print("testFailLong(\""+str+"\") - ");
369
GEObjectEditor.updateObjectWithText(geObj,"long "+str);
370
System.out.println("FAILED");
371
fail("Setting value \""+str+"\" should have failed.");
372
} catch (IllegalArgumentException ex) {
373
System.out.println("OK got: "+ex.getMessage());
380
public void testPassDouble() {
381
updateSimpleValue("testPassDouble", passDoubleValues, "double", false);
384
public void testPassDoubleWithLong() {
385
updateSimpleValue("testPassDoubleWithLong", passLongValues, "double", false);
388
public void testFailDoubleWithString() {
389
updateSimpleValue("testPassDoubleWithLong", passStringValues, "double", true);
392
public void testFailDouble() {
393
for (int i=0; i< failDoubleValues.length; i++) {
394
String str = failDoubleValues[i];
395
System.out.print("testFailDouble(\""+str+"\") - ");
397
GEObjectEditor.updateObjectWithText(geObj,"double "+str);
398
System.out.println("FAILED");
399
fail("Setting value \""+str+"\" should have failed.");
400
} catch (IllegalArgumentException ex) {
401
System.out.println("OK got: "+ex.getMessage());
407
public void testPassStringList() {
408
for (Iterator iter = passStringListValues.keySet().iterator(); iter.hasNext();) {
409
String str = (String)iter.next();
410
System.out.println("testPassStringList(\""+str+"\")");
411
GEObjectEditor.updateObjectWithText(geObj,"string_list "+str);
412
assertEquals("Setting value \""+str+"\" failed.", (List)passStringListValues.get(str), geObj.getStringList());
416
public void testPassStringListWithLong() {
417
updateSimpleValue("testPassStringListWithLong", passLongValues, "string_list", false);
420
public void testPassStringListWithDouble() {
421
updateSimpleValue("testPassStringListWithDouble", passDoubleValues, "string_list", false);
424
public void testPassStringListWithString() {
425
updateSimpleValue("testPassStringListWithString", passStringValues, "string_list", false);
428
public void testFailStringList() {
429
for (Iterator iter = failStringListValues.keySet().iterator(); iter.hasNext();) {
430
String str = (String)iter.next();
431
System.out.print("testFailStringList(\""+str+"\") - ");
433
GEObjectEditor.updateObjectWithText(geObj,"string_list "+str);
434
System.out.println("FAILED");
435
fail("Setting value \""+str+"\" should have failed.");
436
} catch (IllegalArgumentException ex) {
437
String msg = ex.getMessage();
438
String exp = (String)failStringListValues.get(str);
439
if (msg.equals(exp)) {
440
System.out.println("OK got: "+msg);
442
System.out.println("FAILED");
443
fail("Expected exception with \""+exp+"\" text got: \""+msg+"\"");
450
public void testPassStringMap() {
451
for (Iterator iter = passStringMapValues.keySet().iterator(); iter.hasNext();) {
452
String str = (String)iter.next();
453
System.out.println("testPassStringMap(\""+str+"\")");
454
GEObjectEditor.updateObjectWithText(geObj,"string_map "+str);
455
Map valMap = (HashMap)passStringMapValues.get(str);
456
assertEquals(valMap.keySet().size(), geObj.getStringMapKeys().size());
457
for (Iterator iter2 = valMap.keySet().iterator(); iter2.hasNext();) {
458
String key = (String)iter2.next();
459
assertEquals("Setting value \""+str+"\" failed.", valMap.get(key), geObj.getStringMap(key));
464
public void testPassStringMapWithLong() {
465
updateSimpleValue("testPassStringMapWithLong", passLongValues, "string_map", false);
468
public void testPassStringMapWithDouble() {
469
updateSimpleValue("testPassStringMapWithDouble", passDoubleValues, "string_map", false);
472
public void testPassStringMapWithString() {
473
updateSimpleValue("testPassStringMapWithString", passStringValues, "string_map", false);
476
public void testFailStringMap() {
477
for (Iterator iter = failStringMapValues.keySet().iterator(); iter.hasNext();) {
478
String str = (String)iter.next();
479
System.out.print("testFailStringMap(\""+str+"\") - ");
481
GEObjectEditor.updateObjectWithText(geObj,"string_map "+str);
482
System.out.println("FAILED");
483
fail("Setting value \""+str+"\" should have failed.");
484
} catch (IllegalArgumentException ex) {
485
String msg = ex.getMessage();
486
String exp = (String)failStringMapValues.get(str);
487
if (msg.equals(exp)) {
488
System.out.println("OK got: "+msg);
490
System.out.println("FAILED");
491
fail("Expected exception with \""+exp+"\" text got: \""+msg+"\"");
498
public void testPassStringMapList() {
499
for (Iterator iter = passStringMapListValues.keySet().iterator(); iter.hasNext();) {
500
String str = (String)iter.next();
501
System.out.print("testPassStringMap(\""+str+"\")");
502
GEObjectEditor.updateObjectWithText(geObj,"string_map_list "+str);
503
Map valMap = (HashMap)passStringMapListValues.get(str);
504
assertEquals(valMap.keySet().size(), geObj.getStringMapListKeys().size());
505
for (Iterator iter2 = valMap.keySet().iterator(); iter2.hasNext();) {
506
String key = (String)iter2.next();
507
List list = new ArrayList();
508
Map m = (HashMap)valMap.get(key);
509
for (Iterator iter3 = m.keySet().iterator(); iter3.hasNext();) {
510
String k = (String)iter3.next();
511
list.add(k+"="+m.get(k));
513
Collections.sort(list, new Comparator() {
514
public int compare(Object o1, Object o2) {
515
if (o1 == null && o2 == null) {
524
return ((String)o1).compareTo((String)o2);
527
assertEquals("Setting value \""+str+"\" failed.", list, geObj.getStringMapListList(key));
532
public void testPassStringMapListWithLong() {
533
updateSimpleValue("testFailStringMapListWithLong", passLongValues, "string_map_list", false);
536
public void testPassStringMapListWithDouble() {
537
updateSimpleValue("testFailStringMapListWithDouble", passDoubleValues, "string_map_list", false);
540
public void testPassStringMapListWithString() {
541
updateSimpleValue("testFailStringMapListWithString", passStringValues, "string_map_list", false);
544
public void testFailStringMapList() {
545
for (Iterator iter = failStringMapListValues.keySet().iterator(); iter.hasNext();) {
546
String str = (String)iter.next();
547
System.out.print("testFailStringMapList(\""+str+"\") - ");
549
GEObjectEditor.updateObjectWithText(geObj,"string_map_list "+str);
550
System.out.println("FAILED");
551
fail("Setting value \""+str+"\" should have failed.");
552
} catch (IllegalArgumentException ex) {
553
String msg = ex.getMessage();
554
String exp = (String)failStringMapListValues.get(str);
555
if (msg.equals(exp)) {
556
System.out.println("OK got: "+msg);
558
System.out.println("FAILED");
559
fail("Expected exception with \""+exp+"\" text got: \""+msg+"\"");