Skip to content

Commit

Permalink
Improve test resource to File conversion
Browse files Browse the repository at this point in the history
`new File(url.getPath())` and `new File(url.getFile())` work incorrectly
when url (such as test resource) happens to be something else than
`file://`. The `new File(url.toURI())` conversion does all the proper
checks.
  • Loading branch information
findepi committed Apr 1, 2022
1 parent 2d22bc9 commit c10b78c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void setup()

URL resource = getClass().getClassLoader().getResource("33.privateKey");
assertNotNull(resource, "key directory not found");
File keyDir = new File(resource.getFile()).getAbsoluteFile().getParentFile();
File keyDir = new File(resource.toURI()).getAbsoluteFile().getParentFile();

defaultKey = hmacShaKeyFor(getMimeDecoder().decode(asCharSource(new File(keyDir, "default-key.key"), US_ASCII).read().getBytes(US_ASCII)));
hmac222 = hmacShaKeyFor(getMimeDecoder().decode(asCharSource(new File(keyDir, "222.key"), US_ASCII).read().getBytes(US_ASCII)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public class TestResourceSecurity

static {
try {
JWK_PRIVATE_KEY = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/jwk-rsa-private.pem").getPath()), Optional.empty());
JWK_PRIVATE_KEY = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/jwk-rsa-private.pem").toURI()), Optional.empty());
}
catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ public void testJwtRsa()
RSAPublicKey publicKey = (RSAPublicKey) keys.get("test-rsa");
assertNotNull(publicKey);

RSAPublicKey expectedPublicKey = (RSAPublicKey) PemReader.loadPublicKey(new File(Resources.getResource("jwk/jwk-rsa-public.pem").getPath()));
RSAPublicKey expectedPublicKey = (RSAPublicKey) PemReader.loadPublicKey(new File(Resources.getResource("jwk/jwk-rsa-public.pem").toURI()));
assertEquals(publicKey.getPublicExponent(), expectedPublicKey.getPublicExponent());
assertEquals(publicKey.getModulus(), expectedPublicKey.getModulus());

PrivateKey privateKey = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/jwk-rsa-private.pem").getPath()), Optional.empty());
PrivateKey privateKey = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/jwk-rsa-private.pem").toURI()), Optional.empty());
String jwt = newJwtBuilder()
.signWith(privateKey)
.setHeaderParam(JwsHeader.KEY_ID, "test-rsa")
Expand Down Expand Up @@ -319,14 +319,14 @@ private static void assertJwtEc(String keyName, ECParameterSpec expectedSpec)

assertSame(publicKey.getParams(), expectedSpec);

ECPublicKey expectedPublicKey = (ECPublicKey) PemReader.loadPublicKey(new File(Resources.getResource("jwk/" + keyName + "-public.pem").getPath()));
ECPublicKey expectedPublicKey = (ECPublicKey) PemReader.loadPublicKey(new File(Resources.getResource("jwk/" + keyName + "-public.pem").toURI()));
assertEquals(publicKey.getW(), expectedPublicKey.getW());
assertEquals(publicKey.getParams().getCurve(), expectedPublicKey.getParams().getCurve());
assertEquals(publicKey.getParams().getGenerator(), expectedPublicKey.getParams().getGenerator());
assertEquals(publicKey.getParams().getOrder(), expectedPublicKey.getParams().getOrder());
assertEquals(publicKey.getParams().getCofactor(), expectedPublicKey.getParams().getCofactor());

PrivateKey privateKey = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/" + keyName + "-private.pem").getPath()), Optional.empty());
PrivateKey privateKey = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/" + keyName + "-private.pem").toURI()), Optional.empty());
String jwt = newJwtBuilder()
.signWith(privateKey)
.setHeaderParam(JwsHeader.KEY_ID, keyName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class TestWebUi

static {
try {
JWK_PRIVATE_KEY = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/jwk-rsa-private.pem").getPath()), Optional.empty());
JWK_PRIVATE_KEY = PemReader.loadPrivateKey(new File(Resources.getResource("jwk/jwk-rsa-private.pem").toURI()), Optional.empty());
}
catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.testng.annotations.Test;

import java.io.File;
import java.net.URISyntaxException;
import java.util.EnumSet;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -477,8 +478,13 @@ private static ConnectorSecurityContext user(String name, Set<String> groups)

private static ConnectorAccessControl createAccessControl(String fileName)
{
File configFile = new File(getResource(fileName).getPath());
return new FileBasedAccessControl(new CatalogName("test_catalog"), configFile);
try {
File configFile = new File(getResource(fileName).toURI());
return new FileBasedAccessControl(new CatalogName("test_catalog"), configFile);
}
catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

private static void assertDenied(ThrowingRunnable runnable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Objects;
Expand Down Expand Up @@ -100,7 +101,8 @@ public void testReadLastCheckpointFileForMultipart()

private Stream<String> readJsonTransactionLogs(String location)
{
File[] files = new File(getClass().getClassLoader().getResource(location).getPath()).listFiles((dir, name) -> name.matches("[0-9]{20}\\.json"));
File directory = directoryForResource(location);
File[] files = directory.listFiles((dir, name) -> name.matches("[0-9]{20}\\.json"));
verify(files != null);
return Arrays.stream(files)
.sorted()
Expand Down Expand Up @@ -130,4 +132,14 @@ private DeltaLakeTransactionLogEntry deserialize(String json)
throw new RuntimeException("Failed to parse " + json, e);
}
}

private File directoryForResource(String location)
{
try {
return new File(getClass().getClassLoader().getResource(location).toURI());
}
catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -80,9 +79,9 @@ public class TestDeltaLakeFileStatistics

@Test
public void testParseJsonStatistics()
throws IOException
throws Exception
{
File statsFile = new File(getClass().getResource("all_type_statistics.json").getFile());
File statsFile = new File(getClass().getResource("all_type_statistics.json").toURI());
DeltaLakeFileStatistics fileStatistics = objectMapper.readValue(statsFile, DeltaLakeJsonFileStatistics.class);
testStatisticsValues(fileStatistics);
}
Expand All @@ -91,7 +90,7 @@ public void testParseJsonStatistics()
public void testParseParquetStatistics()
throws Exception
{
File statsFile = new File(getClass().getResource("/databricks/pruning/parquet_struct_statistics/_delta_log/00000000000000000010.checkpoint.parquet").getFile());
File statsFile = new File(getClass().getResource("/databricks/pruning/parquet_struct_statistics/_delta_log/00000000000000000010.checkpoint.parquet").toURI());
Path checkpointPath = new Path(statsFile.toURI());

TypeManager typeManager = TESTING_TYPE_MANAGER;
Expand Down

0 comments on commit c10b78c

Please sign in to comment.