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

Add unit tests for com.alibaba.nacos.config.server.utils.GroupKey #1659

Merged
merged 1 commit into from
Sep 19, 2019
Merged
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
Expand Up @@ -16,7 +16,9 @@
package com.alibaba.nacos.config.server.utils;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand All @@ -25,6 +27,8 @@
@WebAppConfiguration
public class GroupKeyTest {

@Rule public final ExpectedException thrown = ExpectedException.none();

@Test
public void test_parseGroupKey_非法的() {
String key = "11111+222+333333+444";
Expand Down Expand Up @@ -64,4 +68,122 @@ public class GroupKeyTest {
Assert.assertEquals("11111%", pair[0]);
Assert.assertEquals("222", pair[1]);
}

@Test
public void getKey_ThreeParams() {

// Act
final String actual = GroupKey.getKey(",", ",", "3");

// Assert result
Assert.assertEquals(",+,+3", actual);
}

@Test
public void getKey_TwoParams() {

// Act
final String actual = GroupKey.getKey("3", "\'");

// Assert result
Assert.assertEquals("3+\'", actual);
}

@Test
public void getKeyTenant_Plus_ThreeParams() {

// Act
final String actual = GroupKey.getKeyTenant("3", "1", ",");

// Assert result
Assert.assertEquals("3+1+,", actual);
}

@Test
public void getKeyTenant_Percent_ThreeParams() {

// Act
final String actual = GroupKey.getKeyTenant("\u0000\u0000", "%+", null);

// Assert result
Assert.assertEquals("\u0000\u0000+%25%2B", actual);
}

@Test
public void parseKey_SingleCharacter() {

// Act
final String[] actual = GroupKey.parseKey("/");

// Assert result
Assert.assertArrayEquals(new String[] {null, "/", null}, actual);
}

@Test
public void parseKey_Plus_IllegalArgumentException() {

// Act
thrown.expect(IllegalArgumentException.class);
GroupKey.parseKey("+");

// Method is not expected to return due to exception thrown
}

@Test
public void parseKey_Percent_IllegalArgumentException() {

// Act
thrown.expect(IllegalArgumentException.class);
GroupKey.parseKey("%%%5\u0000??????????????");

// Method is not expected to return due to exception thrown
}

@Test
public void parseKey_Invalid_StringIndexOutOfBoundsException() {

// Act
thrown.expect(StringIndexOutOfBoundsException.class);
GroupKey.parseKey("++%");

// Method is not expected to return due to exception thrown
}

@Test
public void urlEncode_Plus() {

// Arrange
final StringBuilder sb = new StringBuilder("????");

// Act
GroupKey.urlEncode("+", sb);

// Assert side effects
Assert.assertNotNull(sb);
Assert.assertEquals("????%2B", sb.toString());
}

@Test
public void urlEncode_Percent() {

// Arrange
final StringBuilder sb = new StringBuilder("??????");

// Act
GroupKey.urlEncode("%", sb);

// Assert side effects
Assert.assertNotNull(sb);
Assert.assertEquals("??????%25", sb.toString());
}

@Test
public void urlEncode_NullStringBuilder() {

// Act
thrown.expect(NullPointerException.class);
GroupKey.urlEncode("+", null);

// Method is not expected to return due to exception thrown
}
}