📄 sql92selectortest.java
字号:
/*------------------------------------------------------------------------------Name: Sql92SelectorTest.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE file------------------------------------------------------------------------------*/package org.xmlBlaster.test.classtest;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import junit.framework.TestCase;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.Global;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.lexical.LikeOpWrapper;import org.xmlBlaster.util.lexical.Sql92Selector;import org.xmlBlaster.util.qos.ClientProperty;/** * Test ClientProperty. * <p /> * All methods starting with 'test' and without arguments are invoked automatically * <p /> * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.Sql92SelectorTest * @see org.xmlBlaster.util.qos.ClientProperty * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/engine.qos.clientProperty.html">The client.qos.clientProperty requirement</a> */public class Sql92SelectorTest extends TestCase { private final static String ME = "Sql92SelectorTest"; protected Global glob; private static Logger log = Logger.getLogger(Sql92SelectorTest.class.getName()); private Map[] dataSet; private boolean[][] resultSet; private String[] querySet; public Sql92SelectorTest(String name) { this(null, name); } public Sql92SelectorTest(Global global, String name) { super(name); if (global == null) this.glob = Global.instance(); else this.glob = global; } protected void setUp() { setupDataSets(); } protected void tearDown() { } /** * Change this method if you want to add a new data set to be checked * */ private void setupDataSets() { ArrayList datas = new ArrayList(); Map map; String encoding = null; ClientProperty prop1, prop2, prop3; prop1 = new ClientProperty("age" , "integer", encoding, "23" ); prop2 = new ClientProperty("city" , null, encoding, "London" ); prop3 = new ClientProperty("amount", "double", encoding, "100.1234567"); // set : 0 (0:0:0) map = new HashMap(); datas.add(map); // set : 1 (0:0:1) map = new HashMap(); map.put("amount", prop3); datas.add(map); // set : 2 (0:1:0) map = new HashMap(); map.put("city" , prop2); datas.add(map); // set : 3 (0:1:1) map = new HashMap(); map.put("city" , prop2); map.put("amount", prop3); datas.add(map); // set : 4 (1:0:0) map = new HashMap(); map.put("age" , prop1); datas.add(map); // set : 5 (1:0:1) map = new HashMap(); map.put("age" , prop1); map.put("amount", prop3); datas.add(map); // set : 6 (1:1:0) map = new HashMap(); map.put("age" , prop1); map.put("city" , prop2); datas.add(map); // set : 7 (1:1:1) map = new HashMap(); map.put("age" , prop1); map.put("city" , prop2); map.put("amount", prop3); datas.add(map); this.dataSet = (Map[])datas.toArray(new Map[datas.size()]); } /** * Checks if the provided data sets, the queries and the results are * consistent with eachother (this is invoked before starting the real testing) */ private void consistencyCheck() { int numData = this.dataSet.length; int numQueries = this.querySet.length; int numResults = this.resultSet.length; assertEquals("The number of queries '" + numQueries + "' differes from the number of results '" + numResults + "'", numQueries, numResults); for (int i=0; i < numResults; i++) { assertEquals("The number of results for query '" + i + "' is wrong", numData, this.resultSet[i].length); } } private String getDataAsText(int pos) { Map map = this.dataSet[pos]; StringBuffer buffer = new StringBuffer("["); Object[] keys = map.keySet().toArray(); for (int i=0; i < keys.length; i++) { if (i != 0) buffer.append(";"); buffer.append(keys[i]).append("="); ClientProperty cp = (ClientProperty)map.get(keys[i]); String tmp = "null"; if (cp != null) tmp = cp.getStringValue(); buffer.append(tmp); } buffer.append("]"); return buffer.toString(); } /** * This is the fully automatized initial (general) test. Since it is * difficult to predict all possible problems, additional tests should * be added once a bug is encountered. For each such bug an own test * method should be added. */ private void selectorPerformTest() { // for each data set one selector consistencyCheck(); /* Sql92Selector[] selectors = new Sql92Selector[this.dataSet.length]; for (int i=0; i < this.dataSet.length; i++) { if (log.isLoggable(Level.FINE)) log.fine("testSelectorStandard: creating selector nr. " + i); selectors[i] = new Sql92Selector(this.glob); } */ Sql92Selector selector = new Sql92Selector(this.glob); for (int i=0; i < this.querySet.length; i++) { String query = this.querySet[i]; log.info("testSelectorStandard: process query '" + query + "'"); boolean[] shouldAnswers = this.resultSet[i]; for (int j=0; j < this.dataSet.length; j++) { if (log.isLoggable(Level.FINE)) log.fine("testSelectorStandard: query '" + query + "' on set '" + getDataAsText(j)); try { boolean response = selector.select(query, this.dataSet[j]); assertEquals("wrong answer for query '" + i + "'\"" + query + "\" on set '" + j + "' " + getDataAsText(j), shouldAnswers[j], response); } catch (XmlBlasterException ex) { ex.printStackTrace(); assertTrue("An exception should not occur on query '" + i + "'\"" + query + "\" for dataset " + getDataAsText(j), false); } } } } /** * * @return the milliseconds per request */ private void performanceCheck() { if (log.isLoggable(Level.FINER)) log.finer("performanceCheck"); // for each data set one selector consistencyCheck(); /* Sql92Selector[] selectors = new Sql92Selector[this.dataSet.length]; for (int i=0; i < this.dataSet.length; i++) { selectors[i] = new Sql92Selector(this.glob); } */ Sql92Selector selector = new Sql92Selector(this.glob); try { int kmax = 100; long t0 = System.currentTimeMillis(); for (int k=0; k < kmax; k++) { for (int i=0; i < this.querySet.length; i++) { String query = this.querySet[i]; for (int j=0; j < this.dataSet.length; j++) { boolean response = selector.select(query, this.dataSet[j]); } } } long dt = System.currentTimeMillis() - t0; int nmax = kmax * this.dataSet.length * this.querySet.length; log.info("performance: '" + nmax + "' requests in '" + dt + "' ms"); double ret = 1.0 * dt / nmax; log.info("performance: '" + ret + "' ms per request"); log.info("performance: '" + ((int)(1000.0 / ret)) + "' request per second (rps)"); } catch (XmlBlasterException ex) { ex.printStackTrace(); } } public void interactive() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("\n- input new query: "); while (true) { try { String line = br.readLine(); if (line == null) break; System.out.print("Result: "); for (int i=0; i < this.dataSet.length; i++) { Sql92Selector selector = new Sql92Selector(this.glob); boolean ret = selector.select(line, this.dataSet[i]); System.out.print(ret + "\t"); } } catch (Exception ex) { ex.printStackTrace(); } System.out.println("\n- input new query: "); } } // THE TESTING METHODS COME HERE .....
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -