mirror of
https://github.com/google/nomulus.git
synced 2025-07-10 05:03:24 +02:00
Get rid of all remaining JUnit 4 usages except in prober & proxy (#731)
* Get rid of all remaining JUnit 4 usages except in prober & proxy subprojects Caveat: Test suites aren't yet implemented in JUnit 5 so we still use the ones from JUnit 5 in the core subproject. * Fix some build errors
This commit is contained in:
parent
a02b67caf5
commit
16a31e460c
347 changed files with 3785 additions and 1536 deletions
|
@ -37,13 +37,12 @@ dependencies {
|
|||
testCompile deps['com.google.appengine:appengine-api-stubs']
|
||||
testCompile deps['com.google.guava:guava-testlib']
|
||||
testCompile deps['com.google.truth:truth']
|
||||
testCompile deps['junit:junit']
|
||||
testCompile deps['org.junit.jupiter:junit-jupiter-api']
|
||||
testCompile deps['org.junit.jupiter:junit-jupiter-engine']
|
||||
testCompile deps['org.junit.vintage:junit-vintage-engine']
|
||||
testCompile deps['org.hamcrest:hamcrest-all']
|
||||
testCompile deps['org.hamcrest:hamcrest-core']
|
||||
testCompile deps['org.mockito:mockito-core']
|
||||
testCompile deps['org.mockito:mockito-junit-jupiter']
|
||||
testCompile files("${rootDir}/third_party/objectify/v4_1/objectify-4.1.3.jar")
|
||||
testCompile project(path: ':common', configuration: 'testing')
|
||||
testRuntime deps['com.google.flogger:flogger-system-backend']
|
||||
|
|
|
@ -50,9 +50,9 @@ org.junit.jupiter:junit-jupiter-api:5.6.2
|
|||
org.junit.jupiter:junit-jupiter-engine:5.6.2
|
||||
org.junit.platform:junit-platform-commons:1.6.2
|
||||
org.junit.platform:junit-platform-engine:1.6.2
|
||||
org.junit.vintage:junit-vintage-engine:5.6.2
|
||||
org.junit:junit-bom:5.6.2
|
||||
org.mockito:mockito-core:3.3.3
|
||||
org.mockito:mockito-junit-jupiter:3.3.3
|
||||
org.objenesis:objenesis:2.6
|
||||
org.opentest4j:opentest4j:1.2.0
|
||||
org.yaml:snakeyaml:1.17
|
||||
|
|
|
@ -50,9 +50,9 @@ org.junit.jupiter:junit-jupiter-api:5.6.2
|
|||
org.junit.jupiter:junit-jupiter-engine:5.6.2
|
||||
org.junit.platform:junit-platform-commons:1.6.2
|
||||
org.junit.platform:junit-platform-engine:1.6.2
|
||||
org.junit.vintage:junit-vintage-engine:5.6.2
|
||||
org.junit:junit-bom:5.6.2
|
||||
org.mockito:mockito-core:3.3.3
|
||||
org.mockito:mockito-junit-jupiter:3.3.3
|
||||
org.objenesis:objenesis:2.6
|
||||
org.opentest4j:opentest4j:1.2.0
|
||||
org.yaml:snakeyaml:1.17
|
||||
|
|
|
@ -52,9 +52,9 @@ org.junit.jupiter:junit-jupiter-api:5.6.2
|
|||
org.junit.jupiter:junit-jupiter-engine:5.6.2
|
||||
org.junit.platform:junit-platform-commons:1.6.2
|
||||
org.junit.platform:junit-platform-engine:1.6.2
|
||||
org.junit.vintage:junit-vintage-engine:5.6.2
|
||||
org.junit:junit-bom:5.6.2
|
||||
org.mockito:mockito-core:3.3.3
|
||||
org.mockito:mockito-junit-jupiter:3.3.3
|
||||
org.objenesis:objenesis:2.6
|
||||
org.opentest4j:opentest4j:1.2.0
|
||||
org.yaml:snakeyaml:1.17
|
||||
|
|
|
@ -52,9 +52,9 @@ org.junit.jupiter:junit-jupiter-api:5.6.2
|
|||
org.junit.jupiter:junit-jupiter-engine:5.6.2
|
||||
org.junit.platform:junit-platform-commons:1.6.2
|
||||
org.junit.platform:junit-platform-engine:1.6.2
|
||||
org.junit.vintage:junit-vintage-engine:5.6.2
|
||||
org.junit:junit-bom:5.6.2
|
||||
org.mockito:mockito-core:3.3.3
|
||||
org.mockito:mockito-junit-jupiter:3.3.3
|
||||
org.objenesis:objenesis:2.6
|
||||
org.opentest4j:opentest4j:1.2.0
|
||||
org.yaml:snakeyaml:1.17
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
package google.registry.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
|
@ -24,54 +24,51 @@ import static org.mockito.Mockito.verify;
|
|||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.modules.ModulesService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
/** Unit tests for {@link AppEngineServiceUtilsImpl}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class AppEngineServiceUtilsImplTest {
|
||||
|
||||
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AppEngineServiceUtilsImplTest {
|
||||
|
||||
@Mock private ModulesService modulesService;
|
||||
|
||||
private AppEngineServiceUtils appEngineServiceUtils;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
appEngineServiceUtils = new AppEngineServiceUtilsImpl(modulesService);
|
||||
when(modulesService.getVersionHostname(anyString(), isNull()))
|
||||
.thenReturn("1234.servicename.projectid.appspot.fake");
|
||||
when(modulesService.getVersionHostname(anyString(), eq("2345")))
|
||||
.thenReturn("2345.servicename.projectid.appspot.fake");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getServiceHostname_doesntIncludeVersionId() {
|
||||
void test_getServiceHostname_doesntIncludeVersionId() {
|
||||
when(modulesService.getVersionHostname(anyString(), isNull()))
|
||||
.thenReturn("1234.servicename.projectid.appspot.fake");
|
||||
assertThat(appEngineServiceUtils.getServiceHostname("servicename"))
|
||||
.isEqualTo("servicename.projectid.appspot.fake");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getVersionHostname_doesIncludeVersionId() {
|
||||
void test_getVersionHostname_doesIncludeVersionId() {
|
||||
when(modulesService.getVersionHostname(anyString(), isNull()))
|
||||
.thenReturn("1234.servicename.projectid.appspot.fake");
|
||||
assertThat(appEngineServiceUtils.getCurrentVersionHostname("servicename"))
|
||||
.isEqualTo("1234.servicename.projectid.appspot.fake");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getVersionHostname_worksWithVersionId() {
|
||||
void test_getVersionHostname_worksWithVersionId() {
|
||||
when(modulesService.getVersionHostname(anyString(), eq("2345")))
|
||||
.thenReturn("2345.servicename.projectid.appspot.fake");
|
||||
assertThat(appEngineServiceUtils.getVersionHostname("servicename", "2345"))
|
||||
.isEqualTo("2345.servicename.projectid.appspot.fake");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getVersionHostname_throwsWhenVersionIdIsNull() {
|
||||
void test_getVersionHostname_throwsWhenVersionIdIsNull() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -80,13 +77,13 @@ public class AppEngineServiceUtilsImplTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_setNumInstances_worksWithValidParameters() {
|
||||
void test_setNumInstances_worksWithValidParameters() {
|
||||
appEngineServiceUtils.setNumInstances("service", "version", 10L);
|
||||
verify(modulesService, times(1)).setNumInstances("service", "version", 10L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_setNumInstances_throwsWhenServiceIsNull() {
|
||||
void test_setNumInstances_throwsWhenServiceIsNull() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -95,7 +92,7 @@ public class AppEngineServiceUtilsImplTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_setNumInstances_throwsWhenVersionIsNull() {
|
||||
void test_setNumInstances_throwsWhenVersionIsNull() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -104,7 +101,7 @@ public class AppEngineServiceUtilsImplTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_setNumInstances_throwsWhenNumInstancesIsInvalid() {
|
||||
void test_setNumInstances_throwsWhenNumInstancesIsInvalid() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -113,32 +110,32 @@ public class AppEngineServiceUtilsImplTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_convertToSingleSubdomain_doesNothingWithoutServiceOrHostname() {
|
||||
void test_convertToSingleSubdomain_doesNothingWithoutServiceOrHostname() {
|
||||
assertThat(appEngineServiceUtils.convertToSingleSubdomain("projectid.appspot.com"))
|
||||
.isEqualTo("projectid.appspot.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_convertToSingleSubdomain_doesNothingWhenItCannotParseCorrectly() {
|
||||
void test_convertToSingleSubdomain_doesNothingWhenItCannotParseCorrectly() {
|
||||
assertThat(appEngineServiceUtils.convertToSingleSubdomain("garbage.notrealhost.example"))
|
||||
.isEqualTo("garbage.notrealhost.example");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_convertToSingleSubdomain_convertsWithServiceName() {
|
||||
void test_convertToSingleSubdomain_convertsWithServiceName() {
|
||||
assertThat(appEngineServiceUtils.convertToSingleSubdomain("service.projectid.appspot.com"))
|
||||
.isEqualTo("service-dot-projectid.appspot.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_convertToSingleSubdomain_convertsWithVersionAndServiceName() {
|
||||
void test_convertToSingleSubdomain_convertsWithVersionAndServiceName() {
|
||||
assertThat(
|
||||
appEngineServiceUtils.convertToSingleSubdomain("version.service.projectid.appspot.com"))
|
||||
.isEqualTo("version-dot-service-dot-projectid.appspot.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_convertToSingleSubdomain_convertsWithInstanceAndVersionAndServiceName() {
|
||||
void test_convertToSingleSubdomain_convertsWithInstanceAndVersionAndServiceName() {
|
||||
assertThat(
|
||||
appEngineServiceUtils.convertToSingleSubdomain(
|
||||
"instanceid.version.service.projectid.appspot.com"))
|
||||
|
|
|
@ -14,8 +14,11 @@
|
|||
|
||||
package google.registry.util;
|
||||
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.net.InetAddresses;
|
||||
|
@ -25,40 +28,42 @@ import java.net.InetAddress;
|
|||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests for {@link CidrAddressBlock}.
|
||||
*
|
||||
*/
|
||||
public class CidrAddressBlockTest extends TestCase {
|
||||
/** Tests for {@link CidrAddressBlock}. */
|
||||
class CidrAddressBlockTest {
|
||||
|
||||
public void testNulls() {
|
||||
@Test
|
||||
void testNulls() {
|
||||
NullPointerTester tester = new NullPointerTester();
|
||||
tester.testAllPublicStaticMethods(CidrAddressBlock.class);
|
||||
tester.testAllPublicConstructors(CidrAddressBlock.class);
|
||||
tester.testAllPublicInstanceMethods(new CidrAddressBlock("::/0"));
|
||||
}
|
||||
|
||||
public void testConstructorWithNetmask() {
|
||||
@Test
|
||||
void testConstructorWithNetmask() {
|
||||
CidrAddressBlock b0 = new CidrAddressBlock("22.24.66.0/24");
|
||||
assertEquals("22.24.66.0", b0.getIp());
|
||||
assertEquals(24, b0.getNetmask());
|
||||
}
|
||||
|
||||
public void testConstructorPicksNetmask() {
|
||||
@Test
|
||||
void testConstructorPicksNetmask() {
|
||||
CidrAddressBlock b0 = new CidrAddressBlock("64.132.1.2");
|
||||
assertEquals(32, b0.getNetmask());
|
||||
}
|
||||
|
||||
public void testConstructorDoesntThrow() {
|
||||
@Test
|
||||
void testConstructorDoesntThrow() {
|
||||
new CidrAddressBlock("64.132.0.0/16");
|
||||
new CidrAddressBlock("128.142.217.0/24");
|
||||
new CidrAddressBlock("35.213.0.0", 16);
|
||||
new CidrAddressBlock("89.23.164.0", 24);
|
||||
}
|
||||
|
||||
public void testInetAddressConstructor() {
|
||||
@Test
|
||||
void testInetAddressConstructor() {
|
||||
CidrAddressBlock b0 = new CidrAddressBlock(InetAddresses.forString("1.2.3.4"));
|
||||
assertEquals(32, b0.getNetmask());
|
||||
assertEquals("1.2.3.4", b0.getIp());
|
||||
|
@ -73,7 +78,8 @@ public class CidrAddressBlockTest extends TestCase {
|
|||
assertEquals("5ffe:0:0:0:0:0:0:1", b1.getIp());
|
||||
}
|
||||
|
||||
public void testCornerCasesSucceed() {
|
||||
@Test
|
||||
void testCornerCasesSucceed() {
|
||||
new CidrAddressBlock("0.0.0.0/32");
|
||||
new CidrAddressBlock("255.255.255.255/32");
|
||||
new CidrAddressBlock("255.255.255.254/31");
|
||||
|
@ -88,7 +94,8 @@ public class CidrAddressBlockTest extends TestCase {
|
|||
new CidrAddressBlock("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe/127");
|
||||
}
|
||||
|
||||
public void testFailure() {
|
||||
@Test
|
||||
void testFailure() {
|
||||
assertConstructionFails("");
|
||||
assertConstructionFails("0");
|
||||
assertConstructionFails("1");
|
||||
|
@ -120,31 +127,31 @@ public class CidrAddressBlockTest extends TestCase {
|
|||
assertConstructionFails("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/127");
|
||||
}
|
||||
|
||||
public void testTruncation() {
|
||||
ImmutableMap<String, String> netblocks = new ImmutableMap.Builder<String, String>()
|
||||
// IPv4
|
||||
.put("1.2.3.4/0", "0.0.0.0/0")
|
||||
.put("1.2.3.4/24", "1.2.3.0/24")
|
||||
.put("1.2.3.255/27", "1.2.3.224/27")
|
||||
.put("1.2.3.255/28", "1.2.3.240/28")
|
||||
// IPv6
|
||||
.put("2001:db8::1/0", "::/0")
|
||||
.put("2001:db8::1/16", "2001::/16")
|
||||
.put("2001:db8::1/21", "2001:800::/21")
|
||||
.put("2001:db8::1/22", "2001:c00::/22")
|
||||
.build();
|
||||
@Test
|
||||
void testTruncation() {
|
||||
ImmutableMap<String, String> netblocks =
|
||||
new ImmutableMap.Builder<String, String>()
|
||||
// IPv4
|
||||
.put("1.2.3.4/0", "0.0.0.0/0")
|
||||
.put("1.2.3.4/24", "1.2.3.0/24")
|
||||
.put("1.2.3.255/27", "1.2.3.224/27")
|
||||
.put("1.2.3.255/28", "1.2.3.240/28")
|
||||
// IPv6
|
||||
.put("2001:db8::1/0", "::/0")
|
||||
.put("2001:db8::1/16", "2001::/16")
|
||||
.put("2001:db8::1/21", "2001:800::/21")
|
||||
.put("2001:db8::1/22", "2001:c00::/22")
|
||||
.build();
|
||||
for (Map.Entry<String, String> pair : netblocks.entrySet()) {
|
||||
assertConstructionFails(pair.getKey());
|
||||
assertEquals(new CidrAddressBlock(pair.getValue()), CidrAddressBlock.create(pair.getKey()));
|
||||
assertEquals(
|
||||
new CidrAddressBlock(pair.getValue()),
|
||||
CidrAddressBlock.create(pair.getKey()));
|
||||
assertEquals(
|
||||
CidrAddressBlock.create(pair.getKey()),
|
||||
CidrAddressBlock.create(pair.getValue()));
|
||||
CidrAddressBlock.create(pair.getKey()), CidrAddressBlock.create(pair.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
public void testContains() {
|
||||
@Test
|
||||
void testContains() {
|
||||
CidrAddressBlock b0 = CidrAddressBlock.create("172.24.255.0/24");
|
||||
assertTrue(b0.contains(b0));
|
||||
assertTrue(b0.contains(b0.getIp()));
|
||||
|
@ -226,19 +233,21 @@ public class CidrAddressBlockTest extends TestCase {
|
|||
assertFalse(allIPv6.contains(allIPv4));
|
||||
}
|
||||
|
||||
public void testGetAllOnesAddress() {
|
||||
@Test
|
||||
void testGetAllOnesAddress() {
|
||||
// <CIDR block> -> <expected getAllOnesAddress()>
|
||||
ImmutableMap<String, String> testCases = new ImmutableMap.Builder<String, String>()
|
||||
.put("172.24.255.0/24", "172.24.255.255")
|
||||
.put("172.24.0.0/15", "172.25.255.255")
|
||||
.put("172.24.254.0/23", "172.24.255.255")
|
||||
.put("172.24.255.0/32", "172.24.255.0")
|
||||
.put("0.0.0.0/0", "255.255.255.255")
|
||||
.put("2001:db8::/48", "2001:db8::ffff:ffff:ffff:ffff:ffff")
|
||||
.put("2001:db8::/32", "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff")
|
||||
.put("2001:db8::/128", "2001:db8::")
|
||||
.put("::/0", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
|
||||
.build();
|
||||
ImmutableMap<String, String> testCases =
|
||||
new ImmutableMap.Builder<String, String>()
|
||||
.put("172.24.255.0/24", "172.24.255.255")
|
||||
.put("172.24.0.0/15", "172.25.255.255")
|
||||
.put("172.24.254.0/23", "172.24.255.255")
|
||||
.put("172.24.255.0/32", "172.24.255.0")
|
||||
.put("0.0.0.0/0", "255.255.255.255")
|
||||
.put("2001:db8::/48", "2001:db8::ffff:ffff:ffff:ffff:ffff")
|
||||
.put("2001:db8::/32", "2001:db8:ffff:ffff:ffff:ffff:ffff:ffff")
|
||||
.put("2001:db8::/128", "2001:db8::")
|
||||
.put("::/0", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
|
||||
.build();
|
||||
|
||||
for (Map.Entry<String, String> testCase : testCases.entrySet()) {
|
||||
assertEquals(
|
||||
|
@ -247,7 +256,8 @@ public class CidrAddressBlockTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testEqualsAndHashCode() {
|
||||
@Test
|
||||
void testEqualsAndHashCode() {
|
||||
CidrAddressBlock b0 = new CidrAddressBlock("172.24.66.0/24");
|
||||
CidrAddressBlock b1 = new CidrAddressBlock("172.24.66.0", 24);
|
||||
CidrAddressBlock b2 = new CidrAddressBlock("172.24.0.0/16");
|
||||
|
@ -269,7 +279,8 @@ public class CidrAddressBlockTest extends TestCase {
|
|||
assertNotEquals(b0, b3);
|
||||
}
|
||||
|
||||
public void testIterate() {
|
||||
@Test
|
||||
void testIterate() {
|
||||
CidrAddressBlock b0 = new CidrAddressBlock("172.24.66.0/24");
|
||||
int count = 0;
|
||||
for (InetAddress addr : b0) {
|
||||
|
@ -293,7 +304,8 @@ public class CidrAddressBlockTest extends TestCase {
|
|||
assertThrows(NoSuchElementException.class, i::next);
|
||||
}
|
||||
|
||||
public void testSerializability() {
|
||||
@Test
|
||||
void testSerializability() {
|
||||
SerializableTester.reserializeAndAssert(new CidrAddressBlock("22.24.66.0/24"));
|
||||
SerializableTester.reserializeAndAssert(new CidrAddressBlock("64.132.1.2"));
|
||||
SerializableTester.reserializeAndAssert(
|
||||
|
|
|
@ -17,28 +17,25 @@ package google.registry.util;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.util.CollectionUtils.nullToEmpty;
|
||||
import static google.registry.util.CollectionUtils.partitionMap;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link CollectionUtils} */
|
||||
@RunWith(JUnit4.class)
|
||||
public class CollectionUtilsTest {
|
||||
class CollectionUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testNullToEmptyMap_leavesNonNullAlone() {
|
||||
void testNullToEmptyMap_leavesNonNullAlone() {
|
||||
Map<String, Integer> map = ImmutableMap.of("hello", 1);
|
||||
assertThat(nullToEmpty(map)).isEqualTo(map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullToEmptyMap_convertsNullToEmptyMap() {
|
||||
void testNullToEmptyMap_convertsNullToEmptyMap() {
|
||||
Map<String, Integer> map = null;
|
||||
Map<String, Integer> convertedMap = nullToEmpty(map);
|
||||
assertThat(map).isNull();
|
||||
|
@ -47,7 +44,7 @@ public class CollectionUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testPartitionMap() {
|
||||
void testPartitionMap() {
|
||||
Map<String, String> map = ImmutableMap.of("ka", "va", "kb", "vb", "kc", "vc");
|
||||
assertThat(partitionMap(map, 2)).containsExactlyElementsIn(ImmutableList.of(
|
||||
ImmutableMap.of("ka", "va", "kb", "vb"),
|
||||
|
@ -55,22 +52,22 @@ public class CollectionUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testPartitionMap_emptyInput() {
|
||||
void testPartitionMap_emptyInput() {
|
||||
assertThat(partitionMap(ImmutableMap.of(), 100)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartitionMap_negativePartitionSize() {
|
||||
void testPartitionMap_negativePartitionSize() {
|
||||
assertThrows(IllegalArgumentException.class, () -> partitionMap(ImmutableMap.of("A", "b"), -2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPartitionMap_nullMap() {
|
||||
void testPartitionMap_nullMap() {
|
||||
assertThrows(NullPointerException.class, () -> partitionMap(null, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeadCodeWeDontWantToDelete() {
|
||||
void testDeadCodeWeDontWantToDelete() {
|
||||
CollectionUtils.nullToEmpty(HashMultimap.create());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,23 +15,20 @@
|
|||
package google.registry.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import javax.annotation.Nullable;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link ComparingInvocationHandler}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class ComparingInvocationHandlerTest {
|
||||
class ComparingInvocationHandlerTest {
|
||||
|
||||
static class Dummy {}
|
||||
private static class Dummy {}
|
||||
|
||||
interface MyInterface {
|
||||
String func(int a, String b);
|
||||
|
@ -51,7 +48,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
}
|
||||
|
||||
static final ArrayList<String> log = new ArrayList<>();
|
||||
private static final ArrayList<String> log = new ArrayList<>();
|
||||
|
||||
static final class MyInterfaceComparingInvocationHandler
|
||||
extends ComparingInvocationHandler<MyInterface> {
|
||||
|
@ -112,14 +109,14 @@ public class ComparingInvocationHandlerTest {
|
|||
private final MyInterface mySecondMock = mock(MyInterface.class);
|
||||
private MyInterfaceComparingInvocationHandler invocationHandler;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
log.clear();
|
||||
invocationHandler = new MyInterfaceComparingInvocationHandler(myActualMock, mySecondMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_actualThrows_logDifference() {
|
||||
void test_actualThrows_logDifference() {
|
||||
MyInterface comparator = invocationHandler.makeProxy();
|
||||
MyException myException = new MyException("message");
|
||||
when(myActualMock.func(3, "str")).thenThrow(myException);
|
||||
|
@ -134,7 +131,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_secondThrows_logDifference() {
|
||||
void test_secondThrows_logDifference() {
|
||||
MyInterface comparator = invocationHandler.makeProxy();
|
||||
MyOtherException myOtherException = new MyOtherException("message");
|
||||
when(myActualMock.func(3, "str")).thenReturn(ACTUAL_RESULT);
|
||||
|
@ -150,7 +147,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_bothThrowEqual_noLog() {
|
||||
void test_bothThrowEqual_noLog() {
|
||||
MyInterface comparator = invocationHandler.setExeptionsEquals(true).makeProxy();
|
||||
MyException myException = new MyException("actual message");
|
||||
MyOtherException myOtherException = new MyOtherException("second message");
|
||||
|
@ -162,7 +159,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_bothThrowDifferent_logDifference() {
|
||||
void test_bothThrowDifferent_logDifference() {
|
||||
MyInterface comparator = invocationHandler.setExeptionsEquals(false).makeProxy();
|
||||
MyException myException = new MyException("actual message");
|
||||
MyOtherException myOtherException = new MyOtherException("second message");
|
||||
|
@ -179,7 +176,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_bothReturnSame_noLog() {
|
||||
void test_bothReturnSame_noLog() {
|
||||
MyInterface comparator = invocationHandler.makeProxy();
|
||||
when(myActualMock.func(3, "str")).thenReturn(ACTUAL_RESULT);
|
||||
when(mySecondMock.func(3, "str")).thenReturn(ACTUAL_RESULT);
|
||||
|
@ -190,7 +187,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_bothReturnDifferent_logDifference() {
|
||||
void test_bothReturnDifferent_logDifference() {
|
||||
MyInterface comparator = invocationHandler.makeProxy();
|
||||
when(myActualMock.func(3, "str")).thenReturn(ACTUAL_RESULT);
|
||||
when(mySecondMock.func(3, "str")).thenReturn(SECOND_RESULT);
|
||||
|
@ -202,7 +199,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_usesOverriddenMethods_noDifference() {
|
||||
void test_usesOverriddenMethods_noDifference() {
|
||||
MyInterface comparator = invocationHandler.setDummyEquals(true).makeProxy();
|
||||
when(myActualMock.func()).thenReturn(new Dummy());
|
||||
when(mySecondMock.func()).thenReturn(new Dummy());
|
||||
|
@ -213,7 +210,7 @@ public class ComparingInvocationHandlerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_usesOverriddenMethods_logDifference() {
|
||||
void test_usesOverriddenMethods_logDifference() {
|
||||
MyInterface comparator = invocationHandler.setDummyEquals(false).makeProxy();
|
||||
when(myActualMock.func()).thenReturn(new Dummy());
|
||||
when(mySecondMock.func()).thenReturn(new Dummy());
|
||||
|
|
|
@ -25,57 +25,54 @@ import static google.registry.util.DateTimeUtils.leapSafeAddYears;
|
|||
import static google.registry.util.DateTimeUtils.leapSafeSubtractYears;
|
||||
import static google.registry.util.DateTimeUtils.toJodaDateTime;
|
||||
import static google.registry.util.DateTimeUtils.toZonedDateTime;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.time.ZonedDateTime;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link DateTimeUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class DateTimeUtilsTest {
|
||||
class DateTimeUtilsTest {
|
||||
|
||||
ImmutableList<DateTime> sampleDates = ImmutableList.of(
|
||||
START_OF_TIME, START_OF_TIME.plusDays(1), END_OF_TIME, END_OF_TIME);
|
||||
private ImmutableList<DateTime> sampleDates =
|
||||
ImmutableList.of(START_OF_TIME, START_OF_TIME.plusDays(1), END_OF_TIME, END_OF_TIME);
|
||||
|
||||
@Test
|
||||
public void testSuccess_earliestOf() {
|
||||
void testSuccess_earliestOf() {
|
||||
assertThat(earliestOf(START_OF_TIME, END_OF_TIME)).isEqualTo(START_OF_TIME);
|
||||
assertThat(earliestOf(sampleDates)).isEqualTo(START_OF_TIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_latestOf() {
|
||||
void testSuccess_latestOf() {
|
||||
assertThat(latestOf(START_OF_TIME, END_OF_TIME)).isEqualTo(END_OF_TIME);
|
||||
assertThat(latestOf(sampleDates)).isEqualTo(END_OF_TIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_isBeforeOrAt() {
|
||||
void testSuccess_isBeforeOrAt() {
|
||||
assertThat(isBeforeOrAt(START_OF_TIME, START_OF_TIME.plusDays(1))).isTrue();
|
||||
assertThat(isBeforeOrAt(START_OF_TIME, START_OF_TIME)).isTrue();
|
||||
assertThat(isBeforeOrAt(START_OF_TIME.plusDays(1), START_OF_TIME)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_isAtOrAfter() {
|
||||
void testSuccess_isAtOrAfter() {
|
||||
assertThat(isAtOrAfter(START_OF_TIME, START_OF_TIME.plusDays(1))).isFalse();
|
||||
assertThat(isAtOrAfter(START_OF_TIME, START_OF_TIME)).isTrue();
|
||||
assertThat(isAtOrAfter(START_OF_TIME.plusDays(1), START_OF_TIME)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_leapSafeAddYears() {
|
||||
void testSuccess_leapSafeAddYears() {
|
||||
DateTime startDate = DateTime.parse("2012-02-29T00:00:00Z");
|
||||
assertThat(startDate.plusYears(4)).isEqualTo(DateTime.parse("2016-02-29T00:00:00Z"));
|
||||
assertThat(leapSafeAddYears(startDate, 4)).isEqualTo(DateTime.parse("2016-02-28T00:00:00Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_leapSafeSubtractYears() {
|
||||
void testSuccess_leapSafeSubtractYears() {
|
||||
DateTime startDate = DateTime.parse("2012-02-29T00:00:00Z");
|
||||
assertThat(startDate.minusYears(4)).isEqualTo(DateTime.parse("2008-02-29T00:00:00Z"));
|
||||
assertThat(leapSafeSubtractYears(startDate, 4))
|
||||
|
@ -83,58 +80,58 @@ public class DateTimeUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_leapSafeSubtractYears_zeroYears() {
|
||||
void testSuccess_leapSafeSubtractYears_zeroYears() {
|
||||
DateTime leapDay = DateTime.parse("2012-02-29T00:00:00Z");
|
||||
assertThat(leapDay.minusYears(0)).isEqualTo(leapDay);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_earliestOfEmpty() {
|
||||
void testFailure_earliestOfEmpty() {
|
||||
assertThrows(IllegalArgumentException.class, () -> earliestOf(ImmutableList.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_latestOfEmpty() {
|
||||
void testFailure_latestOfEmpty() {
|
||||
assertThrows(IllegalArgumentException.class, () -> earliestOf(ImmutableList.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_toZonedDateTime_preservesTimeZone() {
|
||||
void testSuccess_toZonedDateTime_preservesTimeZone() {
|
||||
DateTime dateTime = DateTime.parse("2019-09-06T10:59:36.283-07:00"); // PDT
|
||||
ZonedDateTime zonedDateTime = toZonedDateTime(dateTime);
|
||||
assertThat(zonedDateTime.toString()).isEqualTo("2019-09-06T10:59:36.283-07:00"); // still PDT
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_toZonedDateTime_fromStringZulu() {
|
||||
void testSuccess_toZonedDateTime_fromStringZulu() {
|
||||
DateTime dateTime = DateTime.parse("2015-10-13T11:22:33.168Z");
|
||||
ZonedDateTime zonedDateTime = toZonedDateTime(dateTime);
|
||||
assertThat(zonedDateTime.toString()).isEqualTo("2015-10-13T11:22:33.168Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_toZonedDateTime_leapYear() {
|
||||
void testSuccess_toZonedDateTime_leapYear() {
|
||||
DateTime dateTime = DateTime.parse("2016-02-29T11:22:33.168Z");
|
||||
ZonedDateTime zonedDateTime = toZonedDateTime(dateTime);
|
||||
assertThat(zonedDateTime.toString()).isEqualTo("2016-02-29T11:22:33.168Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_toJodaDateTime_preservesTimeZone() {
|
||||
void testSuccess_toJodaDateTime_preservesTimeZone() {
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-09-06T10:59:36.283-07:00"); // PDT
|
||||
DateTime dateTime = toJodaDateTime(zonedDateTime);
|
||||
assertThat(dateTime.toString()).isEqualTo("2019-09-06T10:59:36.283-07:00"); // still PDT
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_toJodaDateTime_fromStringZulu() {
|
||||
void testSuccess_toJodaDateTime_fromStringZulu() {
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2015-10-13T11:22:33.168Z");
|
||||
DateTime dateTime = toJodaDateTime(zonedDateTime);
|
||||
assertThat(dateTime.toString()).isEqualTo("2015-10-13T11:22:33.168Z");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_toJodaDateTime_leapYear() {
|
||||
void testSuccess_toJodaDateTime_leapYear() {
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2016-02-29T11:22:33.168Z");
|
||||
DateTime dateTime = toJodaDateTime(zonedDateTime);
|
||||
assertThat(dateTime.toString()).isEqualTo("2016-02-29T11:22:33.168Z");
|
||||
|
|
|
@ -21,47 +21,43 @@ import static google.registry.util.DiffUtils.prettyPrintSetDiff;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link DiffUtils} */
|
||||
@RunWith(JUnit4.class)
|
||||
public class DiffUtilsTest {
|
||||
class DiffUtilsTest {
|
||||
|
||||
@Test
|
||||
public void test_prettyPrintSetDiff_emptySets() {
|
||||
void test_prettyPrintSetDiff_emptySets() {
|
||||
assertThat(prettyPrintSetDiff(ImmutableSet.of(), ImmutableSet.of()))
|
||||
.isEqualTo("NO DIFFERENCES");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_prettyPrintSetDiff_noDifferences() {
|
||||
void test_prettyPrintSetDiff_noDifferences() {
|
||||
assertThat(prettyPrintSetDiff(ImmutableSet.of("c", "x", "m"), ImmutableSet.of("m", "x", "c")))
|
||||
.isEqualTo("NO DIFFERENCES");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_prettyPrintSetDiff_addedElements() {
|
||||
void test_prettyPrintSetDiff_addedElements() {
|
||||
assertThat(prettyPrintSetDiff(ImmutableSet.of("z"), ImmutableSet.of("a", "b", "z")))
|
||||
.isEqualTo("\n ADDED: [a, b]\n FINAL CONTENTS: [a, b, z]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_prettyPrintSetDiff_removedElements() {
|
||||
void test_prettyPrintSetDiff_removedElements() {
|
||||
assertThat(prettyPrintSetDiff(ImmutableSet.of("x", "y", "z"), ImmutableSet.of("y")))
|
||||
.isEqualTo("\n REMOVED: [x, z]\n FINAL CONTENTS: [y]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_prettyPrintSetDiff_addedAndRemovedElements() {
|
||||
assertThat(prettyPrintSetDiff(
|
||||
ImmutableSet.of("a", "b", "c"), ImmutableSet.of("a", "y", "z")))
|
||||
void test_prettyPrintSetDiff_addedAndRemovedElements() {
|
||||
assertThat(prettyPrintSetDiff(ImmutableSet.of("a", "b", "c"), ImmutableSet.of("a", "y", "z")))
|
||||
.isEqualTo("\n ADDED: [y, z]\n REMOVED: [b, c]\n FINAL CONTENTS: [a, y, z]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_emptyToNullCollection_doesntDisplay() {
|
||||
void test_emptyToNullCollection_doesntDisplay() {
|
||||
Map<String, Object> mapA = new HashMap<>();
|
||||
mapA.put("a", "jim");
|
||||
mapA.put("b", null);
|
||||
|
@ -73,27 +69,27 @@ public class DiffUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_prettyPrintSetDiff_addedAndRemovedElements_objects() {
|
||||
void test_prettyPrintSetDiff_addedAndRemovedElements_objects() {
|
||||
DummyObject a = DummyObject.create("a");
|
||||
DummyObject b = DummyObject.create("b");
|
||||
DummyObject c = DummyObject.create("c");
|
||||
|
||||
assertThat(prettyPrintSetDiff(
|
||||
ImmutableSet.of(a, b), ImmutableSet.of(a, c)))
|
||||
.isEqualTo("\n"
|
||||
+ " ADDED:\n"
|
||||
+ " {c}\n"
|
||||
+ " REMOVED:\n"
|
||||
+ " {b}\n"
|
||||
+ " FINAL CONTENTS:\n"
|
||||
+ " {a},\n"
|
||||
+ " {c}");
|
||||
assertThat(prettyPrintSetDiff(ImmutableSet.of(a, b), ImmutableSet.of(a, c)))
|
||||
.isEqualTo(
|
||||
"\n"
|
||||
+ " ADDED:\n"
|
||||
+ " {c}\n"
|
||||
+ " REMOVED:\n"
|
||||
+ " {b}\n"
|
||||
+ " FINAL CONTENTS:\n"
|
||||
+ " {a},\n"
|
||||
+ " {c}");
|
||||
}
|
||||
|
||||
private static class DummyObject {
|
||||
public String id;
|
||||
String id;
|
||||
|
||||
public static DummyObject create(String id) {
|
||||
static DummyObject create(String id) {
|
||||
DummyObject instance = new DummyObject();
|
||||
instance.id = id;
|
||||
return instance;
|
||||
|
|
|
@ -17,17 +17,15 @@ package google.registry.util;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
|
||||
import static google.registry.util.DomainNameUtils.getSecondLevelDomain;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link DomainNameUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class DomainNameUtilsTest {
|
||||
class DomainNameUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testCanonicalizeDomainName() {
|
||||
void testCanonicalizeDomainName() {
|
||||
assertThat(canonicalizeDomainName("foo")).isEqualTo("foo");
|
||||
assertThat(canonicalizeDomainName("FOO")).isEqualTo("foo");
|
||||
assertThat(canonicalizeDomainName("foo.tld")).isEqualTo("foo.tld");
|
||||
|
@ -41,12 +39,12 @@ public class DomainNameUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testCanonicalizeDomainName_acePrefixUnicodeChars() {
|
||||
void testCanonicalizeDomainName_acePrefixUnicodeChars() {
|
||||
assertThrows(IllegalArgumentException.class, () -> canonicalizeDomainName("xn--みんな"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSecondLevelDomain_returnsProperDomain() {
|
||||
void testGetSecondLevelDomain_returnsProperDomain() {
|
||||
assertThat(getSecondLevelDomain("foo.bar", "bar")).isEqualTo("foo.bar");
|
||||
assertThat(getSecondLevelDomain("ns1.foo.bar", "bar")).isEqualTo("foo.bar");
|
||||
assertThat(getSecondLevelDomain("ns1.abc.foo.bar", "bar")).isEqualTo("foo.bar");
|
||||
|
@ -54,20 +52,18 @@ public class DomainNameUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testGetSecondLevelDomain_insufficientDomainNameDepth() {
|
||||
void testGetSecondLevelDomain_insufficientDomainNameDepth() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> getSecondLevelDomain("bar", "bar"));
|
||||
assertThrows(IllegalArgumentException.class, () -> getSecondLevelDomain("bar", "bar"));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.isEqualTo("hostName must be at least one level below the tld");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSecondLevelDomain_domainNotUnderTld() {
|
||||
void testGetSecondLevelDomain_domainNotUnderTld() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> getSecondLevelDomain("foo.bar", "abc"));
|
||||
assertThrows(IllegalArgumentException.class, () -> getSecondLevelDomain("foo.bar", "abc"));
|
||||
assertThat(thrown).hasMessageThat().isEqualTo("hostName must be under the tld");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,19 +16,16 @@ package google.registry.util;
|
|||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link HexDumper}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class HexDumperTest {
|
||||
class HexDumperTest {
|
||||
|
||||
@Test
|
||||
public void testEmpty() {
|
||||
void testEmpty() {
|
||||
String input = "";
|
||||
String output = "[0 bytes total]\n";
|
||||
assertThat(input).isEmpty();
|
||||
|
@ -36,78 +33,84 @@ public class HexDumperTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testOneLine() {
|
||||
void testOneLine() {
|
||||
String input = "hello world";
|
||||
String output = "[11 bytes total]\n"
|
||||
+ "00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world \n";
|
||||
String output =
|
||||
"[11 bytes total]\n"
|
||||
+ "00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world \n";
|
||||
assertThat(input).hasLength(11);
|
||||
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiLine() {
|
||||
String input = ""
|
||||
+ "\n"
|
||||
+ "Maids heard the goblins cry:\n"
|
||||
+ "\"Come buy our orchard fruits,\n"
|
||||
+ "\"Come buy, come buy:\n";
|
||||
String output = "[81 bytes total]\n"
|
||||
+ "00000000 0a 4d 61 69 64 73 20 68 65 61 72 64 20 74 68 65 .Maids heard the\n"
|
||||
+ "00000016 20 67 6f 62 6c 69 6e 73 20 63 72 79 3a 0a 22 43 goblins cry:.\"C\n"
|
||||
+ "00000032 6f 6d 65 20 62 75 79 20 6f 75 72 20 6f 72 63 68 ome buy our orch\n"
|
||||
+ "00000048 61 72 64 20 66 72 75 69 74 73 2c 0a 22 43 6f 6d ard fruits,.\"Com\n"
|
||||
+ "00000064 65 20 62 75 79 2c 20 63 6f 6d 65 20 62 75 79 3a e buy, come buy:\n"
|
||||
+ "00000080 0a . \n";
|
||||
void testMultiLine() {
|
||||
String input =
|
||||
""
|
||||
+ "\n"
|
||||
+ "Maids heard the goblins cry:\n"
|
||||
+ "\"Come buy our orchard fruits,\n"
|
||||
+ "\"Come buy, come buy:\n";
|
||||
String output =
|
||||
"[81 bytes total]\n"
|
||||
+ "00000000 0a 4d 61 69 64 73 20 68 65 61 72 64 20 74 68 65 .Maids heard the\n"
|
||||
+ "00000016 20 67 6f 62 6c 69 6e 73 20 63 72 79 3a 0a 22 43 goblins cry:.\"C\n"
|
||||
+ "00000032 6f 6d 65 20 62 75 79 20 6f 75 72 20 6f 72 63 68 ome buy our orch\n"
|
||||
+ "00000048 61 72 64 20 66 72 75 69 74 73 2c 0a 22 43 6f 6d ard fruits,.\"Com\n"
|
||||
+ "00000064 65 20 62 75 79 2c 20 63 6f 6d 65 20 62 75 79 3a e buy, come buy:\n"
|
||||
+ "00000080 0a . \n";
|
||||
assertThat(input).hasLength(81);
|
||||
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFullLine() {
|
||||
void testFullLine() {
|
||||
String input = "hello worldddddd";
|
||||
String output = "[16 bytes total]\n"
|
||||
+ "00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 64 64 64 64 64 hello worldddddd\n";
|
||||
String output =
|
||||
"[16 bytes total]\n"
|
||||
+ "00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 64 64 64 64 64 hello worldddddd\n";
|
||||
assertThat(input).hasLength(16);
|
||||
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnicode() {
|
||||
void testUnicode() {
|
||||
String input = "(◕‿◕)";
|
||||
String output = "[11 bytes total]\n"
|
||||
+ "00000000 28 e2 97 95 e2 80 bf e2 97 95 29 (.........) \n";
|
||||
String output =
|
||||
"[11 bytes total]\n"
|
||||
+ "00000000 28 e2 97 95 e2 80 bf e2 97 95 29 (.........) \n";
|
||||
assertThat(input).hasLength(5);
|
||||
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8))).isEqualTo(output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRainbow() {
|
||||
void testRainbow() {
|
||||
byte[] input = new byte[256];
|
||||
for (int n = 0; n < 256; ++n) {
|
||||
input[n] = (byte) n;
|
||||
}
|
||||
String output = "[256 bytes total]\n"
|
||||
+ "00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n"
|
||||
+ "00000016 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................\n"
|
||||
+ "00000032 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
|
||||
+ "00000048 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
|
||||
+ "00000064 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
|
||||
+ "00000080 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
|
||||
+ "00000096 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
|
||||
+ "00000112 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
|
||||
+ "00000128 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................\n"
|
||||
+ "00000144 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................\n"
|
||||
+ "00000160 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af ................\n"
|
||||
+ "00000176 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf ................\n"
|
||||
+ "00000192 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf ................\n"
|
||||
+ "00000208 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df ................\n"
|
||||
+ "00000224 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef ................\n"
|
||||
+ "00000240 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff ................\n";
|
||||
String output =
|
||||
"[256 bytes total]\n"
|
||||
+ "00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n"
|
||||
+ "00000016 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................\n"
|
||||
+ "00000032 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
|
||||
+ "00000048 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
|
||||
+ "00000064 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
|
||||
+ "00000080 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
|
||||
+ "00000096 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
|
||||
+ "00000112 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
|
||||
+ "00000128 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................\n"
|
||||
+ "00000144 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................\n"
|
||||
+ "00000160 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af ................\n"
|
||||
+ "00000176 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf ................\n"
|
||||
+ "00000192 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf ................\n"
|
||||
+ "00000208 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df ................\n"
|
||||
+ "00000224 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef ................\n"
|
||||
+ "00000240 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff ................\n";
|
||||
assertThat(HexDumper.dumpHex(input)).isEqualTo(output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLineBuffering() throws Exception {
|
||||
void testLineBuffering() throws Exception {
|
||||
// Assume that we have some data that's N bytes long.
|
||||
byte[] data = "Sweet to tongue and sound to eye; Come buy, come buy.".getBytes(UTF_8);
|
||||
// And a streaming HexDumper that displays N+1 characters per row.
|
||||
|
@ -134,7 +137,7 @@ public class HexDumperTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFlush() throws Exception {
|
||||
void testFlush() throws Exception {
|
||||
try (StringWriter out = new StringWriter();
|
||||
HexDumper dumper = new HexDumper(out)) {
|
||||
dumper.write("hello ".getBytes(UTF_8));
|
||||
|
@ -146,56 +149,58 @@ public class HexDumperTest {
|
|||
dumper.flush();
|
||||
assertThat(out.toString()).isEqualTo("00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 ");
|
||||
dumper.close();
|
||||
assertThat(out.toString()).isEqualTo(
|
||||
"00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world \n");
|
||||
assertThat(out.toString())
|
||||
.isEqualTo(
|
||||
"00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 hello world \n");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPerLineIsOne() {
|
||||
void testPerLineIsOne() {
|
||||
String input = "hello";
|
||||
String output = "[5 bytes total]\n"
|
||||
+ "00000000 68 h\n"
|
||||
+ "00000001 65 e\n"
|
||||
+ "00000002 6c l\n"
|
||||
+ "00000003 6c l\n"
|
||||
+ "00000004 6f o\n";
|
||||
String output =
|
||||
"[5 bytes total]\n"
|
||||
+ "00000000 68 h\n"
|
||||
+ "00000001 65 e\n"
|
||||
+ "00000002 6c l\n"
|
||||
+ "00000003 6c l\n"
|
||||
+ "00000004 6f o\n";
|
||||
assertThat(HexDumper.dumpHex(input.getBytes(UTF_8), 1, 0)).isEqualTo(output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadArgumentPerLineZero() {
|
||||
void testBadArgumentPerLineZero() {
|
||||
HexDumper.dumpHex(new byte[1], 1, 0);
|
||||
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], 0, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadArgumentPerLineNegative() {
|
||||
void testBadArgumentPerLineNegative() {
|
||||
HexDumper.dumpHex(new byte[1], 1, 0);
|
||||
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], -1, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadArgumentPerGroupNegative() {
|
||||
void testBadArgumentPerGroupNegative() {
|
||||
HexDumper.dumpHex(new byte[1], 1, 0);
|
||||
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], 1, -1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadArgumentPerGroupGreaterThanOrEqualToPerLine() {
|
||||
void testBadArgumentPerGroupGreaterThanOrEqualToPerLine() {
|
||||
HexDumper.dumpHex(new byte[1], 1, 0);
|
||||
HexDumper.dumpHex(new byte[1], 2, 1);
|
||||
assertThrows(IllegalArgumentException.class, () -> HexDumper.dumpHex(new byte[1], 1, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadArgumentBytesIsNull() {
|
||||
void testBadArgumentBytesIsNull() {
|
||||
HexDumper.dumpHex(new byte[1]);
|
||||
assertThrows(NullPointerException.class, () -> HexDumper.dumpHex(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiClose() throws Exception {
|
||||
void testMultiClose() throws Exception {
|
||||
try (StringWriter out = new StringWriter();
|
||||
HexDumper dumper = new HexDumper(out)) {
|
||||
dumper.close();
|
||||
|
|
|
@ -20,16 +20,13 @@ import static google.registry.util.PasswordUtils.SALT_SUPPLIER;
|
|||
import static google.registry.util.PasswordUtils.hashPassword;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link google.registry.util.PasswordUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public final class PasswordUtilsTest {
|
||||
final class PasswordUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testDifferentSalts() {
|
||||
void testDifferentSalts() {
|
||||
byte[] first = SALT_SUPPLIER.get();
|
||||
byte[] second = SALT_SUPPLIER.get();
|
||||
assertThat(first.length).isEqualTo(32);
|
||||
|
@ -38,7 +35,7 @@ public final class PasswordUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testHash() {
|
||||
void testHash() {
|
||||
String salt = base64().encode(SALT_SUPPLIER.get());
|
||||
String password = "mySuperSecurePassword";
|
||||
String hashedPassword = hashPassword(password, salt);
|
||||
|
|
|
@ -1,263 +0,0 @@
|
|||
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
import static google.registry.testing.SystemInfo.hasCommand;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.common.io.Files;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** System integration tests for {@link PosixTarHeader}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class PosixTarHeaderSystemTest {
|
||||
|
||||
@Rule
|
||||
public final TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testCreateSingleFileArchive() throws Exception {
|
||||
assumeTrue(hasCommand("tar"));
|
||||
|
||||
// We have some data (in memory) that we'll call hello.txt.
|
||||
String fileName = "hello.txt";
|
||||
byte[] fileData = "hello world\n".getBytes(UTF_8);
|
||||
|
||||
// We're going to put it in a new tar archive (on the filesystem) named hello.tar.
|
||||
String tarName = "hello.tar";
|
||||
File tarFile = folder.newFile(tarName);
|
||||
try (FileOutputStream output = new FileOutputStream(tarFile)) {
|
||||
output.write(new PosixTarHeader.Builder()
|
||||
.setName(fileName)
|
||||
.setSize(fileData.length)
|
||||
.build()
|
||||
.getBytes());
|
||||
output.write(fileData);
|
||||
output.write(new byte[512 - fileData.length % 512]); // Align with 512-byte block size.
|
||||
output.write(new byte[1024]); // Bunch of null bytes to indicate end of archive.
|
||||
}
|
||||
assertThat(tarFile.length() % 512).isEqualTo(0);
|
||||
assertThat(tarFile.length() / 512).isEqualTo(2 + 2);
|
||||
|
||||
// Now we run the system's tar command to extract our file.
|
||||
String[] cmd = {"tar", "-xf", tarName};
|
||||
String[] env = {"PATH=" + System.getenv("PATH")};
|
||||
File cwd = folder.getRoot();
|
||||
Process pid = Runtime.getRuntime().exec(cmd, env, cwd);
|
||||
String err = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8));
|
||||
assertThat(pid.waitFor()).isEqualTo(0);
|
||||
assertThat(err.trim()).isEmpty();
|
||||
|
||||
// And verify that hello.txt came out.
|
||||
File dataFile = new File(cwd, fileName);
|
||||
assertThat(dataFile.exists()).isTrue();
|
||||
assertThat(dataFile.isFile()).isTrue();
|
||||
assertThat(Files.asByteSource(dataFile).read()).isEqualTo(fileData);
|
||||
|
||||
// And that nothing else came out.
|
||||
Set<String> expectedFiles = ImmutableSet.of(tarName, fileName);
|
||||
assertThat(ImmutableSet.copyOf(folder.getRoot().list())).isEqualTo(expectedFiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testCreateMultiFileArchive() throws Exception {
|
||||
assumeTrue(hasCommand("tar"));
|
||||
|
||||
Map<String, String> files = ImmutableMap.of(
|
||||
"one.txt", ""
|
||||
+ "There is data on line one\n"
|
||||
+ "and on line two\n"
|
||||
+ "and on line three\n",
|
||||
"two.txt", ""
|
||||
+ "There is even more data\n"
|
||||
+ "in this second file\n"
|
||||
+ "with its own three lines\n",
|
||||
"subdir/three.txt", ""
|
||||
+ "More data\n"
|
||||
+ "but only two lines\n");
|
||||
|
||||
String tarName = "hello.tar";
|
||||
File tarFile = folder.newFile(tarName);
|
||||
try (FileOutputStream output = new FileOutputStream(tarFile)) {
|
||||
for (String name : files.keySet()) {
|
||||
byte[] data = files.get(name).getBytes(UTF_8);
|
||||
output.write(new PosixTarHeader.Builder()
|
||||
.setName(name)
|
||||
.setSize(data.length)
|
||||
.build()
|
||||
.getBytes());
|
||||
output.write(data);
|
||||
output.write(new byte[512 - data.length % 512]);
|
||||
}
|
||||
output.write(new byte[1024]);
|
||||
}
|
||||
assertThat(tarFile.length() % 512).isEqualTo(0);
|
||||
assertThat(tarFile.length() / 512).isEqualTo(files.size() * 2 + 2);
|
||||
|
||||
String[] cmd = {"tar", "-xf", tarName};
|
||||
String[] env = {"PATH=" + System.getenv("PATH")};
|
||||
File cwd = folder.getRoot();
|
||||
Process pid = Runtime.getRuntime().exec(cmd, env, cwd);
|
||||
String err = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8));
|
||||
assertThat(pid.waitFor()).isEqualTo(0);
|
||||
assertThat(err.trim()).isEmpty();
|
||||
|
||||
for (String name : files.keySet()) {
|
||||
File file = new File(folder.getRoot(), name);
|
||||
assertWithMessage(name + " exists").that(file.exists()).isTrue();
|
||||
assertWithMessage(name + " is a file").that(file.isFile()).isTrue();
|
||||
byte[] data = files.get(name).getBytes(UTF_8);
|
||||
assertThat(Files.asByteSource(file).read()).isEqualTo(data);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testReadArchiveUstar() throws Exception {
|
||||
assumeTrue(hasCommand("tar"));
|
||||
|
||||
String one = "the first line";
|
||||
String two = "the second line";
|
||||
File cwd = folder.getRoot();
|
||||
Files.write(one.getBytes(UTF_8), new File(cwd, "one"));
|
||||
Files.write(two.getBytes(UTF_8), new File(cwd, "two"));
|
||||
|
||||
String[] cmd = {"tar", "--format=ustar", "-cf", "lines.tar", "one", "two"};
|
||||
String[] env = {"PATH=" + System.getenv("PATH")};
|
||||
Process pid = Runtime.getRuntime().exec(cmd, env, cwd);
|
||||
String err = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8));
|
||||
assertThat(pid.waitFor()).isEqualTo(0);
|
||||
assertThat(err.trim()).isEmpty();
|
||||
|
||||
PosixTarHeader header;
|
||||
byte[] block = new byte[512];
|
||||
try (FileInputStream input = new FileInputStream(new File(cwd, "lines.tar"))) {
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
header = PosixTarHeader.from(block);
|
||||
assertThat(header.getType()).isEqualTo(PosixTarHeader.Type.REGULAR);
|
||||
assertThat(header.getName()).isEqualTo("one");
|
||||
assertThat(header.getSize()).isEqualTo(one.length());
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
assertThat(one).isEqualTo(new String(block, 0, one.length(), UTF_8));
|
||||
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
header = PosixTarHeader.from(block);
|
||||
assertThat(header.getType()).isEqualTo(PosixTarHeader.Type.REGULAR);
|
||||
assertThat(header.getName()).isEqualTo("two");
|
||||
assertThat(header.getSize()).isEqualTo(two.length());
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
assertThat(two).isEqualTo(new String(block, 0, two.length(), UTF_8));
|
||||
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
assertWithMessage("End of archive marker corrupt").that(block).isEqualTo(new byte[512]);
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
assertWithMessage("End of archive marker corrupt").that(block).isEqualTo(new byte[512]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testReadArchiveDefaultFormat() throws Exception {
|
||||
assumeTrue(hasCommand("tar"));
|
||||
|
||||
String truth = "No one really knows\n";
|
||||
Files.write(truth.getBytes(UTF_8), folder.newFile("truth.txt"));
|
||||
|
||||
String[] cmd = {"tar", "-cf", "steam.tar", "truth.txt"};
|
||||
String[] env = {"PATH=" + System.getenv("PATH")};
|
||||
File cwd = folder.getRoot();
|
||||
Process pid = Runtime.getRuntime().exec(cmd, env, cwd);
|
||||
String err = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8));
|
||||
assertThat(pid.waitFor()).isEqualTo(0);
|
||||
assertThat(err.trim()).isEmpty();
|
||||
|
||||
PosixTarHeader header;
|
||||
byte[] block = new byte[512];
|
||||
try (FileInputStream input = new FileInputStream(new File(cwd, "steam.tar"))) {
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
header = PosixTarHeader.from(block);
|
||||
assertThat(header.getType()).isEqualTo(PosixTarHeader.Type.REGULAR);
|
||||
assertThat(header.getName()).isEqualTo("truth.txt");
|
||||
assertThat(header.getSize()).isEqualTo(truth.length());
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
assertThat(truth).isEqualTo(new String(block, 0, truth.length(), UTF_8));
|
||||
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
assertWithMessage("End of archive marker corrupt").that(block).isEqualTo(new byte[512]);
|
||||
assertThat(input.read(block)).isEqualTo(512);
|
||||
assertWithMessage("End of archive marker corrupt").that(block).isEqualTo(new byte[512]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testCreateBigWebScaleData() throws Exception {
|
||||
assumeTrue(hasCommand("tar"));
|
||||
|
||||
String name = "rando_numberissian.mov";
|
||||
byte[] data = new byte[4 * 1024 * 1024];
|
||||
Random rand = new Random();
|
||||
rand.nextBytes(data);
|
||||
|
||||
String tarName = "occupy.tar";
|
||||
File tarFile = folder.newFile(tarName);
|
||||
try (FileOutputStream output = new FileOutputStream(tarFile)) {
|
||||
output.write(new PosixTarHeader.Builder()
|
||||
.setName(name)
|
||||
.setSize(data.length)
|
||||
.build()
|
||||
.getBytes());
|
||||
output.write(data);
|
||||
output.write(new byte[1024]);
|
||||
}
|
||||
assertThat(tarFile.length() % 512).isEqualTo(0);
|
||||
|
||||
String[] cmd = {"tar", "-xf", tarName};
|
||||
String[] env = {"PATH=" + System.getenv("PATH")};
|
||||
File cwd = folder.getRoot();
|
||||
Process pid = Runtime.getRuntime().exec(cmd, env, cwd);
|
||||
String err = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8));
|
||||
assertThat(pid.waitFor()).isEqualTo(0);
|
||||
assertThat(err.trim()).isEmpty();
|
||||
|
||||
File dataFile = new File(cwd, name);
|
||||
assertThat(dataFile.exists()).isTrue();
|
||||
assertThat(dataFile.isFile()).isTrue();
|
||||
assertThat(Files.asByteSource(dataFile).read()).isEqualTo(data);
|
||||
|
||||
Set<String> expectedFiles = ImmutableSet.of(tarName, name);
|
||||
assertThat(ImmutableSet.copyOf(folder.getRoot().list())).isEqualTo(expectedFiles);
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ import static com.google.common.io.BaseEncoding.base64;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.testing.EqualsTester;
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
@ -28,15 +28,13 @@ import java.util.Arrays;
|
|||
import java.util.zip.GZIPInputStream;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link PosixTarHeader}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class PosixTarHeaderTest {
|
||||
class PosixTarHeaderTest {
|
||||
|
||||
@Test
|
||||
public void testGnuTarBlob() throws Exception {
|
||||
void testGnuTarBlob() throws Exception {
|
||||
// This data was generated as follows:
|
||||
//
|
||||
// echo hello kitty >hello.xml
|
||||
|
@ -119,7 +117,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFields() {
|
||||
void testFields() {
|
||||
PosixTarHeader header =
|
||||
new PosixTarHeader.Builder()
|
||||
.setType(PosixTarHeader.Type.REGULAR)
|
||||
|
@ -146,7 +144,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFieldsSomeMoar() {
|
||||
void testFieldsSomeMoar() {
|
||||
PosixTarHeader header =
|
||||
new PosixTarHeader.Builder()
|
||||
.setType(PosixTarHeader.Type.DIRECTORY)
|
||||
|
@ -171,7 +169,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testLoad() {
|
||||
void testLoad() {
|
||||
PosixTarHeader header =
|
||||
new PosixTarHeader.Builder()
|
||||
.setType(PosixTarHeader.Type.REGULAR)
|
||||
|
@ -199,7 +197,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testBadChecksum() {
|
||||
void testBadChecksum() {
|
||||
PosixTarHeader header =
|
||||
new PosixTarHeader.Builder().setName("(◕‿◕).txt").setSize(31337).build();
|
||||
byte[] bytes = header.getBytes();
|
||||
|
@ -211,7 +209,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testHashEquals() {
|
||||
void testHashEquals() {
|
||||
new EqualsTester()
|
||||
.addEqualityGroup(
|
||||
new PosixTarHeader.Builder()
|
||||
|
@ -241,7 +239,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testReadBsdTarFormatUstar() throws Exception {
|
||||
void testReadBsdTarFormatUstar() throws Exception {
|
||||
// $ tar --version
|
||||
// bsdtar 2.8.3 - libarchive 2.8.3
|
||||
|
||||
|
@ -299,7 +297,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testReadBsdTarFormatDefault() throws Exception {
|
||||
void testReadBsdTarFormatDefault() throws Exception {
|
||||
// $ tar --version
|
||||
// bsdtar 2.8.3 - libarchive 2.8.3
|
||||
|
||||
|
@ -357,7 +355,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testReadGnuTarFormatDefault() throws Exception {
|
||||
void testReadGnuTarFormatDefault() throws Exception {
|
||||
// $ tar --version
|
||||
// tar (GNU tar) 1.26
|
||||
|
||||
|
@ -415,7 +413,7 @@ public class PosixTarHeaderTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testReadGnuTarFormatUstar() throws Exception {
|
||||
void testReadGnuTarFormatUstar() throws Exception {
|
||||
// $ tar --version
|
||||
// tar (GNU tar) 1.26
|
||||
|
||||
|
|
|
@ -16,21 +16,18 @@ package google.registry.util;
|
|||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link RegistrarUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class RegistrarUtilsTest {
|
||||
class RegistrarUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testNormalizeRegistrarName_letterOrDigitOnly() {
|
||||
void testNormalizeRegistrarName_letterOrDigitOnly() {
|
||||
assertThat(RegistrarUtils.normalizeRegistrarName("129abzAZ")).isEqualTo("129abzaz");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalizeRegistrarName_hasSymbols() {
|
||||
void testNormalizeRegistrarName_hasSymbols() {
|
||||
assertThat(RegistrarUtils.normalizeRegistrarName("^}129a(bzAZ/:")).isEqualTo("129abzaz");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,21 +15,18 @@
|
|||
package google.registry.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeSleeper;
|
||||
import google.registry.util.Retrier.FailureReporter;
|
||||
import java.util.concurrent.Callable;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link Retrier}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class RetrierTest {
|
||||
class RetrierTest {
|
||||
|
||||
Retrier retrier = new Retrier(new FakeSleeper(new FakeClock()), 3);
|
||||
private Retrier retrier = new Retrier(new FakeSleeper(new FakeClock()), 3);
|
||||
|
||||
/** An exception to throw from {@link CountingThrower}. */
|
||||
static class CountingException extends RuntimeException {
|
||||
|
@ -70,7 +67,7 @@ public class RetrierTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testRetryableException() {
|
||||
void testRetryableException() {
|
||||
CountingException thrown =
|
||||
assertThrows(
|
||||
CountingException.class,
|
||||
|
@ -79,7 +76,7 @@ public class RetrierTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testUnretryableException() {
|
||||
void testUnretryableException() {
|
||||
CountingException thrown =
|
||||
assertThrows(
|
||||
CountingException.class,
|
||||
|
@ -88,14 +85,13 @@ public class RetrierTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testRetrySucceeded() {
|
||||
assertThat(retrier.callWithRetry(new CountingThrower(2), CountingException.class))
|
||||
.isEqualTo(2);
|
||||
void testRetrySucceeded() {
|
||||
assertThat(retrier.callWithRetry(new CountingThrower(2), CountingException.class)).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("AssertThrowsMultipleStatements")
|
||||
public void testRetryFailed_withReporter() {
|
||||
void testRetryFailed_withReporter() {
|
||||
CountingException thrown =
|
||||
assertThrows(
|
||||
CountingException.class,
|
||||
|
@ -112,7 +108,7 @@ public class RetrierTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testRetrySucceeded_withReporter() {
|
||||
void testRetrySucceeded_withReporter() {
|
||||
TestReporter reporter = new TestReporter();
|
||||
assertThat(retrier.callWithRetry(new CountingThrower(2), reporter, CountingException.class))
|
||||
.isEqualTo(2);
|
||||
|
@ -120,7 +116,7 @@ public class RetrierTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFirstTrySucceeded_withReporter() {
|
||||
void testFirstTrySucceeded_withReporter() {
|
||||
TestReporter reporter = new TestReporter();
|
||||
assertThat(retrier.callWithRetry(new CountingThrower(0), reporter, CountingException.class))
|
||||
.isEqualTo(0);
|
||||
|
@ -128,14 +124,14 @@ public class RetrierTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testRetryPredicate_succeedsWhenRetries() {
|
||||
void testRetryPredicate_succeedsWhenRetries() {
|
||||
// Throws a retryable "1" exception is retryable, and then it succeeds on "1".
|
||||
assertThat(retrier.callWithRetry(new CountingThrower(1), e -> e.getMessage().equals("1")))
|
||||
.isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRetryPredicate_failsWhenDoesntRetry() {
|
||||
void testRetryPredicate_failsWhenDoesntRetry() {
|
||||
// Throws a retryable "1" exception, then a non-retryable "2" exception, resulting in failure.
|
||||
CountingException ex =
|
||||
assertThrows(
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
package google.registry.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
@ -32,20 +32,15 @@ import javax.mail.Message.RecipientType;
|
|||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
/** Unit tests for {@link SendEmailService}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class SendEmailServiceTest {
|
||||
|
||||
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SendEmailServiceTest {
|
||||
|
||||
private final Retrier retrier = new Retrier(new FakeSleeper(new FakeClock()), 2);
|
||||
private final TransportEmailSender wrapper = mock(TransportEmailSender.class);
|
||||
|
@ -54,7 +49,7 @@ public class SendEmailServiceTest {
|
|||
@Captor private ArgumentCaptor<Message> messageCaptor;
|
||||
|
||||
@Test
|
||||
public void testSuccess_simple() throws Exception {
|
||||
void testSuccess_simple() throws Exception {
|
||||
EmailMessage content = createBuilder().build();
|
||||
sendEmailService.sendEmail(content);
|
||||
Message message = getMessage();
|
||||
|
@ -73,7 +68,7 @@ public class SendEmailServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_bcc() throws Exception {
|
||||
void testSuccess_bcc() throws Exception {
|
||||
EmailMessage content =
|
||||
createBuilder()
|
||||
.setBccs(
|
||||
|
@ -90,7 +85,7 @@ public class SendEmailServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_contentType() throws Exception {
|
||||
void testSuccess_contentType() throws Exception {
|
||||
EmailMessage content = createBuilder().setContentType(MediaType.HTML_UTF_8).build();
|
||||
sendEmailService.sendEmail(content);
|
||||
Message message = getMessage();
|
||||
|
@ -98,7 +93,7 @@ public class SendEmailServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_attachment() throws Exception {
|
||||
void testSuccess_attachment() throws Exception {
|
||||
EmailMessage content =
|
||||
createBuilder()
|
||||
.setAttachment(
|
||||
|
@ -117,7 +112,7 @@ public class SendEmailServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_retry() throws Exception {
|
||||
void testSuccess_retry() throws Exception {
|
||||
doThrow(new MessagingException("hi"))
|
||||
.doNothing()
|
||||
.when(wrapper)
|
||||
|
@ -128,7 +123,7 @@ public class SendEmailServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_wrongExceptionType() throws Exception {
|
||||
void testFailure_wrongExceptionType() throws Exception {
|
||||
doThrow(new RuntimeException("this is a runtime exception")).when(wrapper).sendMessage(any());
|
||||
EmailMessage content = createBuilder().build();
|
||||
RuntimeException thrown =
|
||||
|
@ -137,7 +132,7 @@ public class SendEmailServiceTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_tooManyTries() throws Exception {
|
||||
void testFailure_tooManyTries() throws Exception {
|
||||
doThrow(new MessagingException("hi"))
|
||||
.doThrow(new MessagingException("second"))
|
||||
.when(wrapper)
|
||||
|
|
|
@ -17,15 +17,12 @@ package google.registry.util;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.util.SerializeUtils.deserialize;
|
||||
import static google.registry.util.SerializeUtils.serialize;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link SerializeUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class SerializeUtilsTest {
|
||||
class SerializeUtilsTest {
|
||||
|
||||
static class Lol {
|
||||
@Override
|
||||
|
@ -33,30 +30,31 @@ public class SerializeUtilsTest {
|
|||
return "LOL_VALUE";
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize_nullValue_returnsNull() {
|
||||
void testSerialize_nullValue_returnsNull() {
|
||||
assertThat(serialize(null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialize_nullValue_returnsNull() {
|
||||
void testDeserialize_nullValue_returnsNull() {
|
||||
assertThat(deserialize(Object.class, null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeDeserialize_stringValue_maintainsValue() {
|
||||
void testSerializeDeserialize_stringValue_maintainsValue() {
|
||||
assertThat(deserialize(String.class, serialize("hello"))).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialize_objectDoesntImplementSerialize_hasInformativeError() {
|
||||
void testSerialize_objectDoesntImplementSerialize_hasInformativeError() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(IllegalArgumentException.class, () -> serialize(new Lol()));
|
||||
assertThat(thrown).hasMessageThat().contains("Unable to serialize: LOL_VALUE");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialize_badValue_hasInformativeError() {
|
||||
void testDeserialize_badValue_hasInformativeError() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
|
|
@ -15,39 +15,30 @@
|
|||
package google.registry.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link SqlTemplate}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class SqlTemplateTest {
|
||||
class SqlTemplateTest {
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate() {
|
||||
void testFillSqlTemplate() {
|
||||
assertThat(SqlTemplate.create("%TEST%").put("TEST", "hello world").build())
|
||||
.isEqualTo("hello world");
|
||||
assertThat(SqlTemplate.create("one %TWO% three").put("TWO", "2").build())
|
||||
.isEqualTo("one 2 three");
|
||||
assertThat(
|
||||
SqlTemplate.create("%TEST%")
|
||||
.put("TEST", "hello world")
|
||||
.build())
|
||||
.isEqualTo("hello world");
|
||||
assertThat(
|
||||
SqlTemplate.create("one %TWO% three")
|
||||
.put("TWO", "2")
|
||||
.build())
|
||||
.isEqualTo("one 2 three");
|
||||
assertThat(
|
||||
SqlTemplate.create("%ONE% %TWO% %THREE%")
|
||||
.put("ONE", "1")
|
||||
.put("TWO", "2")
|
||||
.put("THREE", "3")
|
||||
.build())
|
||||
.isEqualTo("1 2 3");
|
||||
SqlTemplate.create("%ONE% %TWO% %THREE%")
|
||||
.put("ONE", "1")
|
||||
.put("TWO", "2")
|
||||
.put("THREE", "3")
|
||||
.build())
|
||||
.isEqualTo("1 2 3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_substitutionButNoVariables() {
|
||||
void testFillSqlTemplate_substitutionButNoVariables() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> SqlTemplate.create("").put("ONE", "1").build());
|
||||
|
@ -55,7 +46,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_substitutionButMissingVariables() {
|
||||
void testFillSqlTemplate_substitutionButMissingVariables() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -64,7 +55,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_sameKeyTwice_failsEarly() {
|
||||
void testFillSqlTemplate_sameKeyTwice_failsEarly() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -73,7 +64,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_variablesButNotEnoughSubstitutions() {
|
||||
void testFillSqlTemplate_variablesButNotEnoughSubstitutions() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -82,7 +73,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_mismatchedVariableAndSubstitution() {
|
||||
void testFillSqlTemplate_mismatchedVariableAndSubstitution() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -91,14 +82,14 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_missingKeyVals_whatsThePoint() {
|
||||
void testFillSqlTemplate_missingKeyVals_whatsThePoint() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(IllegalArgumentException.class, () -> SqlTemplate.create("%TWO%").build());
|
||||
assertThat(thrown).hasMessageThat().contains("%TWO% found in template but no substitution");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_lowercaseKey_notAllowed() {
|
||||
void testFillSqlTemplate_lowercaseKey_notAllowed() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -107,7 +98,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_substitution_disallowsSingleQuotes() {
|
||||
void testFillSqlTemplate_substitution_disallowsSingleQuotes() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -116,7 +107,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_substitution_disallowsDoubleQuotes() {
|
||||
void testFillSqlTemplate_substitution_disallowsDoubleQuotes() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -125,7 +116,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_quoteMismatch_throwsError() {
|
||||
void testFillSqlTemplate_quoteMismatch_throwsError() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -135,7 +126,7 @@ public class SqlTemplateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testFillSqlTemplate_extendedQuote_throwsError() {
|
||||
void testFillSqlTemplate_extendedQuote_throwsError() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
|
|
@ -17,28 +17,25 @@ package google.registry.util;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link TeeOutputStream}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class TeeOutputStreamTest {
|
||||
class TeeOutputStreamTest {
|
||||
|
||||
private final ByteArrayOutputStream outputA = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream outputB = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream outputC = new ByteArrayOutputStream();
|
||||
|
||||
@Test
|
||||
public void testWrite_writesToMultipleStreams() throws Exception {
|
||||
void testWrite_writesToMultipleStreams() throws Exception {
|
||||
// Write shared data using the tee output stream.
|
||||
try (OutputStream tee =
|
||||
new TeeOutputStream(asList(outputA, outputB, outputC))) {
|
||||
try (OutputStream tee = new TeeOutputStream(asList(outputA, outputB, outputC))) {
|
||||
tee.write("hello ".getBytes(UTF_8));
|
||||
tee.write("hello world!".getBytes(UTF_8), 6, 5);
|
||||
tee.write('!');
|
||||
|
@ -55,12 +52,12 @@ public class TeeOutputStreamTest {
|
|||
|
||||
@Test
|
||||
@SuppressWarnings("resource")
|
||||
public void testConstructor_failsWithEmptyIterable() {
|
||||
void testConstructor_failsWithEmptyIterable() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new TeeOutputStream(ImmutableSet.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteInteger_failsAfterClose() throws Exception {
|
||||
void testWriteInteger_failsAfterClose() throws Exception {
|
||||
OutputStream tee = new TeeOutputStream(ImmutableList.of(outputA));
|
||||
tee.close();
|
||||
IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> tee.write(1));
|
||||
|
@ -68,7 +65,7 @@ public class TeeOutputStreamTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testWriteByteArray_failsAfterClose() throws Exception {
|
||||
void testWriteByteArray_failsAfterClose() throws Exception {
|
||||
OutputStream tee = new TeeOutputStream(ImmutableList.of(outputA));
|
||||
tee.close();
|
||||
IllegalStateException thrown =
|
||||
|
@ -77,7 +74,7 @@ public class TeeOutputStreamTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testWriteByteSubarray_failsAfterClose() throws Exception {
|
||||
void testWriteByteSubarray_failsAfterClose() throws Exception {
|
||||
OutputStream tee = new TeeOutputStream(ImmutableList.of(outputA));
|
||||
tee.close();
|
||||
IllegalStateException thrown =
|
||||
|
|
|
@ -15,26 +15,24 @@
|
|||
package google.registry.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link TypeUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class TypeUtilsTest {
|
||||
class TypeUtilsTest {
|
||||
|
||||
@Test
|
||||
public void test_getClassFromString_validClass() {
|
||||
void test_getClassFromString_validClass() {
|
||||
Class<? extends Serializable> clazz =
|
||||
TypeUtils.getClassFromString("java.util.ArrayList", Serializable.class);
|
||||
assertThat(clazz).isEqualTo(ArrayList.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getClassFromString_notAssignableFrom() {
|
||||
void test_getClassFromString_notAssignableFrom() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -43,7 +41,7 @@ public class TypeUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_getClassFromString_unknownClass() {
|
||||
void test_getClassFromString_unknownClass() {
|
||||
IllegalArgumentException thrown =
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
|
@ -62,7 +60,7 @@ public class TypeUtilsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void test_instantiateWithArg() {
|
||||
void test_instantiateWithArg() {
|
||||
Class<ExampleClass> clazz =
|
||||
TypeUtils.getClassFromString(
|
||||
"google.registry.util.TypeUtilsTest$ExampleClass", ExampleClass.class);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue