1 /*------------------------------------------------------------------------------
  2 Name:      TestJmsAdmin.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test.jms;
  7 
  8 import java.util.Hashtable;
  9 
 10 import org.custommonkey.xmlunit.XMLTestCase;
 11 import org.custommonkey.xmlunit.XMLUnit;
 12 
 13 import javax.naming.InitialContext;
 14 import javax.naming.NamingException;
 15 
 16 import org.apache.naming.NamingService;
 17 import java.util.logging.Logger;
 18 import java.util.logging.Level;
 19 import org.xmlBlaster.util.Global;
 20 
 21 import org.xmlBlaster.client.qos.ConnectQos;
 22 import org.xmlBlaster.jms.XBConnectionFactory;
 23 import org.xmlBlaster.jms.XBPropertyNames;
 24 import org.xmlBlaster.jms.XBDestination;
 25 
 26 /**
 27  * Test JmsAdmin. 
 28  * <p />
 29  * All methods starting with 'test' and without arguments are invoked automatically
 30  * <p />
 31  * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.TestJmsAdmin
 32  * @see org.xmlBlaster.util.qos.ConnectQosData
 33  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/jms.html" target="others">the jms requirement</a>
 34  * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
 35  */
 36 public class TestJmsAdmin extends XMLTestCase {
 37    private final static String CONNECTION_FACTORY = "connectionFactory";
 38    private final static String TOPIC = "jmsAdmin";
 39    protected Global glob;
 40    private static Logger log = Logger.getLogger(TestJmsAdmin.class.getName());
 41    
 42    private String[] args;
 43    private NamingService namingService;
 44    private Hashtable env;
 45    private ConnectQos qos;
 46    
 47    
 48    public TestJmsAdmin(String name) throws Exception {
 49       super(name);
 50       XMLUnit.setIgnoreWhitespace(true);
 51       try {
 52          this.namingService = new NamingService();
 53          this.namingService.start(); 
 54       }
 55       catch (Exception ex) {
 56          ex.printStackTrace();
 57          assertTrue("exception in constructor when starting naming service", false);
 58       }
 59 
 60       this.qos = new ConnectQos(this.glob);
 61       this.qos.addClientProperty("one", "1");
 62       this.qos.setPersistent(true);
 63       this.qos.setMaxSessions(100000);
 64       this.env = new Hashtable();
 65       this.env.put(XBPropertyNames.CONNECT_QOS, qos.toXml());
 66    
 67    }
 68 
 69    public void finalize() {
 70       this.namingService.stop(); 
 71    }
 72 
 73    public void prepare(String[] args) {
 74       //this.args = args;
 75       this.glob = new Global(args);
 76    }
 77 
 78    protected void setUp() {
 79       this.glob = Global.instance();
 80 
 81       adminJmsStart();
 82    }
 83 
 84    protected void tearDown() {
 85       try {
 86          InitialContext ctx = new InitialContext(this.env);
 87          ctx.unbind(CONNECTION_FACTORY);
 88          ctx.unbind(TOPIC);
 89       }
 90       catch (NamingException ex) {
 91          ex.printStackTrace();
 92          assertTrue("exception when unbinding", false);
 93       }
 94    }
 95    
 96    protected void adminJmsStart() {
 97       try {
 98          // System.setProperty("java.naming.factory.initial", "org.apache.naming.modules.memory.MemoryURLContextFactory");
 99          // System.setProperty("java.naming.factory.url.pkgs", "org.apache.naming.modules");
100          InitialContext ctx = new InitialContext(this.env);
101          ctx.bind(CONNECTION_FACTORY, new XBConnectionFactory(null, this.args, false));            
102          ctx.bind(TOPIC, new XBDestination(TOPIC, null, false));
103       }
104       catch (NamingException ex) {
105          ex.printStackTrace();
106          assertTrue("exception occured in testJndi", false);
107       }
108       catch (Exception ex) {
109          ex.printStackTrace();
110          assertTrue("exception when starting naming service", false);
111       }
112    }
113    
114    /**
115     * Checks if the connectQos passed to the initial context also reaches the ConnectionFactrory correcty
116     *
117     */
118    public void testConnectionFactory() {
119       try {
120 
121          InitialContext ctx = new InitialContext(this.env);
122          XBConnectionFactory factory = (XBConnectionFactory)ctx.lookup(CONNECTION_FACTORY);
123          ConnectQos qos1 = factory.getConnectQos();
124 
125          if (log.isLoggable(Level.FINE)) {
126             System.out.println("--------------------------------------");
127             System.out.println(qos.toXml());
128             System.out.println("--------------------------------------");
129             System.out.println(qos1.toXml());
130             System.out.println("--------------------------------------");
131          }
132          
133          assertXMLEqual(qos.toXml(), qos1.toXml());
134       }
135       catch (Exception ex) {
136          ex.printStackTrace();
137          assertTrue("naming exception", false);
138       }
139    }
140 
141    /**
142     * <pre>
143     *  java org.xmlBlaster.test.classtest.TestJmsAdmin
144     * </pre>
145     */
146    public static void main(String args[])
147    {
148       try {
149          TestJmsAdmin test = new TestJmsAdmin("TestJmsAdmin");
150          test.prepare(args);
151          test.setUp();
152          test.testConnectionFactory();
153          test.tearDown();
154       }
155       catch (Exception e) {
156          System.out.println("TEST FAILED: " + e.toString());
157       }
158    }
159 }


syntax highlighted by Code2HTML, v. 0.9.1