KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > test > server > XmlRpcServerTest


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.net.InetAddress JavaDoc;
40 import java.net.URL JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Properties JavaDoc;
44 import java.util.prefs.Preferences JavaDoc;
45
46 public class XmlRpcServerTest extends TestCase {
47   private static final String JavaDoc ADMIN_USER = "ADMIN";
48   private static final String JavaDoc ADMIN_PASS = "123TM456";
49   private static String JavaDoc 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 JavaDoc {
57         oneTimeSetup();
58       }
59
60       protected void tearDown() throws Exception JavaDoc {
61         oneTimeTearDown();
62       }
63     };
64
65     return testSetup;
66   }
67
68   protected static void oneTimeSetup() throws Exception JavaDoc {
69     Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
70
71     Properties JavaDoc config = new Properties JavaDoc();
72     config.load(XmlRpcServerTest.class.getResourceAsStream("/conf/snipsnap.conf"));
73
74     Iterator JavaDoc configIt = config.keySet().iterator();
75     while (configIt.hasNext()) {
76       String JavaDoc key = (String JavaDoc) configIt.next();
77       serverPrefs.put(key, config.getProperty(key));
78     }
79
80     File JavaDoc 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 JavaDoc url = new URL JavaDoc(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 JavaDoc {
99     Preferences JavaDoc serverPrefs = Preferences.userNodeForPackage(ServerConfiguration.class);
100     serverPrefs.removeNode();
101     serverPrefs.flush();
102     xmlRpcServer.shutdown();
103   }
104
105   protected void setUp() throws Exception JavaDoc {
106     xmlRpcClient = new AdminXmlRpcClient(admin_url, ADMIN_USER, ADMIN_PASS);
107   }
108
109   public void testXmlRpcInstall() throws IOException JavaDoc {
110     try {
111       URL JavaDoc url = new URL JavaDoc("http://" + InetAddress.getLocalHost().getHostName() + ":8668/?key=");
112       URL JavaDoc 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 JavaDoc {
120     try {
121       Hashtable JavaDoc 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 JavaDoc {
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