Skip to content

Commit

Permalink
Merge pull request #1659 from Diffblue-benchmarks/add-GroupKey-tests
Browse files Browse the repository at this point in the history
Add unit tests for com.alibaba.nacos.config.server.utils.GroupKey
  • Loading branch information
nkorange authored Sep 19, 2019
2 parents caba884 + 934c423 commit 4b1a65c
Showing 1 changed file with 122 additions and 0 deletions.
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
}
}

0 comments on commit 4b1a65c

Please sign in to comment.