1178476Sjb/*
2178476Sjb * CDDL HEADER START
3178476Sjb *
4178476Sjb * The contents of this file are subject to the terms of the
5178476Sjb * Common Development and Distribution License (the "License").
6178476Sjb * You may not use this file except in compliance with the License.
7178476Sjb *
8178476Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178476Sjb * or http://www.opensolaris.org/os/licensing.
10178476Sjb * See the License for the specific language governing permissions
11178476Sjb * and limitations under the License.
12178476Sjb *
13178476Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178476Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178476Sjb * If applicable, add the following below this CDDL HEADER, with the
16178476Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178476Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178476Sjb *
19178476Sjb * CDDL HEADER END
20178476Sjb */
21178476Sjb
22178476Sjb/*
23178476Sjb * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24178476Sjb * Use is subject to license terms.
25178476Sjb *
26178476Sjb * ident	"%Z%%M%	%I%	%E% SMI"
27178476Sjb */
28178476Sjb
29178476Sjbimport java.io.File;
30178476Sjbimport java.io.IOException;
31178476Sjbimport java.util.List;
32178476Sjbimport org.opensolaris.os.dtrace.*;
33178476Sjb
34178476Sjb/**
35178476Sjb * Regression for multi-aggregation printa() corner cases.
36178476Sjb */
37178476Sjbpublic class TestMultiAggPrinta {
38178476Sjb    static int exitStatus;
39178476Sjb
40178476Sjb    // Gets a string representation of the given PrintaRecord minus the
41178476Sjb    // timestamp of the aggregate snapshot, so that the output is
42178476Sjb    // comparable across multiple test runs.
43178476Sjb    static String
44178476Sjb    printaRecordString(PrintaRecord rec)
45178476Sjb    {
46178476Sjb	StringBuffer buf = new StringBuffer();
47178476Sjb	buf.append(PrintaRecord.class.getName());
48178476Sjb	buf.append("[aggregations = ");
49178476Sjb	buf.append(rec.getAggregations());
50178476Sjb	buf.append(", formattedStrings = ");
51178476Sjb	buf.append(rec.getFormattedStrings());
52178476Sjb	buf.append(", tuples = ");
53178476Sjb	buf.append(rec.getTuples());
54178476Sjb	buf.append(", output = ");
55178476Sjb	buf.append(rec.getOutput());
56178476Sjb	buf.append(']');
57178476Sjb	return buf.toString();
58178476Sjb    }
59178476Sjb
60178476Sjb    static String
61178476Sjb    probeDataString(ProbeData data)
62178476Sjb    {
63178476Sjb	StringBuffer buf = new StringBuffer();
64178476Sjb	buf.append(ProbeData.class.getName());
65178476Sjb	buf.append("[epid = ");
66178476Sjb	buf.append(data.getEnabledProbeID());
67178476Sjb	// Do not include cpu, since it can change across test runs
68178476Sjb	buf.append(", enabledProbeDescription = ");
69178476Sjb	buf.append(data.getEnabledProbeDescription());
70178476Sjb	buf.append(", flow = ");
71178476Sjb	buf.append(data.getFlow());
72178476Sjb	buf.append(", records = ");
73178476Sjb
74178476Sjb	List <Record> records = data.getRecords();
75178476Sjb	Record record;
76178476Sjb	Object value;
77178476Sjb	buf.append('[');
78178476Sjb	for (int i = 0; i < records.size(); ++i) {
79178476Sjb	    if (i > 0) {
80178476Sjb		buf.append(", ");
81178476Sjb	    }
82178476Sjb	    record = records.get(i);
83178476Sjb	    if (record instanceof ValueRecord) {
84178476Sjb		value = ((ValueRecord)record).getValue();
85178476Sjb		if (value instanceof String) {
86178476Sjb		    buf.append("\"");
87178476Sjb		    buf.append((String)value);
88178476Sjb		    buf.append("\"");
89178476Sjb		} else {
90178476Sjb		    buf.append(record);
91178476Sjb		}
92178476Sjb	    } else if (record instanceof PrintaRecord) {
93178476Sjb		PrintaRecord printa = (PrintaRecord)record;
94178476Sjb		buf.append(printaRecordString(printa));
95178476Sjb	    } else {
96178476Sjb		buf.append(record);
97178476Sjb	    }
98178476Sjb	}
99178476Sjb	buf.append(']');
100178476Sjb	return buf.toString();
101178476Sjb    }
102178476Sjb
103178476Sjb    public static void
104178476Sjb    main(String[] args)
105178476Sjb    {
106178476Sjb	if (args.length != 1) {
107178476Sjb	    System.err.println("usage: java TestMultiAggPrinta <script>");
108178476Sjb	    System.exit(2);
109178476Sjb	}
110178476Sjb
111178476Sjb	final Consumer consumer = new LocalConsumer();
112178476Sjb	consumer.addConsumerListener(new ConsumerAdapter() {
113178476Sjb	    public void dataReceived(DataEvent e) {
114178476Sjb		ProbeData data = e.getProbeData();
115178476Sjb		List <Record> records = data.getRecords();
116178476Sjb		for (Record r : records) {
117178476Sjb		    if (r instanceof ExitRecord) {
118178476Sjb			ExitRecord exitRecord = (ExitRecord)r;
119178476Sjb			exitStatus = exitRecord.getStatus();
120178476Sjb		    }
121178476Sjb		}
122178476Sjb		System.out.println(probeDataString(e.getProbeData()));
123178476Sjb	    }
124178476Sjb	    public void consumerStopped(ConsumerEvent e) {
125178476Sjb		consumer.close();
126178476Sjb		System.exit(exitStatus);
127178476Sjb	    }
128178476Sjb	});
129178476Sjb
130178476Sjb	File file = new File(args[0]);
131178476Sjb	try {
132178476Sjb	    consumer.open();
133178476Sjb	    consumer.compile(file);
134178476Sjb	    consumer.enable();
135178476Sjb	    consumer.go();
136178476Sjb	} catch (DTraceException e) {
137178476Sjb	    e.printStackTrace();
138178476Sjb	    System.exit(1);
139178476Sjb	} catch (IOException e) {
140178476Sjb	    e.printStackTrace();
141178476Sjb	    System.exit(1);
142178476Sjb	}
143178476Sjb    }
144178476Sjb}
145