1 22 package org.jboss.test.jca.test; 23 24 import java.io.Serializable ; 25 import java.io.StringReader ; 26 import java.net.URL ; 27 import java.sql.Connection ; 28 29 import javax.management.Attribute ; 30 import javax.management.ObjectName ; 31 import javax.naming.InitialContext ; 32 import javax.sql.DataSource ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 47 import org.xml.sax.InputSource ; 48 49 55 public class StatisticsFormatterUnitTestCase extends JBossTestCase 56 { 57 private static final Logger log = Logger.getLogger(StatisticsFormatterUnitTestCase.class); 58 59 private static final String DEFAULT_FORMATTER = "org.jboss.resource.statistic.pool.JBossDefaultSubPoolStatisticFormatter"; 60 private static final String XML_FORMATTER = "org.jboss.resource.statistic.pool.JBossXmlSubPoolStatisticFormatter"; 61 62 private static final ObjectName POOL_NAME = ObjectNameFactory.create("jboss.jca:service=ManagedConnectionPool,name=StatsDS"); 63 private static final String ATTRIBUTE_NAME = "StatisticsFormatter"; 64 private static final String RAW_STATS_METHOD = "listStatistics"; 65 private static final String FORMATTED_STATS_METHOD = "listFormattedSubPoolStatistics"; 66 67 public StatisticsFormatterUnitTestCase(String name){ 68 69 super(name); 70 71 } 72 73 78 public void testDefaultFormatterSetting() throws Exception { 79 80 String formatter = StatisticsHelper.getStatisticsFormatter(getServer()); 81 log.debug("Found default statistics formatter " + formatter); 82 super.assertEquals(formatter, DEFAULT_FORMATTER); 83 84 } 85 86 91 public void testRawStatistics() throws Exception { 92 93 Object result = StatisticsHelper.listRawStatistics(getServer()); 94 95 assertTrue(result instanceof Serializable ); 96 assertTrue(result instanceof ManagedConnectionPoolStatistics); 97 98 } 99 100 public void testDefaultFormattedStatistics() throws Exception { 101 102 InitialContext initCtx = super.getInitialContext(); 103 DataSource ds = (DataSource )initCtx.lookup("StatsDS"); 104 Connection conn = ds.getConnection("sa", ""); 105 106 Object formattedStats = StatisticsHelper.listFormattedStatistics(getServer()); 107 108 assertTrue(formattedStats instanceof String ); 109 110 Object rawStatistics = StatisticsHelper.listRawStatistics(getServer()); 112 113 StatisticsFormatter defaultFormatter = StatisticsHelper.getDefaultFormatter(); 114 115 ManagedConnectionPoolStatistics stats = (ManagedConnectionPoolStatistics)rawStatistics; 116 117 String rawFormat = (String )defaultFormatter.formatStatistics((JBossStatistics)stats); 118 119 assertEquals(formattedStats, rawFormat); 120 121 conn.close(); 122 123 124 } 125 126 public void testXmlFormatterStatistics() throws Exception { 127 128 InitialContext initCtx = super.getInitialContext(); 129 DataSource ds = (DataSource )initCtx.lookup("StatsDS"); 130 Connection conn = ds.getConnection("sa", ""); 131 132 setStatisticsFormatter(XML_FORMATTER); 133 Object formattedStats = (String )getServer().invoke(POOL_NAME, FORMATTED_STATS_METHOD, new Object [0], new String [0]); 134 135 assertTrue(formattedStats instanceof String ); 136 137 String xml = (String )formattedStats; 138 139 StringReader reader = new StringReader (xml); 140 InputSource source = new InputSource (reader); 141 142 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source); 143 144 ManagedConnectionPoolStatistics rawStatistics = (ManagedConnectionPoolStatistics)getServer().invoke(POOL_NAME, RAW_STATS_METHOD, new Object [0], new String [0]); 145 JBossXmlSubPoolStatisticFormatter xmlFormatter = new JBossXmlSubPoolStatisticFormatter(); 146 String xml2 = (String )xmlFormatter.formatSubPoolStatistics(rawStatistics); 147 assertEquals(xml, xml2); 148 149 conn.close(); 150 151 } 152 153 public void testInvalidFormatter() throws Exception { 154 155 ObjectName name = new ObjectName ("jboss.jca:service=ManagedConnectionPool,name=StatsDS"); 156 157 InitialContext initCtx = super.getInitialContext(); 158 DataSource ds = (DataSource )initCtx.lookup("StatsDS"); 159 Connection conn = ds.getConnection("sa", ""); 160 setStatisticsFormatter("Invalid"); 161 162 Object formattedStats = (String )getServer().invoke(POOL_NAME, FORMATTED_STATS_METHOD, new Object [0], new String [0]); 163 164 assertTrue(formattedStats instanceof String ); 165 166 JBossDefaultSubPoolStatisticFormatter defaultFormatter = new JBossDefaultSubPoolStatisticFormatter(); 167 168 Object rawStatistics = listRawStatistics(); 169 ManagedConnectionPoolStatistics stats = (ManagedConnectionPoolStatistics)rawStatistics; 170 171 String rawFormat = (String )defaultFormatter.formatSubPoolStatistics(stats); 172 173 assertEquals(formattedStats, rawFormat); 174 175 } 176 177 178 179 private Object listRawStatistics() throws Exception { 180 181 return getServer().invoke(POOL_NAME, RAW_STATS_METHOD, new Object [0], new String [0]); 182 183 } 184 185 private void setStatisticsFormatter(String formatter) throws Exception { 186 187 getServer().setAttribute(POOL_NAME, new Attribute (ATTRIBUTE_NAME, formatter)); 188 189 } 190 191 private String getStatisticsFormatter() throws Exception { 192 193 return (String )getServer().getAttribute(POOL_NAME, ATTRIBUTE_NAME); 194 195 } 196 197 public static Test suite() throws Exception 198 { 199 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 200 URL resURL = loader.getResource("jca/stats/default-stats-ds.xml"); 201 return getDeploySetup(StatisticsFormatterUnitTestCase.class, resURL.toString()); 202 } 203 } 204 | Popular Tags |