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: package org.apache.harmony.tests.java.lang.instrument;
019:
020: import junit.framework.AssertionFailedError;
021: import junit.framework.TestCase;
022: import tests.support.Support_Exec;
023:
024: public class InstrumentTest extends TestCase {
025:
026: /**
027: * @tests try to add a null Transformer
028: */
029: public void test_addTransformer_null() throws Exception {
030: String[] arg = new String[2];
031: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_addTransformer_null.jar";
032: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
033: String[] path = new String[1];
034: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_addTransformer_null.jar";
035: String result = Support_Exec.execJava(arg, path, true);
036: assertEquals("", result);
037: }
038:
039: /**
040: * @tests try to remove a null Transformer
041: */
042: public void test_removeTransformer_null() throws Exception {
043: String[] arg = new String[2];
044: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_removeTransformer_null.jar";
045: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
046: String[] path = new String[1];
047: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_removeTransformer_null.jar";
048: String result = Support_Exec.execJava(arg, path, true);
049: assertEquals("", result);
050: }
051:
052: /**
053: * @tests try to remove a non-exists Transformer
054: */
055: public void test_removeTransformer_notExists() throws Exception {
056: String[] arg = new String[2];
057: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_removeTransformer_notExists.jar";
058: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
059: String[] path = new String[1];
060: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_removeTransformer_notExists.jar";
061: String result = Support_Exec.execJava(arg, path, true);
062: assertEquals("", result);
063: }
064:
065: /**
066: * @tests try to load a class that does not exist
067: */
068: public void test_loadClass_null() throws Exception {
069: String[] arg = new String[2];
070: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/loading_class.jar";
071: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
072: String[] path = new String[1];
073: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/loading_class.jar";
074: try {
075: Support_Exec.execJava(arg, path, true);
076: fail("Should fail here!");
077: } catch (AssertionFailedError e) {
078: // class loader changes, can not load classes
079: assertTrue(-1 != e.getMessage().indexOf(
080: "NoClassDefFoundError"));
081: }
082: }
083:
084: /**
085: * @tests try to use a new ClassLoader
086: */
087: public void test_new_classLoader_Exists() throws Exception {
088: String[] arg = new String[2];
089: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/new_classloader.jar";
090: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
091: String[] path = new String[1];
092: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/new_classloader.jar";
093: String result = Support_Exec.execJava(arg, path, true);
094: assertNotNull(result);
095: }
096:
097: /**
098: * @tests test if attribute of "Premain-Class" is null
099: */
100: public void test_Property_Premain_null() throws Exception {
101: String[] arg = new String[2];
102: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/Property_Premain_null.jar";
103: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
104: String[] path = new String[1];
105: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/Property_Premain_null.jar";
106: try {
107: Support_Exec.execJava(arg, path, true);
108: fail("Should fail here!");
109: } catch (AssertionFailedError e) {
110: // expected
111: assertTrue(-1 != e.getMessage().indexOf("Failed")
112: || -1 != e.getMessage().indexOf("error"));
113: assertTrue(-1 != e.getMessage().indexOf("Premain-Class"));
114: assertTrue(-1 != e.getMessage().indexOf("attribute"));
115: }
116: }
117:
118: /**
119: * @tests test if attributes of "Can-Redefine-Classes" and "Boot-Class-Path"
120: * is null
121: */
122: public void test_Property_other_null() throws Exception {
123: String[] arg = new String[2];
124: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/Property_other_null.jar";
125: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
126: String[] path = new String[1];
127: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/Property_other_null.jar";
128: String result = Support_Exec.execJava(arg, path, true);
129: assertNotNull(result);
130: }
131:
132: /**
133: * @tests test if the attributes is case-sensitive
134: */
135: public void test_Properity_case_sensitive() throws Exception {
136: String[] arg = new String[2];
137: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/Properity_case_sensitive.jar";
138: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
139: String[] path = new String[1];
140: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/Properity_case_sensitive.jar";
141: String result = Support_Exec.execJava(arg, path, true);
142: assertEquals("", result);
143: }
144:
145: /**
146: * @tests test if the jar file is bad
147: */
148: public void test_BadFormatJar() throws Exception {
149: String[] arg = new String[2];
150: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/BadFormatJar.jar";
151: arg[1] = "";
152: String[] path = new String[1];
153: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/BadFormatJar.jar";
154: try {
155: Support_Exec.execJava(arg, path, true);
156: fail("Should fail here!");
157: } catch (AssertionFailedError e) {
158: // expected
159: assertTrue(-1 != e.getMessage().indexOf("error")
160: || -1 != e.getMessage().indexOf("Error"));
161: assertTrue(-1 != e.getMessage().indexOf("open"));
162: assertTrue(-1 != e.getMessage().indexOf("file"));
163: }
164: }
165:
166: /**
167: * @tests test if premain class is null
168: */
169: public void test_Premain_Class_null() throws Exception {
170: String[] arg = new String[2];
171: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_Class_null.jar";
172: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
173: String[] path = new String[1];
174: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/Premain_Class_null.jar";
175: try {
176: Support_Exec.execJava(arg, path, true);
177: fail("Should fail here!");
178: } catch (AssertionFailedError e) {
179: // expected
180: assertTrue(-1 != e.getMessage().indexOf(
181: "ClassNotFoundException"));
182: }
183: }
184:
185: /**
186: * @tests test transforming all classes to a zero byte
187: */
188: public void test_zero_byte_transformer() throws Exception {
189: String[] arg = new String[2];
190: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/zero_byte_class.jar";
191: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
192: String[] path = new String[1];
193: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/zero_byte_class.jar";
194: String result = Support_Exec.execJava(arg, path, true);
195: assertEquals("", result);
196: }
197:
198: /**
199: * @tests test if complied by version 1.4
200: */
201: public void test_old_version() throws Exception {
202: String[] arg = new String[2];
203: arg[0] = "-javaagent:src/test/resources/jars/org/apache/harmony/tests/instrument/old_version_class.jar";
204: arg[1] = "org/apache/harmony/tests/java/lang/instrument/TestMain";
205: String[] path = new String[1];
206: path[0] = "src/test/resources/jars/org/apache/harmony/tests/instrument/old_version_class.jar";
207: String result = Support_Exec.execJava(arg, path, true);
208: assertEquals("", result);
209: }
210: }
|