View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.monitoring.reporting;
19  
20  import org.apache.commons.monitoring.Role;
21  import org.apache.commons.monitoring.counters.Counter;
22  import org.apache.commons.monitoring.reporting.web.handler.api.Template;
23  import org.apache.commons.monitoring.reporting.web.handler.api.TemplateHelper;
24  import org.apache.commons.monitoring.reporting.web.plugin.report.format.CSVFormat;
25  import org.apache.commons.monitoring.reporting.web.plugin.report.format.Format;
26  import org.apache.commons.monitoring.reporting.web.template.Templates;
27  import org.apache.commons.monitoring.repositories.Repository;
28  import org.junit.AfterClass;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  
32  import java.io.PrintWriter;
33  import java.io.StringWriter;
34  import java.util.Collections;
35  
36  import static org.junit.Assert.assertEquals;
37  
38  public class FormatsTest {
39      @BeforeClass
40      public static void setup() {
41          Repository.INSTANCE.clear();
42          Templates.init("", "");
43  
44          final Counter counter = Repository.INSTANCE.getCounter(new Counter.Key(Role.FAILURES, "RendererTest"));
45          counter.updateConcurrency(1);
46          counter.add(1.);
47      }
48  
49      @AfterClass
50      public static void clear() {
51          Repository.INSTANCE.clear();
52      }
53  
54      @Test
55      public void renderToXML() throws Exception {
56          final StringWriter out = new StringWriter();
57          final TemplateHelper helper = new TemplateHelper(new PrintWriter(out), Collections.<String, Object>emptyMap());
58          final Template template = Format.Defaults.XML.render(Collections.<String, Object>emptyMap());
59          helper.renderPlain(template.getTemplate(), template.getUserParams());
60  
61          assertEquals("<?xml version=\"1.0\"?> <repository> " +
62              "<counter name=\"RendererTest\" role=\"failures\" unit=\"u\" Hits=\"1.0\" Max=\"1.0\" Mean=\"1.0\" Min=\"1.0\" StandardDeviation=\"0.0\" Sum=\"1.0\" " +
63              "SumOfLogs=\"0.0\" SumOfSquares=\"0.0\" Variance=\"0.0\" GeometricMean=\"1.0\" Value=\"1.0\" Concurrency=\"0.0\" MaxConcurrency=\"1.0\" />" +
64              " </repository>", inline(out));
65      }
66  
67      @Test
68      public void renderToJSON() throws Exception {
69          final StringWriter out = new StringWriter();
70          final TemplateHelper helper = new TemplateHelper(new PrintWriter(out), Collections.<String, Object>emptyMap());
71          final Template template = Format.Defaults.JSON.render(Collections.<String, Object>emptyMap());
72          helper.renderPlain(template.getTemplate(), template.getUserParams());
73  
74          assertEquals("{\"counters\":[" +
75              " {\"name\":\"RendererTest\", \"role\":\"failures\",\"unit\":\"u\",\"Hits\":\"1.0\",\"Max\":\"1.0\",\"Mean\":\"1.0\",\"Min\":\"1.0\"," +
76              "\"StandardDeviation\":\"0.0\",\"Sum\":\"1.0\",\"SumOfLogs\":\"0.0\",\"SumOfSquares\":\"0.0\",\"Variance\":\"0.0\"," +
77              "\"GeometricMean\":\"1.0\",\"Value\":\"1.0\",\"Concurrency\":\"0.0\",\"MaxConcurrency\":\"1.0\"} ]}", inline(out));
78      }
79  
80      @Test
81      public void renderToCSV() throws Exception {
82          final StringWriter out = new StringWriter();
83          final TemplateHelper helper = new TemplateHelper(new PrintWriter(out), Collections.<String, Object>emptyMap());
84          final Template template = Format.Defaults.CSV.render(Collections.<String, Object>emptyMap());
85          helper.renderPlain(template.getTemplate(), template.getUserParams());
86  
87          assertEquals(CSVFormat.HEADER +
88              "RendererTest;failures (u);1.0;1.0;1.0;1.0;0.0;1.0;0.0;0.0;0.0;1.0;1.0;0.0;1.0\n",
89              out.toString());
90      }
91  
92      private static String inline(StringWriter out) {
93          return out.toString().replace("\r\n", " ").replace("\n", " ").replaceAll(" +", " ").replace("\t", "").trim();
94      }
95  }