Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test for UrlUtils #1906

Merged
merged 2 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alibaba.dubbo.common.utils;

import org.junit.Test;

import java.util.EmptyStackException;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class StackTest {
@Test
public void testOps() throws Exception {
Stack<String> stack = new Stack<String>();
stack.push("one");
assertThat(stack.get(0), equalTo("one"));
assertThat(stack.peek(), equalTo("one"));
assertThat(stack.size(), equalTo(1));
stack.push("two");
assertThat(stack.get(0), equalTo("one"));
assertThat(stack.peek(), equalTo("two"));
assertThat(stack.size(), equalTo(2));
assertThat(stack.set(0, "three"), equalTo("one"));
assertThat(stack.remove(0), equalTo("three"));
assertThat(stack.size(), equalTo(1));
assertThat(stack.isEmpty(), is(false));
assertThat(stack.get(0), equalTo("two"));
assertThat(stack.peek(), equalTo("two"));
assertThat(stack.pop(), equalTo("two"));
assertThat(stack.isEmpty(), is(true));
}

@Test
public void testClear() throws Exception {
Stack<String> stack = new Stack<String>();
stack.push("one");
stack.push("two");
assertThat(stack.isEmpty(), is(false));
stack.clear();
assertThat(stack.isEmpty(), is(true));
}

@Test(expected = EmptyStackException.class)
public void testIllegalPop() throws Exception {
Stack<String> stack = new Stack<String>();
stack.pop();
}

@Test(expected = EmptyStackException.class)
public void testIllegalPeek() throws Exception {
Stack<String> stack = new Stack<String>();
stack.peek();
}

@Test(expected = IndexOutOfBoundsException.class)
public void testIllegalGet() throws Exception {
Stack<String> stack = new Stack<String>();
stack.get(1);
}

@Test(expected = IndexOutOfBoundsException.class)
public void testIllegalSet() throws Exception {
Stack<String> stack = new Stack<String>();
stack.set(1, "illegal");
}

@Test(expected = IndexOutOfBoundsException.class)
public void testIllegalRemove() throws Exception {
Stack<String> stack = new Stack<String>();
stack.remove(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,151 @@
*/
package com.alibaba.dubbo.common.utils;

import junit.framework.TestCase;
import com.alibaba.dubbo.common.Constants;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StringUtilsTest extends TestCase {
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.junit.Assert.assertThat;

public class StringUtilsTest {
@Test
public void testIsBlank() throws Exception {
assertTrue(StringUtils.isBlank(null));
assertTrue(StringUtils.isBlank(""));
assertFalse(StringUtils.isBlank("abc"));
}

@Test
public void testIsEmpty() throws Exception {
assertTrue(StringUtils.isEmpty(null));
assertTrue(StringUtils.isEmpty(""));
assertFalse(StringUtils.isEmpty("abc"));
}

@Test
public void testIsNotEmpty() throws Exception {
assertFalse(StringUtils.isNotEmpty(null));
assertFalse(StringUtils.isNotEmpty(""));
assertTrue(StringUtils.isNotEmpty("abc"));
}

@Test
public void testIsEquals() throws Exception {
assertTrue(StringUtils.isEquals(null, null));
assertFalse(StringUtils.isEquals(null, ""));
assertTrue(StringUtils.isEquals("abc", "abc"));
assertFalse(StringUtils.isEquals("abc", "ABC"));
}

@Test
public void testIsInteger() throws Exception {
assertFalse(StringUtils.isInteger(null));
assertFalse(StringUtils.isInteger(""));
assertTrue(StringUtils.isInteger("123"));
}

@Test
public void testParseInteger() throws Exception {
assertThat(StringUtils.parseInteger(null), equalTo(0));
assertThat(StringUtils.parseInteger("123"), equalTo(123));
}

@Test
public void testIsJavaIdentifier() throws Exception {
assertThat(StringUtils.isJavaIdentifier(""), is(false));
assertThat(StringUtils.isJavaIdentifier("1"), is(false));
assertThat(StringUtils.isJavaIdentifier("abc123"), is(true));
assertThat(StringUtils.isJavaIdentifier("abc(23)"), is(false));
}

@Test
public void testExceptionToString() throws Exception {
assertThat(StringUtils.toString(new RuntimeException("abc")), containsString("java.lang.RuntimeException: abc"));
}

@Test
public void testExceptionToStringWithMessage() throws Exception {
String s = StringUtils.toString("greeting", new RuntimeException("abc"));
assertThat(s, containsString("greeting"));
assertThat(s, containsString("java.lang.RuntimeException: abc"));
}

@Test
public void testParseQueryString() throws Exception {
assertThat(StringUtils.getQueryStringValue("key1=value1&key2=value2", "key1"), equalTo("value1"));
assertThat(StringUtils.getQueryStringValue("key1=value1&key2=value2", "key2"), equalTo("value2"));
assertThat(StringUtils.getQueryStringValue("", "key1"), isEmptyOrNullString());
}

@Test
public void testGetServiceKey() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put(Constants.GROUP_KEY, "dubbo");
map.put(Constants.INTERFACE_KEY, "a.b.c.Foo");
map.put(Constants.VERSION_KEY, "1.0.0");
assertThat(StringUtils.getServiceKey(map), equalTo("dubbo/a.b.c.Foo:1.0.0"));
}

@Test
public void testToQueryString() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
String queryString = StringUtils.toQueryString(map);
assertThat(queryString, containsString("key1=value1"));
assertThat(queryString, containsString("key2=value2"));
}

@Test
public void testJoin() throws Exception {
String[] s = {"1", "2", "3"};
assertEquals(StringUtils.join(s), "123");
assertEquals(StringUtils.join(s, ','), "1,2,3");
assertEquals(StringUtils.join(s, ","), "1,2,3");
}

@Test
public void testSplit() throws Exception {
String s = "d,1,2,4";
assertEquals(StringUtils.split(s, ',').length, 4);
}

@Test
public void testTranslat() throws Exception {
String s = "16314";
assertEquals(StringUtils.translat(s, "123456", "abcdef"), "afcad");
assertEquals(StringUtils.translat(s, "123456", "abcd"), "acad");
}

public void testJoin_Colletion_String() throws Exception {
@Test
public void testIsContains() throws Exception {
assertThat(StringUtils.isContains("a,b, c", "b"), is(true));
assertThat(StringUtils.isContains("", "b"), is(false));
assertThat(StringUtils.isContains(new String[]{"a", "b", "c"}, "b"), is(true));
assertThat(StringUtils.isContains((String[]) null, null), is(false));
}

@Test
public void testIsNumeric() throws Exception {
assertThat(StringUtils.isNumeric("123"), is(true));
assertThat(StringUtils.isNumeric("1a3"), is(false));
assertThat(StringUtils.isNumeric(null), is(false));
}

@Test
public void testJoinCollectionString() throws Exception {
List<String> list = new ArrayList<String>();
assertEquals("", StringUtils.join(list, ","));

Expand All @@ -60,4 +180,12 @@ public void testCamelToSplitName() throws Exception {
assertEquals("ab-cd-ef", StringUtils.camelToSplitName("ab-cd-ef", "-"));
assertEquals("abcdef", StringUtils.camelToSplitName("abcdef", "-"));
}

@Test
public void testToArgumentString() throws Exception {
String s = StringUtils.toArgumentString(new Object[]{"a", 0, Collections.singletonMap("enabled", true)});
assertThat(s, containsString("a,"));
assertThat(s, containsString("0,"));
assertThat(s, containsString("{\"enabled\":true}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import java.util.Map;
import java.util.Set;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class UrlUtilsTest {
Expand Down Expand Up @@ -335,4 +337,22 @@ public void testIsServiceKeyMatch() throws Exception {
pattern = pattern.addParameter(Constants.VERSION_KEY, "*");
assertTrue(UrlUtils.isServiceKeyMatch(pattern, value));
}

@Test
public void testGetEmptyUrl() throws Exception {
URL url = UrlUtils.getEmptyUrl("dubbo/a.b.c.Foo:1.0.0", "test");
assertThat(url.toFullString(), equalTo("empty://0.0.0.0/a.b.c.Foo?category=test&group=dubbo&version=1.0.0"));
}

@Test
public void testIsMatchGlobPattern() throws Exception {
assertTrue(UrlUtils.isMatchGlobPattern("*", "value"));
assertTrue(UrlUtils.isMatchGlobPattern("", null));
assertFalse(UrlUtils.isMatchGlobPattern("", "value"));
assertTrue(UrlUtils.isMatchGlobPattern("value", "value"));
assertTrue(UrlUtils.isMatchGlobPattern("v*", "value"));
assertTrue(UrlUtils.isMatchGlobPattern("*e", "value"));
assertTrue(UrlUtils.isMatchGlobPattern("v*e", "value"));
assertTrue(UrlUtils.isMatchGlobPattern("$key", "value", URL.valueOf("dubbo://localhost:8080/Foo?key=v*e")));
}
}