1 25 package org.snipsnap.test.server; 26 27 import junit.extensions.TestSetup; 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 import org.apache.xmlrpc.WebServer; 32 import org.apache.xmlrpc.XmlRpcException; 33 import org.snipsnap.config.ServerConfiguration; 34 import org.snipsnap.server.AdminXmlRpcClient; 35 import org.snipsnap.server.AdminXmlRpcHandler; 36 37 import java.io.File ; 38 import java.io.IOException ; 39 import java.net.InetAddress ; 40 import java.net.URL ; 41 import java.util.Hashtable ; 42 import java.util.Iterator ; 43 import java.util.Properties ; 44 import java.util.prefs.Preferences ; 45 46 public class XmlRpcServerTest extends TestCase { 47 private static final String ADMIN_USER = "ADMIN"; 48 private static final String ADMIN_PASS = "123TM456"; 49 private static String admin_url; 50 51 private static WebServer xmlRpcServer; 52 private AdminXmlRpcClient xmlRpcClient; 53 54 public static Test suite() { 55 TestSetup testSetup = new TestSetup(new TestSuite(XmlRpcServerTest.class)) { 56 protected void setUp() throws Exception { 57 oneTimeSetup(); 58 } 59 60 protected void tearDown() throws Exception { 61 oneTimeTearDown(); 62 } 63 }; 64 65 return testSetup; 66 } 67 68 protected static void oneTimeSetup() throws Exception { 69 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 70 71 Properties config = new Properties (); 72 config.load(XmlRpcServerTest.class.getResourceAsStream("/conf/snipsnap.conf")); 73 74 Iterator configIt = config.keySet().iterator(); 75 while (configIt.hasNext()) { 76 String key = (String ) configIt.next(); 77 serverPrefs.put(key, config.getProperty(key)); 78 } 79 80 File tmpFile = File.createTempFile("xmlrpctest", "root"); 81 tmpFile.deleteOnExit(); 82 tmpFile.delete(); 83 tmpFile.mkdirs(); 84 serverPrefs.put(ServerConfiguration.WEBAPP_ROOT, tmpFile.getPath()); 85 serverPrefs.put(ServerConfiguration.ADMIN_USER, ADMIN_USER); 86 serverPrefs.put(ServerConfiguration.ADMIN_PASS, ADMIN_PASS); 87 serverPrefs.flush(); 88 89 admin_url = serverPrefs.get(ServerConfiguration.ADMIN_URL, null); 90 URL url = new URL (admin_url); 91 92 xmlRpcServer = new WebServer(url.getPort()); 93 xmlRpcServer.addHandler("$default", new AdminXmlRpcHandler()); 94 xmlRpcServer.start(); 95 96 } 97 98 protected static void oneTimeTearDown() throws Exception { 99 Preferences serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class); 100 serverPrefs.removeNode(); 101 serverPrefs.flush(); 102 xmlRpcServer.shutdown(); 103 } 104 105 protected void setUp() throws Exception { 106 xmlRpcClient = new AdminXmlRpcClient(admin_url, ADMIN_USER, ADMIN_PASS); 107 } 108 109 public void testXmlRpcInstall() throws IOException { 110 try { 111 URL url = new URL ("http://" + InetAddress.getLocalHost().getHostName() + ":8668/?key="); 112 URL result = xmlRpcClient.install("test", "localhost", "8668", "/"); 113 assertTrue(result.toExternalForm().startsWith(url.toExternalForm())); 114 } catch (XmlRpcException e) { 115 fail("installation of application failed: " + e.getMessage()); 116 } 117 } 118 119 public void testXmlRpcApplicationList() throws IOException { 120 try { 121 Hashtable list = xmlRpcClient.getApplications(); 122 assertEquals(1, list.size()); 123 } catch (XmlRpcException e) { 124 fail("listing of applications failed: " + e.getMessage()); 125 } 126 } 127 128 public void testXmlRpcDelete() throws IOException { 129 try { 130 xmlRpcClient.delete("test", false); 131 } catch (XmlRpcException e) { 132 fail("deletion of application failed: " + e.getMessage()); 133 } 134 } 135 } 136 | Popular Tags |