Author: nicolas Date: Fri Jan 22 11:17:37 2010 New Revision: 902060 URL: http://svn.apache.org/viewvc?rev=902060&view=rev Log: cleaner rendering architecture : o AbstractVisitor supports filtering and defines start/end "event" method o FormatVisitor applies formatting on "events" o Format implementations to support specific data layout and separators (XML + JSON)
Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractVisitor.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/Format.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/JSONFormat.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/RepositoryFilter.java - copied, changed from r901735, commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/VisitorConfiguration.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/XMLFormat.java commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java - copied, changed from r901735, commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java Removed: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractFilteredVisitor.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/JsonRenderer.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/VisitorConfiguration.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/VisitorConfigurationSupport.java commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/XmlRenderer.java commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java Modified: commons/sandbox/monitoring/trunk/reporting/pom.xml commons/sandbox/monitoring/trunk/reporting/src/test/resources/org/apache/commons/monitoring/reporting/RendererTest.xml Modified: commons/sandbox/monitoring/trunk/reporting/pom.xml URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/pom.xml?rev=902060&r1=902059&r2=902060&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/pom.xml (original) +++ commons/sandbox/monitoring/trunk/reporting/pom.xml Fri Jan 22 11:17:37 2010 @@ -13,6 +13,11 @@ <artifactId>commons-monitoring-core</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.4</version> + </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> @@ -22,6 +27,12 @@ <dependency> <groupId>xmlunit</groupId> <artifactId>xmlunit</artifactId> + <version>1.3</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.codehaus.jettison</groupId> + <artifactId>jettison</artifactId> <version>1.2</version> <scope>test</scope> </dependency> Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractVisitor.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractVisitor.java?rev=902060&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractVisitor.java (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractVisitor.java Fri Jan 22 11:17:37 2010 @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.reporting; + +import java.util.Collection; + +import org.apache.commons.monitoring.Counter; +import org.apache.commons.monitoring.Gauge; +import org.apache.commons.monitoring.Metric; +import org.apache.commons.monitoring.Monitor; +import org.apache.commons.monitoring.Repository; +import org.apache.commons.monitoring.Visitor; + +public abstract class AbstractVisitor + implements Visitor +{ + private RepositoryFilter filter = FULL; + + public void setFilter( RepositoryFilter filter ) + { + this.filter = filter; + } + + protected abstract void monitorEnd( String name ); + + protected abstract void monitorStart( Monitor monitor ); + + protected abstract void gaugeStart( String name ); + + protected abstract void gaugeEnd( String name ); + + protected abstract void counterStart( String name ); + + protected abstract void counterEnd( String name ); + + protected abstract void repositoryEnd(); + + protected abstract void repositoryStart(); + + protected abstract void attribute( String name, double value ); + + protected abstract void attribute( String name, String value ); + + public final void visit( Repository repository ) + { + repositoryStart(); + doVisit( repository ); + repositoryEnd(); + } + + protected abstract void doVisit( Repository repository ); + + protected Collection<Monitor> getMonitors( Repository repository ) + { + return repository.getMonitors(); + } + + public final void visit( Monitor monitor ) + { + Monitor.Key key = monitor.getKey(); + String name = key.getName(); + monitorStart( monitor ); + doVisit( monitor ); + monitorEnd( name ); + } + + protected abstract void doVisit( Monitor monitor ); + + protected Collection<Metric> getMetrics( Monitor monitor ) + { + return monitor.getMetrics(); + } + + public final void visit( Gauge gauge ) + { + String name = gauge.getRole().getName(); + gaugeStart( name ); + attribute( "unit", gauge.getUnit().getName() ); + attribute( "value", gauge.getValue() ); + attribute( "max", gauge.getMax() ); + attribute( "min", gauge.getMin() ); + attribute( "hits", gauge.getHits() ); + attribute( "standardDeviation", gauge.getStandardDeviation() ); + attribute( "variance", gauge.getVariance() ); + attribute( "mean", gauge.getMean() ); + attribute( "geometricMean", gauge.getGeometricMean() ); + attribute( "sumOfLogs", gauge.getSumOfLogs() ); + attribute( "sumOfSquares", gauge.getSumOfSquares() ); + gaugeEnd( name ); + } + + public final void visit( Counter counter ) + { + String name = counter.getRole().getName(); + counterStart( name ); + attribute( "unit", counter.getUnit().getName() ); + attribute( "sum", counter.getSum() ); + attribute( "max", counter.getMax() ); + attribute( "min", counter.getMin() ); + attribute( "hits", counter.getHits() ); + attribute( "standardDeviation", counter.getStandardDeviation() ); + attribute( "variance", counter.getVariance() ); + attribute( "mean", counter.getMean() ); + attribute( "geometricMean", counter.getGeometricMean() ); + attribute( "sumOfLogs", counter.getSumOfLogs() ); + attribute( "sumOfSquares", counter.getSumOfSquares() ); + counterEnd( name ); + } + + private static final RepositoryFilter FULL = new RepositoryFilter() + { + + @Override + public boolean filter( Monitor monitor ) + { + return true; + } + + @Override + public boolean filter( Metric metric ) + { + return true; + } + + }; +} \ No newline at end of file Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/Format.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/Format.java?rev=902060&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/Format.java (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/Format.java Fri Jan 22 11:17:37 2010 @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.reporting; + +import java.io.PrintWriter; + +import org.apache.commons.monitoring.Monitor; + +public interface Format +{ + + void repositoryStart( PrintWriter writer ); + + void repositoryEnd( PrintWriter writer ); + + void monitorStart( PrintWriter writer, Monitor monitor ); + + void monitorEnd( PrintWriter writer, String name ); + + void gaugeStart( PrintWriter writer, String name ); + + void gaugeEnd( PrintWriter writer, String name ); + + void counterStart( PrintWriter writer, String name ); + + void counterEnd( PrintWriter writer, String name ); + + void attribute( PrintWriter writer, String name, String value ); + + void separator( PrintWriter writer ); + + void escape( PrintWriter writer, String string ); + + public static final Format JSON = new JSONFormat( false ); + + public static final Format JSON_PRETTY = new JSONFormat( true ); + + public static final Format XML = new XMLFormat( false ); + + public static final Format XML_PRETTY = new XMLFormat( true );; +} Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java?rev=902060&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/FormattingVisitor.java Fri Jan 22 11:17:37 2010 @@ -0,0 +1,164 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.reporting; + +import java.io.PrintWriter; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +import org.apache.commons.monitoring.Metric; +import org.apache.commons.monitoring.Monitor; +import org.apache.commons.monitoring.Repository; + +public class FormattingVisitor + extends AbstractVisitor +{ + protected PrintWriter writer; + + private NumberFormat numberFormat; + + private Format format; + + public FormattingVisitor( Format format, PrintWriter writer ) + { + this( format, writer, Locale.getDefault() ); + } + + public FormattingVisitor( Format format, PrintWriter writer, Locale locale ) + { + this( format, writer, DecimalFormat.getNumberInstance( locale ) ); + } + + public FormattingVisitor( Format format, PrintWriter writer, NumberFormat numberFormat ) + { + super(); + this.format = format; + this.writer = writer; + this.numberFormat = numberFormat; + } + + protected void doVisit( Repository repository ) + { + boolean first = true; + for ( Monitor monitor : getMonitors( repository ) ) + { + if ( !first ) + { + separator(); + } + first = false; + monitor.accept( this ); + } + } + + protected void doVisit( Monitor monitor ) + { + boolean first = true; + for ( Metric metric : getMetrics( monitor ) ) + { + if ( !first ) + { + separator(); + } + first = false; + metric.accept( this ); + } + } + + protected void attribute( String name, double value ) + { + attribute( name, format( value ) ); + } + + protected String format( double value ) + { + String s = numberFormat.format( value ); + if ( "\uFFFD".equals( s ) ) + { + // Locale may have no DecimalFormatSymbols.NaN (set to "\uFFFD" (REPLACE_CHAR)) + s = "NaN"; + } + return s; + } + + @Override + protected void repositoryStart() + { + format.repositoryStart( writer ); + } + + @Override + protected void repositoryEnd() + { + format.repositoryEnd( writer ); + } + + protected void attribute( String name, String value ) + { + format.attribute( writer, name, value ); + } + + @Override + protected void counterEnd( String name ) + { + format.counterEnd( writer, name ); + } + + @Override + protected void counterStart( String name ) + { + format.counterStart( writer, name ); + } + + protected void escape( String string ) + { + format.escape( writer, string ); + } + + @Override + protected void gaugeEnd( String name ) + { + format.gaugeEnd( writer, name ); + } + + @Override + protected void gaugeStart( String name ) + { + format.gaugeStart( writer, name ); + } + + @Override + protected void monitorStart( Monitor monitor ) + { + format.monitorStart( writer, monitor ); + } + + @Override + protected void monitorEnd( String name ) + { + format.monitorEnd( writer, name ); + } + + protected void separator() + { + format.separator( writer ); + } + + +} \ No newline at end of file Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/JSONFormat.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/JSONFormat.java?rev=902060&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/JSONFormat.java (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/JSONFormat.java Fri Jan 22 11:17:37 2010 @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.reporting; + +import java.io.PrintWriter; + +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.monitoring.Monitor; + + +/** + * Format to JSON (JavaScript), with optional indentation + * + * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> + */ +public class JSONFormat + implements Format +{ + private boolean indent; + + public JSONFormat( boolean indent ) + { + this.indent = indent; + } + + public void repositoryStart( PrintWriter writer ) + { + writer.append( "{" ); + } + + public void repositoryEnd( PrintWriter writer ) + { + if ( indent ) + { + writer.append( "\n" ); + } + writer.append( "}" ); + } + + public void monitorStart( PrintWriter writer, Monitor monitor ) + { + if ( indent ) + { + writer.append( "\n " ); + } + Monitor.Key key = monitor.getKey(); + escape( writer, key.getName() ); + writer.append( ":{" ); + } + + public void monitorEnd( PrintWriter writer, String name ) + { + if ( indent ) + { + writer.append( "\n " ); + } + + writer.append( "}" ); + } + + public void gaugeStart( PrintWriter writer, String name ) + { + if ( indent ) + { + writer.append( "\n " ); + } + escape( writer, name ); + writer.append( ":{" ); + writeAttribute( writer, "type", "gauge" ); + } + + public void gaugeEnd( PrintWriter writer, String name ) + { + writer.append( "}" ); + } + + public void counterStart( PrintWriter writer, String name ) + { + if ( indent ) + { + writer.append( "\n " ); + } + escape( writer, name ); + writer.append( ":{" ); + writeAttribute( writer, "type", "counter" ); + } + + public void counterEnd( PrintWriter writer, String name ) + { + writer.append( "}" ); + } + + public void attribute( PrintWriter writer, String name, String value ) + { + separator( writer ); + writeAttribute( writer, name, value ); + } + + protected void writeAttribute( PrintWriter writer, String name, String value ) + { + writer.append( name ) + .append( ":" ); + escape( writer, value ); + } + + public void escape( PrintWriter writer, String string ) + { + writer.append( '\"' ).append( StringEscapeUtils.escapeJavaScript( string ) ).append( '\"' ); + } + + public void separator( PrintWriter writer ) + { + writer.append( "," ); + } +} \ No newline at end of file Copied: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/RepositoryFilter.java (from r901735, commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/VisitorConfiguration.java) URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/RepositoryFilter.java?p2=commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/RepositoryFilter.java&p1=commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/VisitorConfiguration.java&r1=901735&r2=902060&rev=902060&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/VisitorConfiguration.java (original) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/RepositoryFilter.java Fri Jan 22 11:17:37 2010 @@ -17,17 +17,18 @@ package org.apache.commons.monitoring.reporting; -import org.apache.commons.monitoring.Role; -import org.apache.commons.monitoring.Monitor.Key; +import org.apache.commons.monitoring.Metric; +import org.apache.commons.monitoring.Monitor; /** + * Filter repository content. Used to expose a restricted view on an existing Repository. + * + * @see FilteredRepository * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> */ -public interface VisitorConfiguration +public interface RepositoryFilter { - boolean filter( Key key ); + boolean filter( Monitor monitor ); - boolean filter( Role role ); - - String format( double value ); + boolean filter( Metric metric ); } Added: commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/XMLFormat.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/XMLFormat.java?rev=902060&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/XMLFormat.java (added) +++ commons/sandbox/monitoring/trunk/reporting/src/main/java/org/apache/commons/monitoring/reporting/XMLFormat.java Fri Jan 22 11:17:37 2010 @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.reporting; + +import java.io.PrintWriter; + +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.monitoring.Monitor; + +/** + * Format to XML, with optional indentation + * + * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> + */ +public class XMLFormat + implements Format +{ + public XMLFormat( boolean indent ) + { + super(); + this.indent = indent; + } + + private boolean indent; + + public void repositoryStart( PrintWriter writer ) + { + writer.append( "<repository>" ); + } + + public void repositoryEnd( PrintWriter writer ) + { + if ( indent ) + { + writer.append( "\n" ); + } + writer.append( "</repository>" ); + } + + public void monitorStart( PrintWriter writer, Monitor monitor ) + { + if ( indent ) + { + writer.append( "\n " ); + } + writer.append( "<monitor" ); + Monitor.Key key = monitor.getKey(); + attribute( writer, "name", key.getName() ); + attribute( writer, "category", key.getCategory() ); + attribute( writer, "subsystem", key.getSubsystem() ); + writer.append( '>' ); + } + + public void monitorEnd( PrintWriter writer, String name ) + { + if ( indent ) + { + writer.append( "\n " ); + } + writer.append( "</monitor>" ); + } + + public void gaugeStart( PrintWriter writer, String name ) + { + if ( indent ) + { + writer.append( "\n " ); + } + writer.append( "<gauge" ); + attribute( writer, "role", name ); + } + + public void gaugeEnd( PrintWriter writer, String name ) + { + writer.append( "/>" ); + } + + public void counterStart( PrintWriter writer, String name ) + { + if ( indent ) + { + writer.append( "\n " ); + } + writer.append( "<counter" ); + attribute( writer, "role", name ); + } + + public void counterEnd( PrintWriter writer, String name ) + { + writer.append( "/>" ); + } + + public void attribute( PrintWriter writer, String name, String value ) + { + writer.append( " " ) + .append( name ) + .append( "=\"" ); + escape( writer, value ); + writer.append( "\"" ); + } + + public void escape( PrintWriter writer, String string ) + { + writer.append( StringEscapeUtils.escapeXml( string ) ); + } + + public void separator( PrintWriter writer ) + { + // Nop + } +} \ No newline at end of file Copied: commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java (from r901735, commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java) URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java?p2=commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java&p1=commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java&r1=901735&r2=902060&rev=902060&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java (original) +++ commons/sandbox/monitoring/trunk/reporting/src/test/java/org/apache/commons/monitoring/reporting/FormattingVisitorTest.java Fri Jan 22 11:17:37 2010 @@ -26,10 +26,15 @@ import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; +import java.text.NumberFormat; +import java.util.Locale; import org.apache.commons.monitoring.Repository; import org.apache.commons.monitoring.Visitor; import org.apache.commons.monitoring.repositories.DefaultRepository; +import org.codehaus.jettison.AbstractXMLStreamReader; +import org.codehaus.jettison.json.JSONObject; +import org.codehaus.jettison.mapped.MappedXMLStreamReader; import org.custommonkey.xmlunit.XMLAssert; import org.junit.Before; import org.junit.Test; @@ -37,16 +42,21 @@ /** * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> */ -public class RendererTest +public class FormattingVisitorTest { private Repository repository; + private NumberFormat format; + @Before public void setup() { repository = new DefaultRepository(); repository.getMonitor( "RendererTest", "unit", "test" ).getCounter( FAILURES ).add( 1.0 ); repository.getMonitor( "RendererTest", "unit", "test" ).getGauge( CONCURRENCY ).increment( UNARY ); + + format = NumberFormat.getNumberInstance( Locale.US ); + format.setMinimumFractionDigits( 1 ); } @Test @@ -54,10 +64,11 @@ throws Exception { StringWriter out = new StringWriter(); - Visitor v = new XmlRenderer( new VisitorConfigurationSupport(), new PrintWriter( out ) ); + Visitor v = new FormattingVisitor( Format.XML_PRETTY, new PrintWriter( out ), format ); repository.accept( v ); - Reader expected = new InputStreamReader( getClass().getResourceAsStream( "RendererTest.xml" ) ); + System.out.println( out.toString() ); + Reader expected = new InputStreamReader( getClass().getResourceAsStream( "RendererTest.xml" ) ); XMLAssert.assertXMLEqual( expected, new StringReader( out.toString() ) ); } @@ -66,10 +77,14 @@ throws Exception { StringWriter out = new StringWriter(); - Visitor v = new JsonRenderer( new VisitorConfigurationSupport(), new PrintWriter( out ) ); + Visitor v = new FormattingVisitor( Format.JSON_PRETTY, new PrintWriter( out ), format ); repository.accept( v ); System.out.println( out.toString() ); - // JSON Testing framework + JSONObject json = new JSONObject( out.toString() ); + AbstractXMLStreamReader reader = new MappedXMLStreamReader( json ); + + // FIXME can't use XMLStreamReader to compare actual with expected using XMLUnit :'( + // any JSON Testing framework ? } } Modified: commons/sandbox/monitoring/trunk/reporting/src/test/resources/org/apache/commons/monitoring/reporting/RendererTest.xml URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/reporting/src/test/resources/org/apache/commons/monitoring/reporting/RendererTest.xml?rev=902060&r1=902059&r2=902060&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/reporting/src/test/resources/org/apache/commons/monitoring/reporting/RendererTest.xml (original) +++ commons/sandbox/monitoring/trunk/reporting/src/test/resources/org/apache/commons/monitoring/reporting/RendererTest.xml Fri Jan 22 11:17:37 2010 @@ -1,9 +1,9 @@ <repository> <monitor name="RendererTest" category="unit" subsystem="test"> <counter role="failures" unit="" sum="1.0" max="1.0" min="1.0" hits="1.0" standardDeviation="0.0" variance="0.0" - mean="1.0" geometricMean="1.0" sumOfLogs="0.0" somOfSquares="1.0" /> + mean="1.0" geometricMean="1.0" sumOfLogs="0.0" sumOfSquares="1.0" /> <gauge role="concurrency" unit="" value="1.0" max="1.0" min="1.0" hits="1.0" standardDeviation="NaN" variance="NaN" - mean="NaN" geometricMean="NaN" sumOfLogs="NaN" somOfSquares="NaN" /> + mean="NaN" geometricMean="NaN" sumOfLogs="NaN" sumOfSquares="NaN" /> </monitor> </repository>