123b5c241Stomee /*
223b5c241Stomee  * CDDL HEADER START
323b5c241Stomee  *
423b5c241Stomee  * The contents of this file are subject to the terms of the
523b5c241Stomee  * Common Development and Distribution License (the "License").
623b5c241Stomee  * You may not use this file except in compliance with the License.
723b5c241Stomee  *
823b5c241Stomee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
923b5c241Stomee  * or http://www.opensolaris.org/os/licensing.
1023b5c241Stomee  * See the License for the specific language governing permissions
1123b5c241Stomee  * and limitations under the License.
1223b5c241Stomee  *
1323b5c241Stomee  * When distributing Covered Code, include this CDDL HEADER in each
1423b5c241Stomee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1523b5c241Stomee  * If applicable, add the following below this CDDL HEADER, with the
1623b5c241Stomee  * fields enclosed by brackets "[]" replaced with your own identifying
1723b5c241Stomee  * information: Portions Copyright [yyyy] [name of copyright owner]
1823b5c241Stomee  *
1923b5c241Stomee  * CDDL HEADER END
2023b5c241Stomee  */
2123b5c241Stomee 
2223b5c241Stomee /*
23e77b06d2Stomee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2423b5c241Stomee  * Use is subject to license terms.
2523b5c241Stomee  */
2623b5c241Stomee 
2723b5c241Stomee import org.opensolaris.os.dtrace.*;
2823b5c241Stomee import java.util.*;
2923b5c241Stomee import java.io.*;
3023b5c241Stomee import java.beans.*;
3123b5c241Stomee import java.lang.reflect.*;
3223b5c241Stomee 
3323b5c241Stomee /**
3423b5c241Stomee  * Regression test for serialization and XML encoding/decoding.  Tests
3523b5c241Stomee  * every Serializable class in the Java DTrace API by creating a dummy
3623b5c241Stomee  * instance, writing it to a file, then reading it back in and comparing
3723b5c241Stomee  * the string values of the object before and after, as well as
3823b5c241Stomee  * verifying object equality before and after if the class overrides the
3923b5c241Stomee  * equals() method.
4023b5c241Stomee  */
4123b5c241Stomee public class TestBean {
4223b5c241Stomee     public static final String[] TESTS = new String[] {
4323b5c241Stomee 	"ExitRecord",
4423b5c241Stomee 	"AggregationRecord",
4523b5c241Stomee 	"Aggregation",
4623b5c241Stomee 	"Tuple",
4723b5c241Stomee 	"ScalarRecord",
4823b5c241Stomee 	"KernelStackRecord",
4923b5c241Stomee 	"LogDistribution",
5023b5c241Stomee 	"LinearDistribution",
51*ae94d716SRichard Lowe 	"LogLinearDistribution",
5223b5c241Stomee 	"Option",
5323b5c241Stomee 	"ProcessState",
5423b5c241Stomee 	"ProbeDescription",
5523b5c241Stomee 	"PrintaRecord",
5623b5c241Stomee 	"PrintfRecord",
5723b5c241Stomee 	"ProbeData",
5823b5c241Stomee 	"Aggregate",
5923b5c241Stomee 	"UserStackRecord",
6023b5c241Stomee 	"AvgValue",
6123b5c241Stomee 	"CountValue",
6223b5c241Stomee 	"SumValue",
6323b5c241Stomee 	"MinValue",
6423b5c241Stomee 	"MaxValue",
6523b5c241Stomee 	"Error",
6623b5c241Stomee 	"Drop",
6723b5c241Stomee 	"InterfaceAttributes",
6823b5c241Stomee 	"ProgramInfo",
6923b5c241Stomee 	"ProbeInfo",
7023b5c241Stomee 	"Probe",
7123b5c241Stomee 	"Flow",
7223b5c241Stomee 	"KernelSymbolRecord",
7323b5c241Stomee 	"UserSymbolRecord",
7491cfa10aStomee 	"UserSymbolRecord$Value",
7591cfa10aStomee 	"Program",
76e77b06d2Stomee 	"Program$File",
77e77b06d2Stomee 	"StddevValue"
7823b5c241Stomee     };
7923b5c241Stomee 
8091cfa10aStomee     static File file;
8191cfa10aStomee 
8223b5c241Stomee     static void
exit(int status)8323b5c241Stomee     exit(int status)
8423b5c241Stomee     {
8523b5c241Stomee 	System.out.flush();
8623b5c241Stomee 	System.err.flush();
8723b5c241Stomee 	System.exit(status);
8823b5c241Stomee     }
8923b5c241Stomee 
9023b5c241Stomee     public static XMLEncoder
getXMLEncoder(File file)9123b5c241Stomee     getXMLEncoder(File file)
9223b5c241Stomee     {
9323b5c241Stomee         XMLEncoder encoder = null;
9423b5c241Stomee         try {
9523b5c241Stomee             OutputStream out = new BufferedOutputStream
9623b5c241Stomee                     (new FileOutputStream(file));
9723b5c241Stomee             encoder = new XMLEncoder(out);
9823b5c241Stomee         } catch (Exception e) {
9923b5c241Stomee 	    e.printStackTrace();
10023b5c241Stomee 	    exit(1);
10123b5c241Stomee         }
10223b5c241Stomee         return encoder;
10323b5c241Stomee     }
10423b5c241Stomee 
10523b5c241Stomee     public static XMLDecoder
getXMLDecoder(File file)10623b5c241Stomee     getXMLDecoder(File file)
10723b5c241Stomee     {
10823b5c241Stomee         return getXMLDecoder(file, null);
10923b5c241Stomee     }
11023b5c241Stomee 
11123b5c241Stomee     public static XMLDecoder
getXMLDecoder(File file, ExceptionListener exceptionListener)11223b5c241Stomee     getXMLDecoder(File file, ExceptionListener exceptionListener)
11323b5c241Stomee     {
11423b5c241Stomee         XMLDecoder decoder = null;
11523b5c241Stomee         try {
11623b5c241Stomee             InputStream in = new BufferedInputStream
11723b5c241Stomee                     (new FileInputStream(file));
11823b5c241Stomee             decoder = new XMLDecoder(in, null, exceptionListener);
11923b5c241Stomee         } catch (Exception e) {
12023b5c241Stomee 	    e.printStackTrace();
12123b5c241Stomee 	    exit(1);
12223b5c241Stomee         }
12323b5c241Stomee         return decoder;
12423b5c241Stomee     }
12523b5c241Stomee 
12623b5c241Stomee     public static ExitRecord
getExitRecord()12723b5c241Stomee     getExitRecord()
12823b5c241Stomee     {
12923b5c241Stomee 	ExitRecord r = new ExitRecord(1);
13023b5c241Stomee 	return r;
13123b5c241Stomee     }
13223b5c241Stomee 
13323b5c241Stomee     public static AggregationRecord
getAggregationRecord()13423b5c241Stomee     getAggregationRecord()
13523b5c241Stomee     {
13623b5c241Stomee 	Tuple tuple = getTuple();
13723b5c241Stomee 	AggregationValue value = new CountValue(7);
13823b5c241Stomee 	AggregationRecord r = new AggregationRecord(tuple, value);
13923b5c241Stomee 	return r;
14023b5c241Stomee     }
14123b5c241Stomee 
14223b5c241Stomee     public static Aggregation
getAggregation()14323b5c241Stomee     getAggregation()
14423b5c241Stomee     {
14523b5c241Stomee 	List < AggregationRecord > list =
14623b5c241Stomee 	    new ArrayList < AggregationRecord > ();
14723b5c241Stomee 	AggregationRecord r;
14823b5c241Stomee 	r = getAggregationRecord();
14923b5c241Stomee 	list.add(r);
15023b5c241Stomee 
15123b5c241Stomee 	ValueRecord v1 = new ScalarRecord(new byte[] {(byte)1, (byte)2,
15223b5c241Stomee 	    (byte)3}, 3);
15323b5c241Stomee 	ValueRecord v2 = new ScalarRecord("shebang!", 256);
15423b5c241Stomee 	Tuple tuple = new Tuple(v1, v2);
15523b5c241Stomee 	AggregationValue value = getLinearDistribution();
15623b5c241Stomee 	r = new AggregationRecord(tuple, value);
15723b5c241Stomee 	list.add(r);
15823b5c241Stomee 
15923b5c241Stomee 	Aggregation a = new Aggregation("counts", 2, list);
16023b5c241Stomee 	return a;
16123b5c241Stomee     }
16223b5c241Stomee 
16323b5c241Stomee     public static Tuple
getTuple()16423b5c241Stomee     getTuple()
16523b5c241Stomee     {
16623b5c241Stomee 	ValueRecord r1 = new ScalarRecord("cat", 256);
16723b5c241Stomee 	ValueRecord r2 = new ScalarRecord(new Integer(9), 2);
16823b5c241Stomee 	ValueRecord r3 = new KernelStackRecord(
16923b5c241Stomee 		new StackFrame[] {
17023b5c241Stomee 		    new StackFrame("has"),
17123b5c241Stomee 		    new StackFrame("nine"),
17223b5c241Stomee 		    new StackFrame("lives")},
17323b5c241Stomee 		new byte[] { (byte)0, (byte)1, (byte)2 });
17423b5c241Stomee 	ValueRecord r4 = new ScalarRecord(new byte[] {(byte)1, (byte)2,
17523b5c241Stomee 	    (byte)3}, 3);
17623b5c241Stomee 
17723b5c241Stomee 	Tuple tuple = new Tuple(r1, r2, r3, r4);
17823b5c241Stomee 	return tuple;
17923b5c241Stomee     }
18023b5c241Stomee 
18123b5c241Stomee     public static ScalarRecord
getScalarRecord()18223b5c241Stomee     getScalarRecord()
18323b5c241Stomee     {
18423b5c241Stomee 	Object v = new byte[] {(byte)1, (byte)2, (byte)3};
18523b5c241Stomee 	ScalarRecord r = new ScalarRecord(v, 3);
18623b5c241Stomee 	return r;
18723b5c241Stomee     }
18823b5c241Stomee 
18923b5c241Stomee     public static KernelStackRecord
getKernelStackRecord()19023b5c241Stomee     getKernelStackRecord()
19123b5c241Stomee     {
19223b5c241Stomee 	StackFrame[] stackFrames = new StackFrame[] {
19323b5c241Stomee 	    new StackFrame("Frame 1"),
19423b5c241Stomee 	    new StackFrame("Frame 2"),
19523b5c241Stomee 	    new StackFrame("Frame 3")
19623b5c241Stomee 	};
19723b5c241Stomee 	KernelStackRecord r = new KernelStackRecord(stackFrames,
19823b5c241Stomee 		new byte[] { (byte)0, (byte)1, (byte)2 });
19923b5c241Stomee 	return r;
20023b5c241Stomee     }
20123b5c241Stomee 
20223b5c241Stomee     public static LogDistribution
getLogDistribution()20323b5c241Stomee     getLogDistribution()
20423b5c241Stomee     {
20523b5c241Stomee 	List < Distribution.Bucket > buckets =
20623b5c241Stomee 		new ArrayList < Distribution.Bucket > ();
20723b5c241Stomee 	Distribution.Bucket bucket;
20823b5c241Stomee 	int n = 0;
20923b5c241Stomee 	long base = 0;
21023b5c241Stomee 	long i;
21123b5c241Stomee 	long sign;
21223b5c241Stomee 	long nextSign;
21323b5c241Stomee 	long power;
21423b5c241Stomee 	long nextPower;
21523b5c241Stomee 	long lowerBound;
21623b5c241Stomee 	long upperBound;
21723b5c241Stomee 	for (i = -62; i <= 62; ++i) {
21823b5c241Stomee 	    if (i == 0) {
21923b5c241Stomee 		bucket = new Distribution.Bucket(-1, -1, n++);
22023b5c241Stomee 		buckets.add(bucket);
22123b5c241Stomee 		bucket = new Distribution.Bucket(0, 0, n++);
22223b5c241Stomee 		buckets.add(bucket);
22323b5c241Stomee 		bucket = new Distribution.Bucket(1, 1, n++);
22423b5c241Stomee 		buckets.add(bucket);
22523b5c241Stomee 		continue;
22623b5c241Stomee 	    }
22723b5c241Stomee 	    sign = ((i < 0) ? -1L : 1L);
22823b5c241Stomee 	    power = (sign * i);
22923b5c241Stomee 	    nextSign = (((i + 1) < 0) ? -1L : 1L);
23023b5c241Stomee 	    nextPower = (nextSign * (i + 1));
23123b5c241Stomee 	    lowerBound = sign * ((long) Math.pow(2L, power));
23223b5c241Stomee 	    upperBound = (nextPower == 0 ? -2L :
23323b5c241Stomee 		    (nextSign * ((long) Math.pow(2L, nextPower))) - 1);
23423b5c241Stomee 	    if ((upperBound > 0) && ((upperBound * 2L) < 0)) {
23523b5c241Stomee 		upperBound = Long.MAX_VALUE;
23623b5c241Stomee 	    }
23723b5c241Stomee 	    bucket = new Distribution.Bucket(lowerBound, upperBound, n++);
23823b5c241Stomee 	    buckets.add(bucket);
23923b5c241Stomee 	}
24023b5c241Stomee 	LogDistribution d = new LogDistribution(buckets);
24123b5c241Stomee 	return d;
24223b5c241Stomee     }
24323b5c241Stomee 
24423b5c241Stomee     public static LinearDistribution
getLinearDistribution()24523b5c241Stomee     getLinearDistribution()
24623b5c241Stomee     {
24723b5c241Stomee 	List < Distribution.Bucket > buckets =
24823b5c241Stomee 		new ArrayList < Distribution.Bucket > ();
24923b5c241Stomee 	Distribution.Bucket bucket;
25023b5c241Stomee 	int n = 10; // number of buckets
25123b5c241Stomee 	int base = 1;
25223b5c241Stomee 	int step = 10;
25323b5c241Stomee 	bucket = new Distribution.Bucket(Long.MIN_VALUE, (base - 1), 0);
25423b5c241Stomee 	buckets.add(bucket);
25523b5c241Stomee 	for (int i = base; i < (n * step); i += step) {
25623b5c241Stomee 	    bucket = new Distribution.Bucket(i, (i + (step - 1)),
25723b5c241Stomee 		    ((i - 1) / step));
25823b5c241Stomee 	    buckets.add(bucket);
25923b5c241Stomee 	}
26023b5c241Stomee 	bucket = new Distribution.Bucket((n * step) + 1, Long.MAX_VALUE, 0);
26123b5c241Stomee 	buckets.add(bucket);
26223b5c241Stomee 	LinearDistribution d = new LinearDistribution(base, step, buckets);
26323b5c241Stomee 	return d;
26423b5c241Stomee     }
26523b5c241Stomee 
266*ae94d716SRichard Lowe     public static LogLinearDistribution
getLogLinearDistribution()267*ae94d716SRichard Lowe     getLogLinearDistribution()
268*ae94d716SRichard Lowe     {
269*ae94d716SRichard Lowe         Distribution.Bucket bucket;
270*ae94d716SRichard Lowe         long next, step;
271*ae94d716SRichard Lowe         long low = 0;
272*ae94d716SRichard Lowe         long high = 6;
273*ae94d716SRichard Lowe         long factor = 2;
274*ae94d716SRichard Lowe         long nsteps = 2;
275*ae94d716SRichard Lowe         int value = 1;
276*ae94d716SRichard Lowe         int order;
277*ae94d716SRichard Lowe 
278*ae94d716SRichard Lowe         List < Distribution.Bucket > buckets =
279*ae94d716SRichard Lowe           new ArrayList < Distribution.Bucket > ();
280*ae94d716SRichard Lowe 
281*ae94d716SRichard Lowe         for (order = 0; order < low; order++)
282*ae94d716SRichard Lowe             value *= factor;
283*ae94d716SRichard Lowe 
284*ae94d716SRichard Lowe         bucket = new Distribution.Bucket(Long.MIN_VALUE, (value - 1), 0);
285*ae94d716SRichard Lowe         buckets.add(bucket);
286*ae94d716SRichard Lowe 
287*ae94d716SRichard Lowe         next = value * factor;
288*ae94d716SRichard Lowe         step = (next > nsteps) ? (next / nsteps) : 1;
289*ae94d716SRichard Lowe 
290*ae94d716SRichard Lowe         while (order <= high) {
291*ae94d716SRichard Lowe             bucket = new Distribution.Bucket(value, value + step - 1, 5);
292*ae94d716SRichard Lowe             buckets.add(bucket);
293*ae94d716SRichard Lowe 
294*ae94d716SRichard Lowe             if ((value += step) != next)
295*ae94d716SRichard Lowe                 continue;
296*ae94d716SRichard Lowe 
297*ae94d716SRichard Lowe             next = value * factor;
298*ae94d716SRichard Lowe             step = (next > nsteps) ? (next / nsteps) : 1;
299*ae94d716SRichard Lowe             order++;
300*ae94d716SRichard Lowe         }
301*ae94d716SRichard Lowe 
302*ae94d716SRichard Lowe         bucket = new Distribution.Bucket(value, Long.MAX_VALUE, 0);
303*ae94d716SRichard Lowe         buckets.add(bucket);
304*ae94d716SRichard Lowe 
305*ae94d716SRichard Lowe         LogLinearDistribution d = new LogLinearDistribution(factor, low, high,
306*ae94d716SRichard Lowe           nsteps, 0, buckets);
307*ae94d716SRichard Lowe         return d;
308*ae94d716SRichard Lowe     }
309*ae94d716SRichard Lowe 
31023b5c241Stomee     public static Option
getOption()31123b5c241Stomee     getOption()
31223b5c241Stomee     {
31323b5c241Stomee 	Option option = new Option("aggrate", "1s");
31423b5c241Stomee 	return option;
31523b5c241Stomee     }
31623b5c241Stomee 
31723b5c241Stomee     public static ProcessState
getProcessState()31823b5c241Stomee     getProcessState()
31923b5c241Stomee     {
32023b5c241Stomee 	ProcessState p = new ProcessState(123456, "UNDEAD",
32123b5c241Stomee 		3, "SIGSTOP",
32223b5c241Stomee 		-2, "Process stopped on dime");
32323b5c241Stomee 	return p;
32423b5c241Stomee     }
32523b5c241Stomee 
32623b5c241Stomee     public static ProbeDescription
getProbeDescription()32723b5c241Stomee     getProbeDescription()
32823b5c241Stomee     {
32923b5c241Stomee 	ProbeDescription d = new ProbeDescription(256, "syscall", null,
33023b5c241Stomee 	    "malloc", "entry");
33123b5c241Stomee 	return d;
33223b5c241Stomee     }
33323b5c241Stomee 
33423b5c241Stomee     public static PrintaRecord
getPrintaRecord()33523b5c241Stomee     getPrintaRecord()
33623b5c241Stomee     {
33723b5c241Stomee 	List < Aggregation > aggregations = new ArrayList < Aggregation > ();
33823b5c241Stomee 	Aggregation a = getAggregation();
33923b5c241Stomee 	aggregations.add(a);
34023b5c241Stomee 	aggregations.add(a);
34123b5c241Stomee 	Map < Tuple, String > formattedOutput =
34223b5c241Stomee 		new HashMap < Tuple, String > ();
34323b5c241Stomee 	for (Tuple t : a.asMap().keySet()) {
34423b5c241Stomee 	    formattedOutput.put(t, "cat");
34523b5c241Stomee 	}
34623b5c241Stomee 	List < Tuple > tuples = new ArrayList < Tuple > ();
34723b5c241Stomee 	for (Tuple t : a.asMap().keySet()) {
34823b5c241Stomee 	    tuples.add(t);
34923b5c241Stomee 	}
35023b5c241Stomee 	Collections.sort(tuples);
35123b5c241Stomee 	PrintaRecord r = new PrintaRecord(1234567890L,
35223b5c241Stomee 	    aggregations, formattedOutput, tuples,
35323b5c241Stomee 	    "Yes, this is the formatted printa() output");
35423b5c241Stomee 	return r;
35523b5c241Stomee     }
35623b5c241Stomee 
35723b5c241Stomee     public static PrintfRecord
getPrintfRecord()35823b5c241Stomee     getPrintfRecord()
35923b5c241Stomee     {
36023b5c241Stomee 	List < ValueRecord > list = new ArrayList < ValueRecord > ();
36123b5c241Stomee 	ValueRecord v1 = getScalarRecord();
36223b5c241Stomee 	ValueRecord v2 = new ScalarRecord(new Integer(7), 4);
36323b5c241Stomee 	list.add(v1);
36423b5c241Stomee 	list.add(v2);
36523b5c241Stomee 	PrintfRecord r = new PrintfRecord(list,
36623b5c241Stomee 		"long formatted string");
36723b5c241Stomee 	return r;
36823b5c241Stomee     }
36923b5c241Stomee 
37023b5c241Stomee     public static ProbeData
getProbeData()37123b5c241Stomee     getProbeData()
37223b5c241Stomee     {
37323b5c241Stomee 	List < Record > list = new ArrayList < Record > ();
37423b5c241Stomee 	list.add(getPrintaRecord());
37523b5c241Stomee 	list.add(getPrintfRecord());
37623b5c241Stomee 	list.add(getScalarRecord());
377e77b06d2Stomee 	list.add(getUserSymbolRecord());
378e77b06d2Stomee 	list.add(getUserStackRecord());
379e77b06d2Stomee 	list.add(getExitRecord());
38023b5c241Stomee 	ProbeData d = new ProbeData(7, 1, getProbeDescription(),
38123b5c241Stomee 	    getFlow(), list);
38223b5c241Stomee 	return d;
38323b5c241Stomee     }
38423b5c241Stomee 
38523b5c241Stomee     public static Aggregate
getAggregate()38623b5c241Stomee     getAggregate()
38723b5c241Stomee     {
38823b5c241Stomee 	List < Aggregation > list = new ArrayList < Aggregation > ();
38923b5c241Stomee 	list.add(getAggregation());
39023b5c241Stomee 
39123b5c241Stomee 	List < AggregationRecord > reclist =
39223b5c241Stomee 	    new ArrayList < AggregationRecord > ();
39323b5c241Stomee 	AggregationRecord r;
39423b5c241Stomee 	ValueRecord v1 = new ScalarRecord("cat", 256);
39523b5c241Stomee 	ValueRecord v2 = new ScalarRecord("dog", 256);
39623b5c241Stomee 	ValueRecord v3 = new ScalarRecord("mouse", 256);
39723b5c241Stomee 	ValueRecord v4 = new ScalarRecord("mouse", 256);
39823b5c241Stomee 	ValueRecord v5 = new ScalarRecord(new Byte((byte) 'C'), 1);
39923b5c241Stomee 	ValueRecord v6 = new ScalarRecord(new Short((short) 7), 2);
40023b5c241Stomee 	Tuple tuple = new Tuple(v1, v2, v3, v4, v5, v6);
40123b5c241Stomee 	AggregationValue value = getCountValue();
40223b5c241Stomee 	r = new AggregationRecord(tuple, value);
40323b5c241Stomee 	reclist.add(r);
40423b5c241Stomee 	list.add(new Aggregation("times", 1, reclist));
40523b5c241Stomee 
40623b5c241Stomee         Aggregate a = new Aggregate(1234567890L, list);
40723b5c241Stomee 	return a;
40823b5c241Stomee     }
40923b5c241Stomee 
41023b5c241Stomee     public static UserStackRecord
getUserStackRecord()41123b5c241Stomee     getUserStackRecord()
41223b5c241Stomee     {
41323b5c241Stomee 	StackFrame[] frames = new StackFrame[] {
41423b5c241Stomee 	    new StackFrame("User Stack Frame 1"),
41523b5c241Stomee 	    new StackFrame("User Stack Frame 2"),
41623b5c241Stomee 	    new StackFrame("User Stack Frame 3")
41723b5c241Stomee 	};
41823b5c241Stomee 	UserStackRecord r = new UserStackRecord(123456, frames,
41923b5c241Stomee 		new byte[] { (byte)0, (byte)1, (byte)2 });
42023b5c241Stomee 	return r;
42123b5c241Stomee     }
42223b5c241Stomee 
42323b5c241Stomee     public static AvgValue
getAvgValue()42423b5c241Stomee     getAvgValue()
42523b5c241Stomee     {
42623b5c241Stomee 	AvgValue v = new AvgValue(5, 20, 4);
42723b5c241Stomee 	return v;
42823b5c241Stomee     }
42923b5c241Stomee 
43023b5c241Stomee     public static CountValue
getCountValue()43123b5c241Stomee     getCountValue()
43223b5c241Stomee     {
43323b5c241Stomee 	CountValue v = new CountValue(9);
43423b5c241Stomee 	return v;
43523b5c241Stomee     }
43623b5c241Stomee 
43723b5c241Stomee     public static MinValue
getMinValue()43823b5c241Stomee     getMinValue()
43923b5c241Stomee     {
44023b5c241Stomee 	MinValue v = new MinValue(101);
44123b5c241Stomee 	return v;
44223b5c241Stomee     }
44323b5c241Stomee 
44423b5c241Stomee     public static MaxValue
getMaxValue()44523b5c241Stomee     getMaxValue()
44623b5c241Stomee     {
44723b5c241Stomee 	MaxValue v = new MaxValue(101);
44823b5c241Stomee 	return v;
44923b5c241Stomee     }
45023b5c241Stomee 
45123b5c241Stomee     public static SumValue
getSumValue()45223b5c241Stomee     getSumValue()
45323b5c241Stomee     {
45423b5c241Stomee 	SumValue v = new SumValue(25);
45523b5c241Stomee 	return v;
45623b5c241Stomee     }
45723b5c241Stomee 
45823b5c241Stomee     public static org.opensolaris.os.dtrace.Error
getError()45923b5c241Stomee     getError()
46023b5c241Stomee     {
46123b5c241Stomee 	ProbeDescription probe = getProbeDescription();
46223b5c241Stomee 	org.opensolaris.os.dtrace.Error e =
46323b5c241Stomee 	    new org.opensolaris.os.dtrace.Error(probe, 8, 3,
46423b5c241Stomee 	    1, 20, "DTRACEFLT_BADALIGN", -1, "error on enabled probe ID 8 " +
46523b5c241Stomee 	    "(ID " + probe.getID() + ": " + probe + "): Bad alignment " +
46623b5c241Stomee 	    "(0x33ef) in action #1 at DIF offset 20");
46723b5c241Stomee 	return e;
46823b5c241Stomee     }
46923b5c241Stomee 
47023b5c241Stomee     public static Drop
getDrop()47123b5c241Stomee     getDrop()
47223b5c241Stomee     {
47323b5c241Stomee 	Drop drop = new Drop(2, "SPECBUSY", 72, 1041,
47423b5c241Stomee 	    "Guess we dropped stuff all over the place.");
47523b5c241Stomee 	return drop;
47623b5c241Stomee     }
47723b5c241Stomee 
47823b5c241Stomee     public static InterfaceAttributes
getInterfaceAttributes()47923b5c241Stomee     getInterfaceAttributes()
48023b5c241Stomee     {
48123b5c241Stomee 	InterfaceAttributes a = new InterfaceAttributes(
48223b5c241Stomee                 InterfaceAttributes.Stability.UNSTABLE,
48323b5c241Stomee                 InterfaceAttributes.Stability.EVOLVING,
48423b5c241Stomee                 InterfaceAttributes.DependencyClass.ISA);
48523b5c241Stomee 	return a;
48623b5c241Stomee     }
48723b5c241Stomee 
48823b5c241Stomee     public static ProgramInfo
getProgramInfo()48923b5c241Stomee     getProgramInfo()
49023b5c241Stomee     {
49123b5c241Stomee 	ProgramInfo info = new ProgramInfo(getInterfaceAttributes(),
49223b5c241Stomee 		getInterfaceAttributes(), 256);
49323b5c241Stomee 	return info;
49423b5c241Stomee     }
49523b5c241Stomee 
49623b5c241Stomee     public static ProbeInfo
getProbeInfo()49723b5c241Stomee     getProbeInfo()
49823b5c241Stomee     {
49923b5c241Stomee 	ProbeInfo info = new ProbeInfo(getInterfaceAttributes(),
50023b5c241Stomee 		getInterfaceAttributes());
50123b5c241Stomee 	return info;
50223b5c241Stomee     }
50323b5c241Stomee 
50423b5c241Stomee     public static Probe
getProbe()50523b5c241Stomee     getProbe()
50623b5c241Stomee     {
50723b5c241Stomee 	Probe p = new Probe(getProbeDescription(), getProbeInfo());
50823b5c241Stomee 	return p;
50923b5c241Stomee     }
51023b5c241Stomee 
51123b5c241Stomee     public static Flow
getFlow()51223b5c241Stomee     getFlow()
51323b5c241Stomee     {
51423b5c241Stomee 	Flow f = new Flow(Flow.Kind.RETURN.name(), 3);
51523b5c241Stomee 	return f;
51623b5c241Stomee     }
51723b5c241Stomee 
51823b5c241Stomee     public static KernelSymbolRecord
getKernelSymbolRecord()51923b5c241Stomee     getKernelSymbolRecord()
52023b5c241Stomee     {
52123b5c241Stomee 	KernelSymbolRecord r = new KernelSymbolRecord("mod`func+0x4", -1L);
52223b5c241Stomee 	return r;
52323b5c241Stomee     }
52423b5c241Stomee 
52523b5c241Stomee     public static UserSymbolRecord
getUserSymbolRecord()52623b5c241Stomee     getUserSymbolRecord()
52723b5c241Stomee     {
52823b5c241Stomee 	UserSymbolRecord r = new UserSymbolRecord(7, "mod`func+0x4", -1L);
52923b5c241Stomee 	return r;
53023b5c241Stomee     }
53123b5c241Stomee 
53223b5c241Stomee     public static UserSymbolRecord.Value
getUserSymbolRecord$Value()53323b5c241Stomee     getUserSymbolRecord$Value()
53423b5c241Stomee     {
53523b5c241Stomee 	UserSymbolRecord.Value v = new UserSymbolRecord.Value(7, -1L);
53623b5c241Stomee 	return v;
53723b5c241Stomee     }
53823b5c241Stomee 
53991cfa10aStomee     public static Program
getProgram()54091cfa10aStomee     getProgram()
54191cfa10aStomee     {
54291cfa10aStomee 	final String PROGRAM = "syscall:::entry { @[execname] = count(); }";
54391cfa10aStomee 	Consumer consumer = new LocalConsumer();
54491cfa10aStomee 	Program p;
54591cfa10aStomee 	try {
54691cfa10aStomee 	    consumer.open();
54791cfa10aStomee 	    p = consumer.compile(PROGRAM);
54891cfa10aStomee 	    consumer.close();
54991cfa10aStomee 	} catch (DTraceException e) {
55091cfa10aStomee 	    e.printStackTrace();
55191cfa10aStomee 	    p = null;
55291cfa10aStomee 	}
55391cfa10aStomee 	return p;
55491cfa10aStomee     }
55591cfa10aStomee 
55691cfa10aStomee     public static Program.File
getProgram$File()55791cfa10aStomee     getProgram$File()
55891cfa10aStomee     {
55991cfa10aStomee 	final String PROGRAM = "syscall:::entry { @[execname] = count(); }";
56091cfa10aStomee 	Consumer consumer = new LocalConsumer();
56191cfa10aStomee 	Program p;
56291cfa10aStomee 	try {
56391cfa10aStomee             OutputStream out = new FileOutputStream(file);
56491cfa10aStomee 	    out.write(PROGRAM.getBytes(), 0, PROGRAM.length());
56591cfa10aStomee 	    out.flush();
56691cfa10aStomee 	    out.close();
56791cfa10aStomee 	    consumer.open();
56891cfa10aStomee 	    p = consumer.compile(file);
56991cfa10aStomee 	    consumer.close();
57091cfa10aStomee 	} catch (Exception e) {
57191cfa10aStomee 	    e.printStackTrace();
57291cfa10aStomee 	    p = null;
57391cfa10aStomee 	}
57491cfa10aStomee 	return Program.File.class.cast(p);
57591cfa10aStomee     }
57691cfa10aStomee 
577e77b06d2Stomee     public static StddevValue
getStddevValue()578e77b06d2Stomee     getStddevValue()
579e77b06d2Stomee     {
580e77b06d2Stomee 	StddevValue v = new StddevValue(37, 114, 5, Integer.toString(9544));
581e77b06d2Stomee 	return v;
582e77b06d2Stomee     }
583e77b06d2Stomee 
584e77b06d2Stomee     @SuppressWarnings("unchecked")
58523b5c241Stomee     static String
getString(Object o)58623b5c241Stomee     getString(Object o)
58723b5c241Stomee     {
58823b5c241Stomee 	String s;
58923b5c241Stomee 	if (o instanceof ScalarRecord) {
59023b5c241Stomee 	    o = ((ScalarRecord)o).getValue();
59123b5c241Stomee 	}
59223b5c241Stomee 
59323b5c241Stomee 	if (o instanceof byte[]) {
59423b5c241Stomee 	    s = Arrays.toString((byte[])o);
59523b5c241Stomee 	} else if (o instanceof Object[]) {
59623b5c241Stomee 	    s = Arrays.toString((Object[])o);
59723b5c241Stomee 	} else {
59823b5c241Stomee 	    Class c = o.getClass();
59923b5c241Stomee 	    try {
60023b5c241Stomee 		Method m = c.getDeclaredMethod("toLogString");
60123b5c241Stomee 		s = (String)m.invoke(o);
60223b5c241Stomee 	    } catch (Exception e) {
60323b5c241Stomee 		s = o.toString();
60423b5c241Stomee 	    }
60523b5c241Stomee 	}
60623b5c241Stomee 	return s;
60723b5c241Stomee     }
60823b5c241Stomee 
60923b5c241Stomee     static void
checkEquality(Object obj, Object newobj)61023b5c241Stomee     checkEquality(Object obj, Object newobj)
61123b5c241Stomee     {
61223b5c241Stomee 	// If the class overrides equals(), make sure the re-created
61323b5c241Stomee 	// object still equals the original object
61423b5c241Stomee 	try {
61523b5c241Stomee 	    Method eq = obj.getClass().getDeclaredMethod("equals",
61623b5c241Stomee 		    Object.class);
61723b5c241Stomee 	    Boolean ret = (Boolean) eq.invoke(obj, newobj);
61823b5c241Stomee 	    if (ret != true) {
61923b5c241Stomee 		System.err.println("serialization failed: " +
62023b5c241Stomee 			obj.getClass().getName());
62123b5c241Stomee 		exit(1);
62223b5c241Stomee 	    }
62323b5c241Stomee 	} catch (Exception e) {
62423b5c241Stomee 	    // Does not override equals(), although a super-class might.
62523b5c241Stomee 	    // A better test would check for any superclass other than
62623b5c241Stomee 	    // Object.class.
62723b5c241Stomee 	}
62823b5c241Stomee     }
62923b5c241Stomee 
63023b5c241Stomee     static void
performSerializationTest(File file, String classname)63123b5c241Stomee     performSerializationTest(File file, String classname)
63223b5c241Stomee             throws IOException, ClassNotFoundException
63323b5c241Stomee     {
63423b5c241Stomee 	String methodName = "get" + classname;
63523b5c241Stomee 	Object obj = null;
63623b5c241Stomee 	Object newobj = null;
63723b5c241Stomee 	try {
63823b5c241Stomee 	    Method method = TestBean.class.getDeclaredMethod(methodName);
63923b5c241Stomee 	    obj = method.invoke(null);
64023b5c241Stomee 	} catch (Exception e) {
64123b5c241Stomee 	    e.printStackTrace();
64223b5c241Stomee 	    exit(1);
64323b5c241Stomee 	}
64423b5c241Stomee 
64523b5c241Stomee 	System.out.println(classname + ":");
64623b5c241Stomee 	String serialized = getString(obj);
64723b5c241Stomee 	System.out.println("  serialized: " + serialized);
64823b5c241Stomee 	FileOutputStream fos = new FileOutputStream(file);
64923b5c241Stomee 	ObjectOutputStream out = new ObjectOutputStream(fos);
65023b5c241Stomee 	out.writeObject(obj);
651e77b06d2Stomee 	out.close();
65223b5c241Stomee 	FileInputStream fis = new FileInputStream(file);
65323b5c241Stomee 	ObjectInputStream in = new ObjectInputStream(fis);
65423b5c241Stomee 	newobj = in.readObject();
655e77b06d2Stomee 	in.close();
65623b5c241Stomee 	String deserialized = getString(newobj);
65723b5c241Stomee 	System.out.println("  deserialized: " + deserialized);
65823b5c241Stomee 
65923b5c241Stomee 	if (!serialized.equals(deserialized)) {
66023b5c241Stomee 	    System.err.println("serialization failed: " + classname);
66123b5c241Stomee 	    exit(1);
66223b5c241Stomee 	}
66323b5c241Stomee 	checkEquality(obj, newobj);
66423b5c241Stomee     }
66523b5c241Stomee 
66623b5c241Stomee     static void
performBeanTest(File file, String classname)66723b5c241Stomee     performBeanTest(File file, String classname)
66823b5c241Stomee     {
66923b5c241Stomee 	String methodName = "get" + classname;
67023b5c241Stomee 	Object obj = null;
67123b5c241Stomee 	Object newobj = null;
67223b5c241Stomee 	try {
67323b5c241Stomee 	    Method method = TestBean.class.getDeclaredMethod(methodName);
67423b5c241Stomee 	    obj = method.invoke(null);
67523b5c241Stomee 	} catch (Exception e) {
67623b5c241Stomee 	    e.printStackTrace();
67723b5c241Stomee 	    exit(1);
67823b5c241Stomee 	}
67923b5c241Stomee 
68091cfa10aStomee 	Class c = obj.getClass();
68191cfa10aStomee 	if (c.getConstructors().length == 0) {
68291cfa10aStomee 	    return;
68391cfa10aStomee 	}
68491cfa10aStomee 
68523b5c241Stomee 	System.out.println(classname + ":");
68623b5c241Stomee 	XMLEncoder encoder = getXMLEncoder(file);
68723b5c241Stomee 	String encoded = getString(obj);
68823b5c241Stomee 	System.out.println("  encoded: " + encoded);
68923b5c241Stomee 	encoder.writeObject(obj);
69023b5c241Stomee 	encoder.close();
69123b5c241Stomee 	XMLDecoder decoder = getXMLDecoder(file);
69223b5c241Stomee 	newobj = decoder.readObject();
69323b5c241Stomee 	String decoded = getString(newobj);
69423b5c241Stomee 	System.out.println("  decoded: " + decoded);
69523b5c241Stomee 	decoder.close();
69623b5c241Stomee 
69723b5c241Stomee 	if (!encoded.equals(decoded)) {
69823b5c241Stomee 	    System.err.println("bean persistence failed: " + classname);
69923b5c241Stomee 	    exit(1);
70023b5c241Stomee 	}
70123b5c241Stomee 	checkEquality(obj, newobj);
70223b5c241Stomee     }
70323b5c241Stomee 
70423b5c241Stomee     public static void
main(String[] args)70523b5c241Stomee     main(String[] args)
70623b5c241Stomee     {
70723b5c241Stomee 	if ((args.length != 1) && (args.length != 2)) {
70823b5c241Stomee 	    System.err.println("usage: java TestBean < filename > " +
70923b5c241Stomee 		    "[ < classname > ]");
71023b5c241Stomee 	    exit(1);
71123b5c241Stomee 	}
71223b5c241Stomee 
71323b5c241Stomee 	String filename = args[0];
71423b5c241Stomee 	String classname = null;
71523b5c241Stomee 	if (args.length >= 2) {
71623b5c241Stomee 	    classname = args[1];
71723b5c241Stomee 	}
71823b5c241Stomee 
71991cfa10aStomee 	file = new File(filename);
72023b5c241Stomee 	try {
72123b5c241Stomee 	    if (!file.canRead()) {
72223b5c241Stomee 		try {
72323b5c241Stomee 		    file.createNewFile();
72423b5c241Stomee 		} catch (Exception e) {
72523b5c241Stomee 		    System.err.println("failed to create " + filename);
72623b5c241Stomee 		    exit(1);
72723b5c241Stomee 		}
72823b5c241Stomee 	    }
72923b5c241Stomee 	} catch (SecurityException e) {
73023b5c241Stomee 	    System.err.println("failed to open " + filename);
73123b5c241Stomee 	    exit(1);
73223b5c241Stomee 	}
73323b5c241Stomee 
73423b5c241Stomee 	String[] tests = (classname == null ? TESTS:
73523b5c241Stomee 		new String[] { classname });
73623b5c241Stomee 	try {
73723b5c241Stomee 	    for (int i = 0; i < tests.length; ++i) {
73823b5c241Stomee 		performSerializationTest(file, tests[i]);
73923b5c241Stomee 		performBeanTest(file, tests[i]);
74023b5c241Stomee 	    }
74123b5c241Stomee 	} catch (IOException e) {
74223b5c241Stomee 	    e.printStackTrace();
74323b5c241Stomee 	    exit(1);
74423b5c241Stomee 	} catch (ClassNotFoundException e) {
74523b5c241Stomee 	    e.printStackTrace();
74623b5c241Stomee 	    exit(1);
74723b5c241Stomee 	}
74823b5c241Stomee     }
74923b5c241Stomee }
750