4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * ident "%Z%%M% %I% %E% SMI"
29 import org.opensolaris.os.dtrace.*;
33 import java.lang.reflect.*;
36 * Regression test for serialization and XML encoding/decoding. Tests
37 * every Serializable class in the Java DTrace API by creating a dummy
38 * instance, writing it to a file, then reading it back in and comparing
39 * the string values of the object before and after, as well as
40 * verifying object equality before and after if the class overrides the
43 public class TestBean {
44 public static final String[] TESTS = new String[] {
68 "InterfaceAttributes",
75 "UserSymbolRecord$Value",
91 public static XMLEncoder
92 getXMLEncoder(File file)
94 XMLEncoder encoder = null;
96 OutputStream out = new BufferedOutputStream
97 (new FileOutputStream(file));
98 encoder = new XMLEncoder(out);
99 } catch (Exception e) {
106 public static XMLDecoder
107 getXMLDecoder(File file)
109 return getXMLDecoder(file, null);
112 public static XMLDecoder
113 getXMLDecoder(File file, ExceptionListener exceptionListener)
115 XMLDecoder decoder = null;
117 InputStream in = new BufferedInputStream
118 (new FileInputStream(file));
119 decoder = new XMLDecoder(in, null, exceptionListener);
120 } catch (Exception e) {
127 public static ExitRecord
130 ExitRecord r = new ExitRecord(1);
134 public static AggregationRecord
135 getAggregationRecord()
137 Tuple tuple = getTuple();
138 AggregationValue value = new CountValue(7);
139 AggregationRecord r = new AggregationRecord(tuple, value);
143 public static Aggregation
146 List < AggregationRecord > list =
147 new ArrayList < AggregationRecord > ();
149 r = getAggregationRecord();
152 ValueRecord v1 = new ScalarRecord(new byte[] {(byte)1, (byte)2,
154 ValueRecord v2 = new ScalarRecord("shebang!", 256);
155 Tuple tuple = new Tuple(v1, v2);
156 AggregationValue value = getLinearDistribution();
157 r = new AggregationRecord(tuple, value);
160 Aggregation a = new Aggregation("counts", 2, list);
167 ValueRecord r1 = new ScalarRecord("cat", 256);
168 ValueRecord r2 = new ScalarRecord(new Integer(9), 2);
169 ValueRecord r3 = new KernelStackRecord(
171 new StackFrame("has"),
172 new StackFrame("nine"),
173 new StackFrame("lives")},
174 new byte[] { (byte)0, (byte)1, (byte)2 });
175 ValueRecord r4 = new ScalarRecord(new byte[] {(byte)1, (byte)2,
178 Tuple tuple = new Tuple(r1, r2, r3, r4);
182 public static ScalarRecord
185 Object v = new byte[] {(byte)1, (byte)2, (byte)3};
186 ScalarRecord r = new ScalarRecord(v, 3);
190 public static KernelStackRecord
191 getKernelStackRecord()
193 StackFrame[] stackFrames = new StackFrame[] {
194 new StackFrame("Frame 1"),
195 new StackFrame("Frame 2"),
196 new StackFrame("Frame 3")
198 KernelStackRecord r = new KernelStackRecord(stackFrames,
199 new byte[] { (byte)0, (byte)1, (byte)2 });
203 public static LogDistribution
206 List < Distribution.Bucket > buckets =
207 new ArrayList < Distribution.Bucket > ();
208 Distribution.Bucket bucket;
218 for (i = -62; i <= 62; ++i) {
220 bucket = new Distribution.Bucket(-1, -1, n++);
222 bucket = new Distribution.Bucket(0, 0, n++);
224 bucket = new Distribution.Bucket(1, 1, n++);
228 sign = ((i < 0) ? -1L : 1L);
230 nextSign = (((i + 1) < 0) ? -1L : 1L);
231 nextPower = (nextSign * (i + 1));
232 lowerBound = sign * ((long) Math.pow(2L, power));
233 upperBound = (nextPower == 0 ? -2L :
234 (nextSign * ((long) Math.pow(2L, nextPower))) - 1);
235 if ((upperBound > 0) && ((upperBound * 2L) < 0)) {
236 upperBound = Long.MAX_VALUE;
238 bucket = new Distribution.Bucket(lowerBound, upperBound, n++);
241 LogDistribution d = new LogDistribution(buckets);
245 public static LinearDistribution
246 getLinearDistribution()
248 List < Distribution.Bucket > buckets =
249 new ArrayList < Distribution.Bucket > ();
250 Distribution.Bucket bucket;
251 int n = 10; // number of buckets
254 bucket = new Distribution.Bucket(Long.MIN_VALUE, (base - 1), 0);
256 for (int i = base; i < (n * step); i += step) {
257 bucket = new Distribution.Bucket(i, (i + (step - 1)),
261 bucket = new Distribution.Bucket((n * step) + 1, Long.MAX_VALUE, 0);
263 LinearDistribution d = new LinearDistribution(base, step, buckets);
270 Option option = new Option("aggrate", "1s");
274 public static ProcessState
277 ProcessState p = new ProcessState(123456, "UNDEAD",
279 -2, "Process stopped on dime");
283 public static ProbeDescription
284 getProbeDescription()
286 ProbeDescription d = new ProbeDescription(256, "syscall", null,
291 public static PrintaRecord
294 List < Aggregation > aggregations = new ArrayList < Aggregation > ();
295 Aggregation a = getAggregation();
298 Map < Tuple, String > formattedOutput =
299 new HashMap < Tuple, String > ();
300 for (Tuple t : a.asMap().keySet()) {
301 formattedOutput.put(t, "cat");
303 List < Tuple > tuples = new ArrayList < Tuple > ();
304 for (Tuple t : a.asMap().keySet()) {
307 Collections.sort(tuples);
308 PrintaRecord r = new PrintaRecord(1234567890L,
309 aggregations, formattedOutput, tuples,
310 "Yes, this is the formatted printa() output");
314 public static PrintfRecord
317 List < ValueRecord > list = new ArrayList < ValueRecord > ();
318 ValueRecord v1 = getScalarRecord();
319 ValueRecord v2 = new ScalarRecord(new Integer(7), 4);
322 PrintfRecord r = new PrintfRecord(list,
323 "long formatted string");
327 public static ProbeData
330 List < Record > list = new ArrayList < Record > ();
331 list.add(getPrintaRecord());
332 list.add(getPrintfRecord());
333 list.add(getScalarRecord());
334 list.add(getUserSymbolRecord());
335 list.add(getUserStackRecord());
336 list.add(getExitRecord());
337 ProbeData d = new ProbeData(7, 1, getProbeDescription(),
342 public static Aggregate
345 List < Aggregation > list = new ArrayList < Aggregation > ();
346 list.add(getAggregation());
348 List < AggregationRecord > reclist =
349 new ArrayList < AggregationRecord > ();
351 ValueRecord v1 = new ScalarRecord("cat", 256);
352 ValueRecord v2 = new ScalarRecord("dog", 256);
353 ValueRecord v3 = new ScalarRecord("mouse", 256);
354 ValueRecord v4 = new ScalarRecord("mouse", 256);
355 ValueRecord v5 = new ScalarRecord(new Byte((byte) 'C'), 1);
356 ValueRecord v6 = new ScalarRecord(new Short((short) 7), 2);
357 Tuple tuple = new Tuple(v1, v2, v3, v4, v5, v6);
358 AggregationValue value = getCountValue();
359 r = new AggregationRecord(tuple, value);
361 list.add(new Aggregation("times", 1, reclist));
363 Aggregate a = new Aggregate(1234567890L, list);
367 public static UserStackRecord
370 StackFrame[] frames = new StackFrame[] {
371 new StackFrame("User Stack Frame 1"),
372 new StackFrame("User Stack Frame 2"),
373 new StackFrame("User Stack Frame 3")
375 UserStackRecord r = new UserStackRecord(123456, frames,
376 new byte[] { (byte)0, (byte)1, (byte)2 });
380 public static AvgValue
383 AvgValue v = new AvgValue(5, 20, 4);
387 public static CountValue
390 CountValue v = new CountValue(9);
394 public static MinValue
397 MinValue v = new MinValue(101);
401 public static MaxValue
404 MaxValue v = new MaxValue(101);
408 public static SumValue
411 SumValue v = new SumValue(25);
415 public static org.opensolaris.os.dtrace.Error
418 ProbeDescription probe = getProbeDescription();
419 org.opensolaris.os.dtrace.Error e =
420 new org.opensolaris.os.dtrace.Error(probe, 8, 3,
421 1, 20, "DTRACEFLT_BADALIGN", -1, "error on enabled probe ID 8 " +
422 "(ID " + probe.getID() + ": " + probe + "): Bad alignment " +
423 "(0x33ef) in action #1 at DIF offset 20");
430 Drop drop = new Drop(2, "SPECBUSY", 72, 1041,
431 "Guess we dropped stuff all over the place.");
435 public static InterfaceAttributes
436 getInterfaceAttributes()
438 InterfaceAttributes a = new InterfaceAttributes(
439 InterfaceAttributes.Stability.UNSTABLE,
440 InterfaceAttributes.Stability.EVOLVING,
441 InterfaceAttributes.DependencyClass.ISA);
445 public static ProgramInfo
448 ProgramInfo info = new ProgramInfo(getInterfaceAttributes(),
449 getInterfaceAttributes(), 256);
453 public static ProbeInfo
456 ProbeInfo info = new ProbeInfo(getInterfaceAttributes(),
457 getInterfaceAttributes());
464 Probe p = new Probe(getProbeDescription(), getProbeInfo());
471 Flow f = new Flow(Flow.Kind.RETURN.name(), 3);
475 public static KernelSymbolRecord
476 getKernelSymbolRecord()
478 KernelSymbolRecord r = new KernelSymbolRecord("mod`func+0x4", -1L);
482 public static UserSymbolRecord
483 getUserSymbolRecord()
485 UserSymbolRecord r = new UserSymbolRecord(7, "mod`func+0x4", -1L);
489 public static UserSymbolRecord.Value
490 getUserSymbolRecord$Value()
492 UserSymbolRecord.Value v = new UserSymbolRecord.Value(7, -1L);
496 public static Program
499 final String PROGRAM = "syscall:::entry { @[execname] = count(); }";
500 Consumer consumer = new LocalConsumer();
504 p = consumer.compile(PROGRAM);
506 } catch (DTraceException e) {
513 public static Program.File
516 final String PROGRAM = "syscall:::entry { @[execname] = count(); }";
517 Consumer consumer = new LocalConsumer();
520 OutputStream out = new FileOutputStream(file);
521 out.write(PROGRAM.getBytes(), 0, PROGRAM.length());
525 p = consumer.compile(file);
527 } catch (Exception e) {
531 return Program.File.class.cast(p);
534 public static StddevValue
537 StddevValue v = new StddevValue(37, 114, 5, Integer.toString(9544));
541 @SuppressWarnings("unchecked")
546 if (o instanceof ScalarRecord) {
547 o = ((ScalarRecord)o).getValue();
550 if (o instanceof byte[]) {
551 s = Arrays.toString((byte[])o);
552 } else if (o instanceof Object[]) {
553 s = Arrays.toString((Object[])o);
555 Class c = o.getClass();
557 Method m = c.getDeclaredMethod("toLogString");
558 s = (String)m.invoke(o);
559 } catch (Exception e) {
567 checkEquality(Object obj, Object newobj)
569 // If the class overrides equals(), make sure the re-created
570 // object still equals the original object
572 Method eq = obj.getClass().getDeclaredMethod("equals",
574 Boolean ret = (Boolean) eq.invoke(obj, newobj);
576 System.err.println("serialization failed: " +
577 obj.getClass().getName());
580 } catch (Exception e) {
581 // Does not override equals(), although a super-class might.
582 // A better test would check for any superclass other than
588 performSerializationTest(File file, String classname)
589 throws IOException, ClassNotFoundException
591 String methodName = "get" + classname;
593 Object newobj = null;
595 Method method = TestBean.class.getDeclaredMethod(methodName);
596 obj = method.invoke(null);
597 } catch (Exception e) {
602 System.out.println(classname + ":");
603 String serialized = getString(obj);
604 System.out.println(" serialized: " + serialized);
605 FileOutputStream fos = new FileOutputStream(file);
606 ObjectOutputStream out = new ObjectOutputStream(fos);
607 out.writeObject(obj);
609 FileInputStream fis = new FileInputStream(file);
610 ObjectInputStream in = new ObjectInputStream(fis);
611 newobj = in.readObject();
613 String deserialized = getString(newobj);
614 System.out.println(" deserialized: " + deserialized);
616 if (!serialized.equals(deserialized)) {
617 System.err.println("serialization failed: " + classname);
620 checkEquality(obj, newobj);
624 performBeanTest(File file, String classname)
626 String methodName = "get" + classname;
628 Object newobj = null;
630 Method method = TestBean.class.getDeclaredMethod(methodName);
631 obj = method.invoke(null);
632 } catch (Exception e) {
637 Class c = obj.getClass();
638 if (c.getConstructors().length == 0) {
642 System.out.println(classname + ":");
643 XMLEncoder encoder = getXMLEncoder(file);
644 String encoded = getString(obj);
645 System.out.println(" encoded: " + encoded);
646 encoder.writeObject(obj);
648 XMLDecoder decoder = getXMLDecoder(file);
649 newobj = decoder.readObject();
650 String decoded = getString(newobj);
651 System.out.println(" decoded: " + decoded);
654 if (!encoded.equals(decoded)) {
655 System.err.println("bean persistence failed: " + classname);
658 checkEquality(obj, newobj);
664 if ((args.length != 1) && (args.length != 2)) {
665 System.err.println("usage: java TestBean < filename > " +
666 "[ < classname > ]");
670 String filename = args[0];
671 String classname = null;
672 if (args.length >= 2) {
676 file = new File(filename);
678 if (!file.canRead()) {
680 file.createNewFile();
681 } catch (Exception e) {
682 System.err.println("failed to create " + filename);
686 } catch (SecurityException e) {
687 System.err.println("failed to open " + filename);
691 String[] tests = (classname == null ? TESTS:
692 new String[] { classname });
694 for (int i = 0; i < tests.length; ++i) {
695 performSerializationTest(file, tests[i]);
696 performBeanTest(file, tests[i]);
698 } catch (IOException e) {
701 } catch (ClassNotFoundException e) {