/* ******************************************************************
Copyright 2010 Xu Hui Hui (http://xhh.me)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************** */
package me.xhh.utils;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import junit.framework.TestCase;
public class UtilTest extends TestCase {
public void testIsEqual() {
assertTrue(Util.isEqual(null, null));
assertTrue(Util.isEqual("", ""));
assertTrue(Util.isEqual(0, 0));
assertFalse(Util.isEqual(0, 0l));
assertTrue(Util.isEqual('c', 'c'));
assertTrue(Util.isEqual(this, this));
assertFalse(Util.isEqual("", null));
assertFalse(Util.isEqual(null, ""));
assertFalse(Util.isEqual(" ", ' '));
assertFalse(Util.isEqual(" ", "abc"));
Object a = new Object();
Object b = new Object();
assertTrue(Util.isEqual(a, a));
assertFalse(Util.isEqual(a, b));
assertFalse(Util.isEqual(b, a));
}
public void testNotEqual() {
assertFalse(Util.notEqual(null, null));
assertFalse(Util.notEqual("", ""));
assertFalse(Util.notEqual(0, 0));
assertTrue(Util.notEqual(0, 0l));
assertFalse(Util.notEqual('c', 'c'));
assertFalse(Util.notEqual(this, this));
assertTrue(Util.notEqual("", null));
assertTrue(Util.notEqual(null, ""));
assertTrue(Util.notEqual(null, ' '));
assertTrue(Util.notEqual(" ", ' '));
assertTrue(Util.notEqual(" ", "abc"));
Object a = new Object();
Object b = new Object();
assertFalse(Util.notEqual(a, a));
assertTrue(Util.notEqual(a, b));
assertTrue(Util.notEqual(b, a));
Random ran = new Random();
int c = ran.nextInt();
int d = ran.nextInt();
assertTrue(Util.notEqual(a, b) == !Util.isEqual(c, d));
}
public void test_buildLinkURL() {
assertEquals(Util.buildLinkURL(null, null, null), "/");
assertEquals(Util.buildLinkURL("", null, null), "/");
assertEquals(Util.buildLinkURL("http://localhost", null, null), "http://localhost/");
assertEquals(Util.buildLinkURL("http://localhost/", null, null), "http://localhost/");
assertEquals(Util.buildLinkURL("", new String[]{"with space"}, null), "/with+space/");
assertEquals(Util.buildLinkURL("", new String[]{"with+space"}, null), "/with%2Bspace/");
assertEquals(Util.buildLinkURL("", new String[]{"with%2Bspace/"}, null), "/with%252Bspace%2F/");
assertEquals(Util.buildLinkURL("", new String[]{"with", "space"}, null), "/with/space/");
assertEquals(Util.buildLinkURL("", new String[]{"with", null, "space", null}, null), "/with/space/");
Map<String, String> params = new HashMap<String, String>();
params.put("key", "value");
assertEquals(Util.buildLinkURL("", null, params), "/?key=value");
assertEquals(Util.buildLinkURL("http://localhost/api.php", null, params), "http://localhost/api.php?key=value");
params = new LinkedHashMap<String, String>();
params.put("key", "value ");
params.put("other", "info");
assertEquals(Util.buildLinkURL("", null, params), "/?key=value+&other=info");
}
}