/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
package KFM.GUI.test;
import junit.framework.*;
import java.util.*;
import KFM.GUI.HttpParams;
public class TestHttpParams extends TestCase {
public TestHttpParams(String s) {
super(s);
}
protected void setUp() {
}
protected void tearDown() {
}
public void testConstructor1()
{
// according to rfc1738 this is a valid URL
String tURLString = "http://host/path";
new HttpParams(tURLString);
}
public void testConstructor2()
{
// according to rfc1738 this is a valid URL
String tURLString = "http://host/path?";
new HttpParams(tURLString);
}
public void testConstructor3()
{
// we allow anchors in the URL
String tURLString = "http://host/path#anchor";
new HttpParams(tURLString);
}
public void testConstructor4()
{
// we allow anchors in the URL
String tURLString = "http://host/path?a=b&c=d#anchor";
new HttpParams(tURLString);
}
public void testAddParam() {
HttpParams httpparams = new HttpParams();
String aName1= "a";
String aValue2= "b";
httpparams.addParam(aName1, aValue2);
assertTrue("addParam did not addParam('a', 'b')", httpparams.getParam(aName1).equals(aValue2));
}
public void testAddParam2() {
HttpParams httpparams = new HttpParams("http://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d");
String aName1= "a";
String aValue2= "e";
httpparams.addParam(aName1, aValue2);
assertTrue("addParam did not addParam('a', 'b') to existing key 'a'", httpparams.getParameterValues(aName1).length == 2);
}
public void testGetParam() {
HttpParams httpparams = new HttpParams();
String aName1= "a";
String stringRet = httpparams.getParam(aName1);
assertTrue("getParam() with not existing key returned " + stringRet, stringRet.equals(""));
}
public void testGetParam2() {
HttpParams httpparams = new HttpParams();
String aName1= "";
String stringRet = httpparams.getParam(aName1);
assertTrue("getParam() with empty key returned " + stringRet, stringRet.equals(""));
}
public void testGetParam3() {
HttpParams httpparams = new HttpParams();
String aName1= null;
try {
String stringRet = httpparams.getParam(aName1);
fail("getParam() with null key did not throw a NullPointerException");
}
catch (NullPointerException e) {
}
}
public void testGetParam4() {
HttpParams httpparams = new HttpParams("http://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=&d=e");
String aName1= "c";
assertTrue("", httpparams.getParam(aName1).equals(""));
}
public void testGetParameterNames() {
HttpParams httpparams = new HttpParams("http://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e");
Enumeration enumerationRet = httpparams.getParameterNames();
int size = 0;
while (enumerationRet.hasMoreElements()) {
enumerationRet.nextElement();
size++;
}
assertTrue("Found wrong number of parameter names. Expected 2, found " + size, size == 2);
}
public void testGetParameterValues() {
HttpParams httpparams = new HttpParams("http://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e");
String aName1= "a";
String[] ret = httpparams.getParameterValues(aName1);
assertTrue("Found wrong number of parameter values. Expected 2, found" + ret.length, ret.length == 2);
}
public void testGetProtocol() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e");
String stringRet = httpparams.getProtocol();
assertTrue("Found wrong protocol: " + stringRet, stringRet.equals("https"));
}
public void testGetProtocol2() {
HttpParams httpparams = new HttpParams();
String stringRet = httpparams.getProtocol();
assertTrue("getProtocol() with empty constructor did not return null but: " + stringRet, stringRet == null);
}
public void testRemoveParam() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e");
String aName1= "a";
httpparams.removeParam(aName1);
assertTrue("removed " + aName1 + " but getParam returned value " + httpparams.getParam(aName1), httpparams.getParam(aName1).equals(""));
}
public void testRemoveParam2() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e");
String aName1= "x";
Enumeration tNames = httpparams.getParameterNames();
int sizeBefore = 0;
while (tNames.hasMoreElements()) {
tNames.nextElement();
sizeBefore++;
}
httpparams.removeParam(aName1);
tNames = httpparams.getParameterNames();
int sizeAfter = 0;
while (tNames.hasMoreElements()) {
tNames.nextElement();
sizeAfter++;
}
assertTrue("removed not existing param" + aName1 + " but number of param names differs", sizeBefore == sizeAfter);
}
public void testRemoveParam3() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e");
String aName1= "a";
String aValue2= "e";
httpparams.removeParam(aName1, aValue2);
String[] tValues = httpparams.getParameterValues(aName1);
for (int i = 0; i < tValues.length; i++) {
if (tValues[i].equals(aValue2)) {
fail("removed" + aName1 + " with value " + aValue2 + " but it is still there.");
}
}
}
public void testReplaceParam() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e");
String aName1= "a";
String aValue2= "r";
httpparams.replaceParam(aName1, aValue2);
assertTrue("replacement of " + aName1 + " by " + aValue2 + "failed:", httpparams.getParam(aName1).equals(aValue2));
}
public void testGetAnchor1() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap?a=b&c=d&a=e#anchor");
String aName1= "anchor";
assertTrue("Anchor missing, expected #" + aName1, httpparams.getAnchor().equals(aName1));
}
public void testGetAnchor2() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap#anchor");
String aName1= "anchor";
assertTrue("Anchor missing, expected #" + aName1, httpparams.getAnchor().equals(aName1));
}
public void testSetAnchor() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap");
String aName1= "anchor";
httpparams.setAnchor(aName1);
assertTrue("Setting Anchor failed, expected #" + aName1, httpparams.getAnchor().equals(aName1));
}
public void testToUrlEncodedQueryString() {
HttpParams httpparams = new HttpParams("https://www.siemens.com/pfad/zum/servlet/SieMap?a=B+e%72nd&a=%2520");
String stringRet = httpparams.toUrlEncodedQueryString();
boolean tAssertion = stringRet.equals("a=B%20ernd&a=%2520") || stringRet.equals("a=%2520&a=B%20ernd");
assertTrue("Got wrong QueryString, expected 'a=Bernd&c=abc%20def&a=%2520', found: " + stringRet, tAssertion);
}
public static Test suite() {
// * Create a dummy object of this class
TestHttpParams tObj = new TestHttpParams(null);
TestSuite suite = null;
// Create a new TestSuite with name of this class
// which contains all basic tests in this class (void test*())
suite = new TestSuite("TestHttpParams");
suite.addTest(new TestSuite(TestHttpParams.class));
// These are the Parameters which should be passed to a parameterized test
// Create a new TestSuite which contains all individual tests which differ
// only in the parameters passed to it. In this case it will result in
// subTestValue
// +- subTestValue(Integer(1), Integer(2))
// +- subTestValue(Integer(3), Integer(3))
//suite.addTest(new TestSuiteParams(
// "testXML_ImportParameters", tParams, BookmarkTest.class));
return suite;
}
}