001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Serguei S.Zapreyev
020: * @version $Revision$
021: */package java.lang;
022:
023: import java.io.File;
024:
025: import junit.framework.TestCase;
026:
027: /*
028: * Created on 01.31.2006
029: *
030: * This SystemExtensionTest class is used to test the Core API
031: * java.lang.System class
032: *
033: */
034:
035: public class SystemExtensionTest extends TestCase {
036:
037: /**
038: *
039: */
040: public void test_arraycopy_Obj_I_Obj_I_I() {
041: class X {
042: public int fld;
043:
044: public X(int i) {
045: fld = i;
046: }
047: }
048: X ax1[] = new X[] { new X(0), new X(1), new X(2), new X(3),
049: new X(4), new X(5), new X(6), new X(7), new X(8),
050: new X(9) };
051: X ax2[] = new X[20];
052: try {
053: System.arraycopy((Object) ax1, 5, (Object) ax2, 12, 3);
054: assertTrue("Error1", ax2[12].fld == 5 && ax2[13].fld == 6
055: && ax2[14].fld == 7);
056: } catch (Exception e) {
057: fail("Error2: " + e.toString());
058: }
059: System.out.println("test_arraycopy_Obj_I_Obj_I_I");
060: }
061:
062: /**
063: *
064: */
065: public void test_currentTimeMillis_V() {
066: assertTrue("Error1", (System.currentTimeMillis() - System
067: .currentTimeMillis()) <= 0);
068: System.out.println("test_currentTimeMillis_V");
069: }
070:
071: /**
072: *
073: */
074: public void test_exit_I() {
075: //exit(777);
076: System.out.println("test_exit_I");
077: }
078:
079: /**
080: *
081: */
082: public void test_gc() {
083: long r1 = Runtime.getRuntime().freeMemory();
084: long r2, r4;
085: String[] sa = new String[(int) r1 / 50000];
086: int ind1 = 0;
087: try {
088: String stmp = "";
089: for (int ind2 = 0; ind2 < 50/*1000*/; ind2++) {
090: stmp += "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
091: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
092: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
093: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
094: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
095: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
096: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
097: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
098: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
099: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
100: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
101: }
102: for (ind1 = 0; ind1 < (int) r1 / 50000; ind1++) {
103: sa[ind1] = "" + stmp;
104: }
105:
106: r2 = Runtime.getRuntime().freeMemory();
107: for (ind1 = 0; ind1 < (int) r1 / 50000; /*ind1++*/ind1 += 100) {
108: sa[ind1] = null;
109: System.gc();
110: try {
111: Thread.sleep(10);
112: } catch (Exception e) {
113: }
114: }
115: sa = null;
116: try {
117: Thread.sleep(500);
118: } catch (Exception e) {
119: }
120: System.gc();
121: try {
122: Thread.sleep(500);
123: } catch (Exception e) {
124: }
125: r4 = Runtime.getRuntime().freeMemory();
126:
127: if (!(r4 > r2)) {
128: System.out
129: .println("WARNNING: It would be better if System.gc method could initiate garbage collecting! (Here we have "
130: + r4 + " !> " + r2 + " .)");
131: }
132: } catch (OutOfMemoryError e) {
133: System.out
134: .println("WARNNING: test did not check System.gc method due to the technical reason !");
135: } catch (Exception e) {
136: fail("Error1: " + e.toString());
137: }
138: System.out.println("test_gc");
139: }
140:
141: /**
142: * See SystemTest.testGetenvString()
143: */
144: //public void test_getenv_Str() {
145: //}
146: /**
147: *
148: */
149: public void test_getProperties_V() {
150: try {
151: java.util.Properties pl = System.getProperties();
152: for (java.util.Enumeration e = pl.propertyNames(); e
153: .hasMoreElements();) {
154: if (((String) e.nextElement()).equals("os.name")) {
155: System.out.println("test_getProperties_V");
156: return;
157: }
158: }
159: fail("Error1");
160: } catch (Exception e) {
161: fail("Error2: " + e.toString());
162: }
163: }
164:
165: /**
166: *
167: */
168: public void test_getProperty_Str() {
169: try {
170: if (System.getProperty("java.version") != null
171: || System.getProperty("java.vendor") != null
172: || System.getProperty("java.vendor.url") != null
173: || System.getProperty("java.home") != null
174: || System
175: .getProperty("java.vm.specification.version") != null
176: || System
177: .getProperty("java.vm.specification.vendor") != null
178: || System.getProperty("java.vm.specification.name") != null
179: || System.getProperty("java.vm.version") != null
180: || System.getProperty("java.vm.vendor") != null
181: || System.getProperty("java.vm.name") != null
182: || System.getProperty("java.specification.version") != null
183: || System.getProperty("java.specification.vendor") != null
184: || System.getProperty("java.specification.name") != null
185: || System.getProperty("java.class.version") != null
186: || System.getProperty("java.class.path") != null
187: || System.getProperty("java.library.path") != null
188: || System.getProperty("java.io.tmpdir") != null
189: || System.getProperty("java.compiler") != null
190: || System.getProperty("java.ext.dirs") != null
191: || System.getProperty("os.name") != null
192: || System.getProperty("os.arch") != null
193: || System.getProperty("os.version") != null
194: || System.getProperty("file.separator") != null
195: || System.getProperty("path.separator") != null
196: || System.getProperty("line.separator") != null
197: || System.getProperty("user.name") != null
198: || System.getProperty("user.home") != null
199: || System.getProperty("user.dir") != null
200: || System.getProperty("os.name") != null) {
201: System.out.println("test_getProperty_Str");
202: return;
203: }
204: fail("Error1");
205: } catch (Exception e) {
206: fail("Error2: " + e.toString());
207: }
208: }
209:
210: /**
211: *
212: */
213: public void test_getProperty_Str_Str() {
214: try {
215: if (System.getProperty(
216: System.getProperty("os.name") + "_UND_"
217: + System.getProperty("user.name"), "ZSS")
218: .equals("ZSS")) {
219: System.out.println("test_getProperty_Str_Str");
220: return;
221: }
222: fail("Error1");
223: } catch (Exception e) {
224: fail("Error2: " + e.toString());
225: }
226: }
227:
228: /**
229: *
230: */
231: public void test_getSecurityManager_V() {
232: assertTrue("Error1", System.getSecurityManager() == null);
233: System.out.println("test_getSecurityManager_V");
234: }
235:
236: /**
237: *
238: */
239: public void test_identityHashCode_Obj() {
240: assertTrue("Error1", System.identityHashCode(null) == 0);
241: Boolean o1 = new Boolean(true);
242: assertTrue("Error2", System.identityHashCode(o1) != o1
243: .hashCode());
244: Byte o2 = new Byte(Byte.MAX_VALUE);
245: assertTrue("Error3", System.identityHashCode(o2) != o2
246: .hashCode());
247: Character o3 = new Character(Character.MAX_VALUE);
248: assertTrue("Error4", System.identityHashCode(o3) != o3
249: .hashCode());
250: Double o4 = new Double(Double.MAX_VALUE);
251: assertTrue("Error5", System.identityHashCode(o4) != o4
252: .hashCode());
253: Float o5 = new Float(Float.MAX_VALUE);
254: assertTrue("Error6", System.identityHashCode(o5) != o5
255: .hashCode());
256: Integer o6 = new Integer(Integer.MAX_VALUE);
257: assertTrue("Error7", System.identityHashCode(o6) != o6
258: .hashCode());
259: Long o7 = new Long(Long.MAX_VALUE);
260: assertTrue("Error8", System.identityHashCode(o7) != o7
261: .hashCode());
262: Short o8 = new Short(Short.MAX_VALUE);
263: assertTrue("Error9", System.identityHashCode(o8) != o8
264: .hashCode());
265: System.out.println("test_identityHashCode_Obj");
266: }
267:
268: /**
269: *
270: */
271: public void test_load_Str() {
272: String jLP = null;
273:
274: String jlp = System.getProperty("java.library.path");
275: String vblp = System.getProperty("vm.boot.library.path");
276: jLP = (jlp != null && jlp.length() != 0 ? jlp : "")
277: + (vblp != null && vblp.length() != 0 ? File.pathSeparator
278: + vblp
279: : "");
280:
281: if (jLP.length() == 0) {
282: System.out
283: .println("WARNNING: test didn't check the loading process.");
284: return;
285: }
286: String[] paths = jLP.split(File.pathSeparator);
287: String ext = (System.getProperty("os.name").indexOf("indows") != -1 ? ".dll"
288: : ".so");
289: int ind1;
290: int ind2;
291: File[] asf = null;
292: for (ind1 = 0; ind1 < paths.length; ind1++) {
293: asf = new java.io.File(paths[ind1]).listFiles();
294: if (asf != null) {
295: for (ind2 = 0; ind2 < asf.length; ind2++) {
296: if (asf[ind2].getName().indexOf(ext) != -1) {
297: try {
298: System.load(asf[ind2].getCanonicalPath());
299: System.out.println("test_load_Str");
300: return;
301: } catch (UnsatisfiedLinkError e) {
302: continue;
303: } catch (Throwable e) {
304: continue;
305: }
306: }
307: }
308: }
309: }
310: fail("System.load method should provide loading a dynamic library!");
311: }
312:
313: /**
314: *
315: */
316: public void test_loadLibrary_Str() {
317: String jLP = null;
318:
319: String jlp = System.getProperty("java.library.path");
320: String vblp = System.getProperty("vm.boot.library.path");
321: jLP = (jlp != null && jlp.length() != 0 ? jlp : "")
322: + (vblp != null && vblp.length() != 0 ? File.pathSeparator
323: + vblp
324: : "");
325: if (jLP.length() == 0) {
326: System.out
327: .println("WARNNING: test didn't check the loading process.");
328: return;
329: }
330: String[] paths = jLP.split(File.pathSeparator);
331: String ext = (System.getProperty("os.name").indexOf("indows") != -1 ? ".dll"
332: : ".so");
333: int ind1;
334: int ind2;
335: File[] asf = null;
336: for (ind1 = 0; ind1 < paths.length; ind1++) {
337: if (paths[ind1] == null) {
338: continue;
339: }
340: asf = new java.io.File(paths[ind1]).listFiles();
341: if (asf != null) {
342: for (ind2 = 0; ind2 < asf.length; ind2++) {
343: if (asf[ind2].getName().indexOf(ext) != -1) {
344: String libName = asf[ind2].getName();
345: if (ext.equals(".dll")) {
346: libName = libName.substring(0, libName
347: .length() - 4);
348: } else {
349: libName = libName.substring(3, libName
350: .length() - 3);
351: }
352: try {
353: System.loadLibrary(libName);
354: System.out.println("test_loadLibrary_Str");
355: return;
356: } catch (UnsatisfiedLinkError e) {
357: continue;
358: } catch (Throwable e) {
359: continue;
360: }
361:
362: }
363: }
364: }
365: }
366: fail("System.loadLibrary method should provide loading a dynamic library!");
367: }
368:
369: /**
370: *
371: */
372: public void test_mapLibraryName_Str() {
373: assertTrue("Error1", System.mapLibraryName("lib").indexOf(
374: (System.getProperty("os.name").toLowerCase().indexOf(
375: "windows") != -1 ? ".dll" : ".so")) != -1);
376: System.out.println("test_mapLibraryName_Str");
377: }
378:
379: /**
380: *
381: */
382: static class forInternalUseOnly {
383: String stmp;
384:
385: forInternalUseOnly() {
386: this .stmp = "";
387: for (int ind2 = 0; ind2 < 100; ind2++) {
388: this .stmp += "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
389: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
390: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
391: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
392: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
393: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
394: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
395: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
396: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
397: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
398: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
399: }
400: }
401:
402: protected void finalize() throws Throwable {
403: runFinalizationFlag = true;
404: super .finalize();
405: }
406: }
407:
408: static boolean runFinalizationFlag = false;
409:
410: public void test_runFinalization_V() {
411: runFinalizationFlag = false;
412: for (int ind2 = 0; ind2 < 10; ind2++) {
413: forInternalUseOnly ins = new forInternalUseOnly();
414: ins.stmp += "";
415: ins = null;
416: try {
417: Thread.sleep(10);
418: } catch (Exception e) {
419: }
420: System.gc();
421: try {
422: Thread.sleep(10);
423: } catch (Exception e) {
424: }
425: System.runFinalization();
426: }
427: assertTrue("ERROR1: someting's wrong", runFinalizationFlag);
428: System.out.println("test_runFinalization_V");
429: }
430:
431: /**
432: *
433: */
434: public void test_runFinalizersOnExit_Z() {
435: runFinalizationFlag = false;
436: for (int ind2 = 0; ind2 < 5; ind2++) {
437: System.runFinalizersOnExit(false);
438: forInternalUseOnly ins = new forInternalUseOnly();
439: ins.stmp += "";
440: ins = null;
441: try {
442: Thread.sleep(10);
443: } catch (Exception e) {
444: }
445: System.gc();
446: try {
447: Thread.sleep(10);
448: } catch (Exception e) {
449: }
450: }
451: assertTrue("ERROR1: someting's wrong", runFinalizationFlag);
452:
453: runFinalizationFlag = false;
454: for (int ind2 = 0; ind2 < 5; ind2++) {
455: System.runFinalizersOnExit(true);
456: forInternalUseOnly ins = new forInternalUseOnly();
457: ins.stmp += "";
458: ins = null;
459: try {
460: Thread.sleep(10);
461: } catch (Exception e) {
462: }
463: System.gc();
464: try {
465: Thread.sleep(10);
466: } catch (Exception e) {
467: }
468: }
469: assertTrue("ERROR2: someting's wrong", runFinalizationFlag);
470: System.out.println("test_runFinalizersOnExit_Z");
471: }
472:
473: /**
474: *
475: */
476: public void test_setErr_Pri() {
477: java.io.PrintStream cp = System.err;
478: java.io.ByteArrayOutputStream es = new java.io.ByteArrayOutputStream(
479: 1000);
480: System.setErr(new java.io.PrintStream(es));
481: System.err.print("Serguei S.Zapreyev");
482: System.err.flush();
483: System.setErr(cp);
484: assertTrue("Error1", es.toString()
485: .indexOf("Serguei S.Zapreyev") != -1);
486: System.out.println("test_setErr_Pri");
487: }
488:
489: /**
490: *
491: */
492: public void test_setIn_Inp() {
493: java.io.InputStream cp = System.in;
494: byte ab[] = new byte[300];
495: System.setIn(new java.io.StringBufferInputStream(
496: "Serguei S.Zapreyev"));
497: try {
498: System.in.read(ab, 0, 18);
499: } catch (java.io.IOException _) {
500: System.setIn(cp);
501: fail("Error1");
502: }
503: System.setIn(cp);
504: assertTrue("Error2", new String(ab).toString().indexOf(
505: "Serguei S.Zapreyev") != -1);
506: System.out.println("test_setIn_Inp");
507: }
508:
509: /**
510: *
511: */
512: public void test_setOut_Pri() {
513: java.io.PrintStream cp = System.out;
514: java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream(
515: 1000);
516: System.setOut(new java.io.PrintStream(os));
517: System.out.print("Serguei S.Zapreyev");
518: System.out.flush();
519: System.setOut(cp);
520: assertTrue("Error1", os.toString()
521: .indexOf("Serguei S.Zapreyev") != -1);
522: System.out.println("test_setOut_Pri");
523: }
524:
525: /**
526: *
527: */
528: public void test_setProperties_Pro() {
529: try {
530: java.util.Properties pl = System.getProperties();
531: pl.setProperty("SeStZa", "ZAPREYEV SERGUEI");
532: System.setProperties(pl);
533: if (System.getProperty("SeStZa").equals("ZAPREYEV SERGUEI")) {
534: System.out.println("test_setProperties_Pro");
535: return;
536: }
537: fail("Error1");
538: } catch (Exception e) {
539: fail("Error2: " + e.toString());
540: }
541: }
542:
543: /**
544: *
545: */
546: public void test_setProperty_Str_Str() {
547: try {
548: System.setProperty("ZSS", "Serguei S.Zapreyev");
549: if (System.getProperty("ZSS").equals("Serguei S.Zapreyev")) {
550: System.out.println("test_setProperty_Str_Str");
551: return;
552: }
553: fail("Error1");
554: } catch (Exception e) {
555: fail("Error2: " + e.toString());
556: }
557: }
558:
559: /**
560: *
561: */
562: static String policyFile;
563: static {
564: if (System.getProperty("java.vm.vendor").equals("Intel DRL")) {
565: //policyFile = "drl.policy";
566: policyFile = "java.policy";
567: } else {
568: policyFile = "java.policy";
569: }
570: }
571:
572: // Commented because of the drlvm issue (env.SYSTEMDRIVE)
573: public void te_st_setSecurityManager_Sec() {
574: setAllPermissions();
575: assertTrue("Error1", System.getSecurityManager() != null);
576: setNoPermissions();
577: try {
578: new SystemExtensionTest().test_load_Str();
579: fail("Error2");
580: } catch (SecurityException e) {
581: // correct behaviour
582: } catch (Exception e) {
583: fail("Error3: " + e.toString());
584: }
585: System.setSecurityManager(null);
586: assertTrue("Error4", System.getSecurityManager() == null);
587:
588: File fff = null;
589: try {
590: fff = new File(System.getProperty("user.home")
591: + java.io.File.separator + "." + policyFile
592: + ".copiedBySMT");
593: if (fff.exists()) {
594: File fff3 = new File(System.getProperty("user.home")
595: + java.io.File.separator + "." + policyFile
596: + "");
597: if (fff3.exists()) {
598: fff3.delete();
599: }
600: fff.renameTo(fff3);
601: }
602: } catch (Throwable e) {
603: e.printStackTrace();
604: }
605: System.out.println("test_setSecurityManager_Sec");
606: }
607:
608: /**
609: *
610: */
611: private void setTestPermission0(String perm, String arg1,
612: String arg2, boolean changeSM) {
613: SecurityManager sm = System.getSecurityManager();
614: try {
615: File fff2 = null;
616: java.io.DataOutputStream dos2 = null;
617: try {
618: fff2 = new File(System.getProperty("user.home")
619: + java.io.File.separator + "." + policyFile
620: + "");
621: if (fff2.exists()) {
622: File fff3 = new File(System
623: .getProperty("user.home")
624: + java.io.File.separator
625: + "."
626: + policyFile
627: + ".copiedBySMT");
628: if (fff3.exists()) {
629: fff2.delete();
630: fff2 = new File(System.getProperty("user.home")
631: + java.io.File.separator + "."
632: + policyFile + "");
633: } else {
634: fff2.renameTo(fff3);
635: }
636: }
637: ;
638: fff2.createNewFile();
639: dos2 = new java.io.DataOutputStream(
640: new java.io.FileOutputStream(fff2));
641: } catch (Throwable e) {
642: e.printStackTrace();
643: }
644:
645: try {
646: dos2.writeBytes("grant {\n");
647: String tmpstr = "";
648: if (arg1.length() != 0 || arg2.length() != 0) {
649: if (arg1.length() != 0 && arg2.length() != 0) {
650: tmpstr = " \"" + arg1 + "\", \"" + arg2 + "\"";
651: } else {
652: tmpstr = " \"" + arg1 + arg2 + "\"";
653: }
654: }
655: if (perm != null && perm.length() != 0) {
656: dos2.writeBytes("permission " + perm + tmpstr
657: + ";\n");
658: }
659: if (changeSM) {
660: dos2
661: .writeBytes("permission "
662: + "java.lang.RuntimePermission \"createSecurityManager\";\n");
663: dos2
664: .writeBytes("permission "
665: + "java.lang.RuntimePermission \"setSecurityManager\";\n");
666: dos2
667: .writeBytes("permission "
668: + "java.util.PropertyPermission \"user.home\", \"read\";\n");
669: String tmp = System.getProperty("user.home")
670: + java.io.File.separator + "." + policyFile
671: + "";
672: tmp = tmp.replace(java.io.File.separatorChar, '=');
673: tmp = tmp.replaceAll("=", "==");
674: tmp = tmp.replace('=', java.io.File.separatorChar);
675: dos2.writeBytes("permission "
676: + "java.io.FilePermission \"" + tmp
677: + "\", \"read,delete,write\";\n");
678: tmp = System.getProperty("user.home")
679: + java.io.File.separator + "." + policyFile
680: + ".copiedBySMT";
681: tmp = tmp.replace(java.io.File.separatorChar, '=');
682: tmp = tmp.replaceAll("=", "==");
683: tmp = tmp.replace('=', java.io.File.separatorChar);
684: dos2.writeBytes("permission "
685: + "java.io.FilePermission \"" + tmp
686: + "\", \"read,delete\";\n");
687: dos2
688: .writeBytes("permission "
689: + "java.util.PropertyPermission \"java.security.policy\", \"write\";\n");
690: dos2
691: .writeBytes("permission "
692: + "java.security.SecurityPermission \"getPolicy\";\n");
693: dos2
694: .writeBytes("permission "
695: + "java.security.SecurityPermission \"setProperty.policy.url.1\";\n");
696: }
697: dos2.writeBytes("};\n");
698: dos2.flush();
699: dos2.close();
700: } catch (Throwable e) {
701: e.printStackTrace();
702: }
703: //System.setProperty("java.security.policy", "=" + System.getProperty("user.home") + java.io.File.separator + "."+policyFile+"");
704: java.security.Policy.getPolicy().refresh();
705: //java.security.Security.setProperty("policy.url.1", "file:${java.home}/lib/security/"+policyFile+"");
706: java.security.Security.setProperty("policy.url.1",
707: "file:${user.home}/." + policyFile + "");
708:
709: sm = new SecurityManager();
710: System.setSecurityManager(sm);
711: } catch (Throwable e) {
712: e.printStackTrace();
713: }
714: }
715:
716: /**
717: *
718: */
719: private void setAllPermissions() {
720: setTestPermission0("java.security.AllPermission", "", "", false);
721: }
722:
723: /**
724: *
725: */
726: private void setNoPermissions() {
727: //setTestPermission0("NothingPermissions", "", "", false);
728: setTestPermission0("", "", "", true);
729: }
730:
731: }
|