1 package org.xmlBlaster.test.classtest.qos;
  2 
  3 import java.util.logging.Logger;
  4 import java.util.logging.Level;
  5 import org.xmlBlaster.util.Global;
  6 import org.xmlBlaster.util.XmlBlasterException;
  7 import org.xmlBlaster.util.qos.StatusQosData;
  8 import org.xmlBlaster.util.qos.I_StatusQosFactory;
  9 import org.xmlBlaster.util.qos.StatusQosSaxFactory;
 10 import org.xmlBlaster.util.qos.StatusQosQuickParseFactory;
 11 import org.xmlBlaster.client.qos.GetReturnQos;
 12 import org.xmlBlaster.client.qos.UpdateQos;
 13 import org.xmlBlaster.util.def.Constants;
 14 
 15 import junit.framework.*;
 16 
 17 /**
 18  * Test I_StatusQosFactory implementations. 
 19  * <p />
 20  * <pre>
 21  * java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.qos.StatusQosFactoryTest
 22  * </pre>
 23  * @see org.xmlBlaster.util.qos.StatusQosSaxFactory
 24  * @see org.xmlBlaster.util.qos.StatusQosQuickParseFactory
 25  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">the xmlBlaster access interface requirement</a>
 26  */
 27 public class StatusQosFactoryTest extends TestCase {
 28    private String ME = "StatusQosFactoryTest";
 29    protected final Global glob;
 30    private static Logger log = Logger.getLogger(StatusQosFactoryTest.class.getName());
 31    private String currImpl;
 32    private I_StatusQosFactory factory;
 33    static I_StatusQosFactory[] IMPL = { 
 34                    new org.xmlBlaster.util.qos.StatusQosSaxFactory(Global.instance()),
 35                    new org.xmlBlaster.util.qos.StatusQosQuickParseFactory(Global.instance())
 36                  };
 37 
 38    public StatusQosFactoryTest(Global glob, String name, int currImpl) {
 39       super(name);
 40       this.glob = glob;
 41 
 42       this.factory = IMPL[currImpl];
 43    }
 44 
 45    protected void setUp() {
 46       log.info("Testing parser factory " + factory.getName());
 47    }
 48 
 49    /**
 50     * Tries with all known tags
 51     */
 52    public void testParse() {
 53       System.out.println("***StatusQosFactoryTest: testParse ...");
 54       
 55       try {
 56          String xml =
 57             "<qos>\n" +
 58             "   <state id='ERASED' info='QUEUED[bilbo]'/>\n" +
 59             "   <key oid='yourMessageOid'/>\n" +
 60             "   <subscribe id='_subId:1'/>\n" +
 61             "</qos>\n";
 62 
 63          StatusQosData qos = factory.readObject(xml);
 64 
 65          assertEquals("", Constants.STATE_ERASED, qos.getState());
 66          assertEquals("", false, qos.isOk());
 67          assertEquals("", true, qos.isErased());
 68          assertEquals("", false, qos.isTimeout());
 69          assertEquals("", false, qos.isForwardError());
 70          assertEquals("", "QUEUED[bilbo]", qos.getStateInfo());
 71          assertEquals("", "yourMessageOid", qos.getKeyOid());
 72          assertEquals("", "_subId:1", qos.getSubscriptionId());
 73       }
 74       catch (XmlBlasterException e) {
 75          fail("testParse failed: " + e.toString());
 76       }
 77 
 78       System.out.println("***StatusQosFactoryTest: testParse [SUCCESS]");
 79    }
 80 
 81    /**
 82     * Test toXml (parse - createXml - parse again - test)
 83     */
 84    public void testToXml() {
 85       System.out.println("***StatusQosFactoryTest: testToXml ...");
 86       
 87       try {
 88          String xml =
 89             "<qos>\n" +
 90             "   <state id='ERASED' info='QUEUED[bilbo]'/>\n" +
 91             "   <key oid='yourMessageOid'/>\n" +
 92             "   <subscribe id='_subId:1'/>\n" +
 93             "</qos>\n";
 94 
 95          StatusQosData qos = factory.readObject(xml);
 96          String newXml = qos.toXml();
 97          log.info("New XML=" + newXml);
 98          qos = factory.readObject(newXml);
 99 
100          assertEquals("", Constants.STATE_ERASED, qos.getState());
101          assertEquals("", false, qos.isOk());
102          assertEquals("", true, qos.isErased());
103          assertEquals("", false, qos.isTimeout());
104          assertEquals("", false, qos.isForwardError());
105          assertEquals("", "QUEUED[bilbo]", qos.getStateInfo());
106          assertEquals("", "yourMessageOid", qos.getKeyOid());
107          assertEquals("", "_subId:1", qos.getSubscriptionId());
108       }
109       catch (XmlBlasterException e) {
110          fail("testToXml failed: " + e.toString());
111       }
112 
113       System.out.println("***StatusQosFactoryTest: testToXml [SUCCESS]");
114    }
115 
116    /**
117     * Tests empty xml string
118     */
119    public void testDefault() {
120       System.out.println("***StatusQosFactoryTest: testDefault ...");
121       
122       try {
123          StatusQosData qos = factory.readObject((String)null);
124 
125          assertEquals("", Constants.STATE_OK, qos.getState());
126          assertEquals("", true, qos.isOk());
127          assertEquals("", false, qos.isErased());
128          assertEquals("", false, qos.isTimeout());
129          assertEquals("", false, qos.isForwardError());
130          assertEquals("", null, qos.getStateInfo());
131          assertEquals("", null, qos.getKeyOid());
132          assertEquals("", null, qos.getSubscriptionId());
133       }
134       catch (XmlBlasterException e) {
135          fail("testDefault failed: " + e.toString());
136       }
137 
138       System.out.println("***StatusQosFactoryTest: testDefault [SUCCESS]");
139    }
140 
141    /**
142     * Tries with all known tags
143     */
144    public void testPerformance() {
145       System.out.println("***StatusQosFactoryTest: testPerformance ...");
146       
147       try {
148          String xml =
149             "<qos>\n" +
150             "   <state id='ERASED' info='QUEUED[bilbo]'/>\n" +
151             "   <key oid='yourMessageOid'/>\n" +
152             "   <subscribe id='_subId:1'/>\n" +
153             "</qos>\n";
154 
155          for (int j=0; j<5; j++) {
156             int num = 1000;
157             long start = System.currentTimeMillis();
158             for (int i=0; i<num; i++) {
159                StatusQosData qos = factory.readObject(xml);
160             }
161             long elapsed = System.currentTimeMillis() - start;
162             log.info(num + " parses for " + factory.getName() + ": " + elapsed + " millisec -> " +
163                      ((((double)elapsed)*1000.*1000.)/((double)num)) + " nanosec/parse");
164          }
165       }
166       catch (XmlBlasterException e) {
167          fail("testPerformance failed: " + e.toString());
168       }
169 
170       System.out.println("***StatusQosFactoryTest: testPerformance [SUCCESS]");
171    }
172 
173    /**
174     * Method is used by TestRunner to load these tests
175     */
176    public static Test suite()
177    {
178       TestSuite suite= new TestSuite();
179       Global glob = new Global();
180       for (int i=0; i<IMPL.length; i++) {
181          suite.addTest(new StatusQosFactoryTest(glob, "testDefault", i));
182          suite.addTest(new StatusQosFactoryTest(glob, "testParse", i));
183          suite.addTest(new StatusQosFactoryTest(glob, "testToXml", i));
184          suite.addTest(new StatusQosFactoryTest(glob, "testPerformance", i));
185       }
186       return suite;
187    }
188 
189    /**
190     * <pre>
191     *  java org.xmlBlaster.test.classtest.qos.StatusQosFactoryTest
192     * </pre>
193     */
194    public static void main(String args[]) {
195       Global glob = new Global(args);
196       for (int i=0; i<IMPL.length; i++) {
197          StatusQosFactoryTest testSub = new StatusQosFactoryTest(glob, "StatusQosFactoryTest", i);
198          testSub.setUp();
199          testSub.testDefault();
200          testSub.testParse();
201          testSub.testPerformance();
202          testSub.testToXml();
203          //testSub.tearDown();
204       }
205    }
206 }


syntax highlighted by Code2HTML, v. 0.9.1