1 package org.xmlBlaster.test.classloader;
  2 
  3 import java.util.logging.Logger;
  4 import org.xmlBlaster.util.*;
  5 import java.net.URL;
  6 import java.net.URLClassLoader;
  7 import org.xmlBlaster.client.qos.ConnectQos;
  8 import org.xmlBlaster.client.I_XmlBlasterAccess;
  9 import org.xmlBlaster.util.MsgUnit;
 10 
 11 
 12 import junit.framework.*;
 13 import java.net.*;
 14 
 15 /**
 16  * Test SNMP (simple network management protocol) to insert data.
 17  * <p />
 18  * All methods starting with 'test' and without arguments are invoked automatically
 19  * <p />
 20  * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.snmp.InsertTest
 21  *
 22  * @see org.xmlBlaster.engine.admin.extern.snmp.NodeEntryImpl
 23  */
 24 public class XmlBlasterClassloaderTest extends TestCase {
 25    protected Global glob;
 26    private static Logger log = Logger.getLogger(XmlBlasterClassloaderTest.class.getName());
 27    private MsgUnit msgUnit;     // a message to play with
 28 
 29    public XmlBlasterClassloaderTest(String name) {
 30       super(name);
 31    }
 32 
 33    protected void setUp() {
 34       this.glob = Global.instance();
 35 
 36       URLClassLoader cl = (URLClassLoader)this.getClass().getClassLoader();
 37       URL[] urls = cl.getURLs();
 38       String path = "";
 39       for( int i = 0; i < urls.length; i++ ) {
 40          String file = urls[i].getFile();
 41          // TODO: parser.jar is not used anymore, remove code below
 42          if( file.endsWith("parser.jar") ) {
 43             int pos = file.indexOf("parser.jar");
 44             path = urls[i].getProtocol()+"://"+file.substring(0,pos);
 45             break;
 46          }
 47       }
 48 
 49       // Add a xerces.jar at the beginning, so that CLASSPATH contains another XML-Parser in front.
 50       try {
 51          urls = new URL[2];
 52          urls[0] = new URL(path+"ant/xerces.jar");
 53          urls[1] = new URL(path+"xmlBlaster.jar");
 54       }
 55       catch (MalformedURLException ex) {
 56          log.severe("error. >>>"+ex.toString());
 57       }
 58 
 59       cl = cl.newInstance(urls);
 60       try {
 61          Class clazz = cl.loadClass("org.xmlBlaster.util.EmbeddedXmlBlaster");
 62       }
 63       catch (ClassNotFoundException ex) {
 64          assertTrue(ex.getMessage(), true);
 65       }
 66 
 67    }
 68 
 69    public void testClassloader() {
 70       System.out.println("***XmlBlasterClassloaderTest: testClassloader ...");
 71 
 72       try {
 73          EmbeddedXmlBlaster embed = EmbeddedXmlBlaster.startXmlBlaster(glob);
 74 
 75          I_XmlBlasterAccess conn = glob.getXmlBlasterAccess();
 76          ConnectQos qos = new ConnectQos(glob, "marcel", "secret");
 77          conn.connect( qos, null ); // Login to xmlBlaster
 78 
 79          // a sample message unit
 80          String xmlKey = "<key oid='123' contentMime='text/plain' contentMimeExtended='myMime'>\n" +
 81                            "   <TestLogin-AGENT>" +
 82                            "   </TestLogin-AGENT>" +
 83                            "</key>";
 84          String senderContent = "Some content";
 85          MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), "<qos></qos>");
 86          conn.publish(msgUnit);
 87 
 88       }
 89       catch (Throwable t) {
 90          assertTrue( "Some error occured:"+t.toString(), true );
 91       }
 92 
 93       System.out.println("***XmlBlasterClassloaderTest: testClassloader [SUCCESS]");
 94    }
 95 
 96 
 97    protected void tearDown() {
 98    }
 99 
100    /**
101     * <pre>
102     *  java org.xmlBlaster.test.snmp.InsertTest
103     * </pre>
104     */
105    public static void main(String args[])
106    {
107       XmlBlasterClassloaderTest testCl = new XmlBlasterClassloaderTest("XmlBlasterClassloaderTest");
108       testCl.setUp();
109       testCl.testClassloader();
110       testCl.tearDown();
111    }
112 }


syntax highlighted by Code2HTML, v. 0.9.1