KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > StatisticsFormatterUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.test;
23
24 import java.io.Serializable JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.sql.Connection JavaDoc;
28
29 import javax.management.Attribute JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34
35 import junit.framework.Test;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.mx.util.ObjectNameFactory;
39 import org.jboss.resource.statistic.JBossStatistics;
40 import org.jboss.resource.statistic.formatter.StatisticsFormatter;
41 import org.jboss.resource.statistic.pool.JBossDefaultSubPoolStatisticFormatter;
42 import org.jboss.resource.statistic.pool.JBossXmlSubPoolStatisticFormatter;
43 import org.jboss.resource.statistic.pool.ManagedConnectionPoolStatistics;
44 import org.jboss.test.JBossTestCase;
45 import org.jboss.test.jca.statistics.StatisticsHelper;
46 import org.w3c.dom.Document JavaDoc;
47 import org.xml.sax.InputSource JavaDoc;
48
49 /**
50  * A StatisticsReporterUnitTestCase.
51  *
52  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
53  * @version $Revision: 44973 $
54  */

55 public class StatisticsFormatterUnitTestCase extends JBossTestCase
56 {
57    private static final Logger log = Logger.getLogger(StatisticsFormatterUnitTestCase.class);
58    
59    private static final String JavaDoc DEFAULT_FORMATTER = "org.jboss.resource.statistic.pool.JBossDefaultSubPoolStatisticFormatter";
60    private static final String JavaDoc XML_FORMATTER = "org.jboss.resource.statistic.pool.JBossXmlSubPoolStatisticFormatter";
61    
62    private static final ObjectName JavaDoc POOL_NAME = ObjectNameFactory.create("jboss.jca:service=ManagedConnectionPool,name=StatsDS");
63    private static final String JavaDoc ATTRIBUTE_NAME = "StatisticsFormatter";
64    private static final String JavaDoc RAW_STATS_METHOD = "listStatistics";
65    private static final String JavaDoc FORMATTED_STATS_METHOD = "listFormattedSubPoolStatistics";
66    
67    public StatisticsFormatterUnitTestCase(String JavaDoc name){
68     
69       super(name);
70       
71    }
72    
73    /**
74     * Test basic formatter MBean.
75     *
76     * @throws Exception
77     */

78    public void testDefaultFormatterSetting() throws Exception JavaDoc{
79
80       String JavaDoc formatter = StatisticsHelper.getStatisticsFormatter(getServer());
81       log.debug("Found default statistics formatter " + formatter);
82       super.assertEquals(formatter, DEFAULT_FORMATTER);
83       
84    }
85    
86    /**
87     * FIXME Comment this
88     *
89     * @throws Exception
90     */

91    public void testRawStatistics() throws Exception JavaDoc{
92       
93       Object JavaDoc result = StatisticsHelper.listRawStatistics(getServer());
94       
95       assertTrue(result instanceof Serializable JavaDoc);
96       assertTrue(result instanceof ManagedConnectionPoolStatistics);
97             
98    }
99
100    public void testDefaultFormattedStatistics() throws Exception JavaDoc{
101       
102       InitialContext JavaDoc initCtx = super.getInitialContext();
103       DataSource JavaDoc ds = (DataSource JavaDoc)initCtx.lookup("StatsDS");
104       Connection JavaDoc conn = ds.getConnection("sa", "");
105       
106       Object JavaDoc formattedStats = StatisticsHelper.listFormattedStatistics(getServer());
107       
108       assertTrue(formattedStats instanceof String JavaDoc);
109       
110       //Do a diff
111
Object JavaDoc rawStatistics = StatisticsHelper.listRawStatistics(getServer());
112       
113       StatisticsFormatter defaultFormatter = StatisticsHelper.getDefaultFormatter();
114       
115       ManagedConnectionPoolStatistics stats = (ManagedConnectionPoolStatistics)rawStatistics;
116       
117       String JavaDoc rawFormat = (String JavaDoc)defaultFormatter.formatStatistics((JBossStatistics)stats);
118       
119       assertEquals(formattedStats, rawFormat);
120       
121       conn.close();
122       
123       
124    }
125    
126    public void testXmlFormatterStatistics() throws Exception JavaDoc{
127
128       InitialContext JavaDoc initCtx = super.getInitialContext();
129       DataSource JavaDoc ds = (DataSource JavaDoc)initCtx.lookup("StatsDS");
130       Connection JavaDoc conn = ds.getConnection("sa", "");
131
132       setStatisticsFormatter(XML_FORMATTER);
133       Object JavaDoc formattedStats = (String JavaDoc)getServer().invoke(POOL_NAME, FORMATTED_STATS_METHOD, new Object JavaDoc[0], new String JavaDoc[0]);
134       
135       assertTrue(formattedStats instanceof String JavaDoc);
136       
137       String JavaDoc xml = (String JavaDoc)formattedStats;
138       
139       StringReader JavaDoc reader = new StringReader JavaDoc(xml);
140       InputSource JavaDoc source = new InputSource JavaDoc(reader);
141       
142       Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
143          
144       ManagedConnectionPoolStatistics rawStatistics = (ManagedConnectionPoolStatistics)getServer().invoke(POOL_NAME, RAW_STATS_METHOD, new Object JavaDoc[0], new String JavaDoc[0]);
145       JBossXmlSubPoolStatisticFormatter xmlFormatter = new JBossXmlSubPoolStatisticFormatter();
146       String JavaDoc xml2 = (String JavaDoc)xmlFormatter.formatSubPoolStatistics(rawStatistics);
147       assertEquals(xml, xml2);
148       
149       conn.close();
150       
151    }
152    
153    public void testInvalidFormatter() throws Exception JavaDoc{
154
155       ObjectName JavaDoc name = new ObjectName JavaDoc("jboss.jca:service=ManagedConnectionPool,name=StatsDS");
156       
157       InitialContext JavaDoc initCtx = super.getInitialContext();
158       DataSource JavaDoc ds = (DataSource JavaDoc)initCtx.lookup("StatsDS");
159       Connection JavaDoc conn = ds.getConnection("sa", "");
160       setStatisticsFormatter("Invalid");
161       
162       Object JavaDoc formattedStats = (String JavaDoc)getServer().invoke(POOL_NAME, FORMATTED_STATS_METHOD, new Object JavaDoc[0], new String JavaDoc[0]);
163       
164       assertTrue(formattedStats instanceof String JavaDoc);
165       
166       JBossDefaultSubPoolStatisticFormatter defaultFormatter = new JBossDefaultSubPoolStatisticFormatter();
167       
168       Object JavaDoc rawStatistics = listRawStatistics();
169       ManagedConnectionPoolStatistics stats = (ManagedConnectionPoolStatistics)rawStatistics;
170       
171       String JavaDoc rawFormat = (String JavaDoc)defaultFormatter.formatSubPoolStatistics(stats);
172       
173       assertEquals(formattedStats, rawFormat);
174       
175    }
176    
177   
178    
179    private Object JavaDoc listRawStatistics() throws Exception JavaDoc{
180       
181       return getServer().invoke(POOL_NAME, RAW_STATS_METHOD, new Object JavaDoc[0], new String JavaDoc[0]);
182             
183    }
184    
185    private void setStatisticsFormatter(String JavaDoc formatter) throws Exception JavaDoc{
186       
187       getServer().setAttribute(POOL_NAME, new Attribute JavaDoc(ATTRIBUTE_NAME, formatter));
188       
189    }
190
191    private String JavaDoc getStatisticsFormatter() throws Exception JavaDoc{
192    
193       return (String JavaDoc)getServer().getAttribute(POOL_NAME, ATTRIBUTE_NAME);
194       
195    }
196
197    public static Test suite() throws Exception JavaDoc
198    {
199       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
200       URL JavaDoc resURL = loader.getResource("jca/stats/default-stats-ds.xml");
201       return getDeploySetup(StatisticsFormatterUnitTestCase.class, resURL.toString());
202    }
203 }
204
Popular Tags