Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[PIE-1531] Allow whitespace in file paths loaded from resources directory #1329

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ public PantheonNode createIbftNodeWithValidators(final String name, final String

private Optional<String> createCliqueGenesisConfig(
final Collection<? extends RunnableNode> validators) {
final String template = readGenesisFile("clique/clique.json");
final String template = readGenesisFile("/clique/clique.json");
return updateGenesisExtraData(
validators, template, CliqueExtraData::createGenesisExtraDataString);
}

private Optional<String> createIbftGenesisConfig(
final Collection<? extends RunnableNode> validators) {
final String template = readGenesisFile("ibft/ibft.json");
final String template = readGenesisFile("/ibft/ibft.json");
return updateGenesisExtraData(
validators, template, IbftExtraData::createGenesisExtraDataString);
}
Expand All @@ -292,7 +292,7 @@ private Optional<String> updateGenesisExtraData(

private String readGenesisFile(final String filepath) {
try {
final URI uri = Resources.getResource(filepath).toURI();
final URI uri = this.getClass().getResource(filepath).toURI();
return Resources.toString(uri.toURL(), Charset.defaultCharset());
} catch (final URISyntaxException | IOException e) {
throw new IllegalStateException("Unable to get test genesis config " + filepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public PermissionedNodeBuilder nodesContractEnabled(final String address) {

public PermissionedNodeBuilder genesisFile(final String path) {
try {
URI uri = Resources.getResource(path).toURI();
URI uri = this.getClass().getResource(path).toURI();
this.genesisFile = Resources.toString(uri.toURL(), Charset.defaultCharset());
} catch (final URISyntaxException | IOException e) {
throw new IllegalStateException("Unable to read genesis file from: " + path, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ private GenesisConfigFile(final JsonObject config) {

public static GenesisConfigFile mainnet() {
try {
return fromConfig(Resources.toString(Resources.getResource("mainnet.json"), UTF_8));
return fromConfig(
Resources.toString(GenesisConfigFile.class.getResource("/mainnet.json"), UTF_8));
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}

public static GenesisConfigFile development() {
try {
return fromConfig(Resources.toString(Resources.getResource("dev.json"), UTF_8));
return fromConfig(
Resources.toString(GenesisConfigFile.class.getResource("/dev.json"), UTF_8));
} catch (final IOException e) {
throw new IllegalStateException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private void updateWorldStateForDao(final MutableWorldState worldState) {
final JsonArray json =
new JsonArray(
Resources.toString(
Resources.getResource("daoAddresses.json"), StandardCharsets.UTF_8));
this.getClass().getResource("/daoAddresses.json"), StandardCharsets.UTF_8));
final List<Address> addresses =
IntStream.range(0, json.size())
.mapToObj(json::getString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void shouldCreateRopstenConfig() throws Exception {
MainnetProtocolSchedule.fromConfig(
GenesisConfigFile.fromConfig(
Resources.toString(
Resources.getResource("ropsten.json"), StandardCharsets.UTF_8))
this.getClass().getResource("/ropsten.json"), StandardCharsets.UTF_8))
.getConfigOptions(),
PrivacyParameters.DEFAULT);
Assertions.assertThat(sched.getByBlockNumber(0).getName()).isEqualTo("TangerineWhistle");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static BlockchainSetupUtil<Void> forTesting() {
final TemporaryFolder temp = new TemporaryFolder();
try {
temp.create();
final URL genesisFileUrl = getResourceUrl(temp, "testGenesis.json");
final URL genesisFileUrl = getResourceUrl(temp, "/testGenesis.json");
final GenesisState genesisState =
GenesisState.fromJson(
Resources.toString(genesisFileUrl, Charsets.UTF_8), protocolSchedule);
Expand All @@ -107,7 +107,7 @@ public static BlockchainSetupUtil<Void> forTesting() {
final ProtocolContext<Void> protocolContext =
new ProtocolContext<>(blockchain, worldArchive, null);

final Path blocksPath = getResourcePath(temp, "testBlockchain.blocks");
final Path blocksPath = getResourcePath(temp, "/testBlockchain.blocks");
final List<Block> blocks = new ArrayList<>();
final BlockHashFunction blockHashFunction =
ScheduleBasedBlockHashFunction.create(protocolSchedule);
Expand All @@ -128,7 +128,7 @@ public static BlockchainSetupUtil<Void> forTesting() {

private static Path getResourcePath(final TemporaryFolder temp, final String resource)
throws IOException {
final URL url = Resources.getResource(resource);
final URL url = BlockchainSetupUtil.class.getResource(resource);
final Path path =
Files.write(
temp.newFile().toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class BlockBodiesMessageTest {
public void blockBodiesRoundTrip() throws IOException {
final List<BlockBody> bodies = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class BlockHeadersMessageTest {
public void blockHeadersRoundTrip() throws IOException {
final List<BlockHeader> headers = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class GetBlockBodiesMessageTest {
public void getBlockBodiesRoundTrip() throws IOException {
final List<Hash> hashes = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class NewBlockHashesMessageTest {
public void blockHeadersRoundTrip() throws IOException {
final List<NewBlockHashesMessage.NewBlockHash> hashes = new ArrayList<>();
final ByteBuffer buffer =
ByteBuffer.wrap(Resources.toByteArray(Resources.getResource("50.blocks")));
ByteBuffer.wrap(Resources.toByteArray(this.getClass().getResource("/50.blocks")));
for (int i = 0; i < 50; ++i) {
final int blockSize = RLP.calculateSize(BytesValue.wrapBuffer(buffer));
final byte[] block = new byte[blockSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void permissioningConfig() throws Exception {
final String uri = "enode://" + VALID_NODE_ID + "@192.168.0.9:4567";
final String uri2 = "enode://" + VALID_NODE_ID + "@192.169.0.9:4568";

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_VALID);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_VALID);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration = permissioningConfig(toml);
Expand All @@ -71,7 +71,7 @@ public void permissioningConfig() throws Exception {
public void permissioningConfigWithOnlyNodeWhitelistSet() throws Exception {
final String uri = "enode://" + VALID_NODE_ID + "@192.168.0.9:4567";

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration =
Expand All @@ -85,7 +85,7 @@ public void permissioningConfigWithOnlyNodeWhitelistSet() throws Exception {

@Test
public void permissioningConfigWithOnlyAccountWhitelistSet() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_ACCOUNT_WHITELIST_ONLY);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_ACCOUNT_WHITELIST_ONLY);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration =
Expand All @@ -100,7 +100,7 @@ public void permissioningConfigWithOnlyAccountWhitelistSet() throws Exception {

@Test
public void permissioningConfigWithInvalidAccount() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_INVALID_ACCOUNT);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_INVALID_ACCOUNT);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> accountOnlyPermissioningConfig(toml));
Expand All @@ -112,7 +112,7 @@ public void permissioningConfigWithInvalidAccount() throws Exception {

@Test
public void permissioningConfigWithInvalidEnode() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_INVALID_ENODE);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_INVALID_ENODE);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> nodeOnlyPermissioningConfig(toml));
Expand All @@ -124,7 +124,7 @@ public void permissioningConfigWithInvalidEnode() throws Exception {

@Test
public void permissioningConfigWithEmptyWhitelistMustNotError() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_EMPTY_WHITELISTS);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_EMPTY_WHITELISTS);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration = permissioningConfig(toml);
Expand All @@ -137,7 +137,7 @@ public void permissioningConfigWithEmptyWhitelistMustNotError() throws Exception

@Test
public void permissioningConfigWithAbsentWhitelistMustThrowException() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_ABSENT_WHITELISTS);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_ABSENT_WHITELISTS);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> permissioningConfig(toml));
Expand All @@ -147,7 +147,7 @@ public void permissioningConfigWithAbsentWhitelistMustThrowException() throws Ex

@Test
public void permissioningConfigWithUnrecognizedKeyMustThrowException() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_UNRECOGNIZED_KEY);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_UNRECOGNIZED_KEY);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

final Throwable thrown = catchThrowable(() -> accountOnlyPermissioningConfig(toml));
Expand All @@ -170,7 +170,7 @@ public void permissioningConfigWithEmptyFileMustThrowException() throws Exceptio

@Test
public void permissioningConfigFromFileMustSetFilePath() throws Exception {
final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_VALID);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_VALID);
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

LocalPermissioningConfiguration permissioningConfiguration =
Expand All @@ -196,7 +196,7 @@ public void permissioningConfigFromNonexistentFileMustThrowException() {
@Test
public void permissioningConfigFromMultilineFileMustParseCorrectly() throws Exception {
final URL configFile =
Resources.getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY_MULTILINE);
this.getClass().getResource(PERMISSIONING_CONFIG_NODE_WHITELIST_ONLY_MULTILINE);
final LocalPermissioningConfiguration permissioningConfiguration =
PermissioningConfigurationBuilder.permissioningConfiguration(
true, configFile.getPath(), false, configFile.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private SmartContractPermissioningController setupController(
final String resourceName, final String contractAddressString) throws IOException {
final ProtocolSchedule<Void> protocolSchedule = MainnetProtocolSchedule.create();

final String emptyContractFile = Resources.toString(Resources.getResource(resourceName), UTF_8);
final String emptyContractFile =
Resources.toString(this.getClass().getResource(resourceName), UTF_8);
final GenesisState genesisState =
GenesisState.fromConfig(GenesisConfigFile.fromConfig(emptyContractFile), protocolSchedule);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class EthNetworkConfig {
public static final int RINKEBY_NETWORK_ID = 4;
public static final int GOERLI_NETWORK_ID = 5;
public static final int DEV_NETWORK_ID = 2018;
private static final String MAINNET_GENESIS = "mainnet.json";
private static final String ROPSTEN_GENESIS = "ropsten.json";
private static final String RINKEBY_GENESIS = "rinkeby.json";
private static final String GOERLI_GENESIS = "goerli.json";
private static final String DEV_GENESIS = "dev.json";
private static final String MAINNET_GENESIS = "/mainnet.json";
private static final String ROPSTEN_GENESIS = "/ropsten.json";
private static final String RINKEBY_GENESIS = "/rinkeby.json";
private static final String GOERLI_GENESIS = "/goerli.json";
private static final String DEV_GENESIS = "/dev.json";
private final String genesisConfig;
private final int networkId;
private final Collection<URI> bootNodes;
Expand Down Expand Up @@ -118,7 +118,7 @@ public static EthNetworkConfig getNetworkConfig(final NetworkName networkName) {

private static String jsonConfig(final String resourceName) {
try {
final URI uri = Resources.getResource(resourceName).toURI();
final URI uri = EthNetworkConfig.class.getResource(resourceName).toURI();
return Resources.toString(uri.toURL(), UTF_8);
} catch (final URISyntaxException | IOException e) {
throw new IllegalStateException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
private final String ENCLAVE_PUBLIC_KEY = "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=";
private final String VALID_NODE_ID =
"6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0";
static final String PERMISSIONING_CONFIG_TOML = "permissioning_config.toml";
static final String PERMISSIONING_CONFIG_TOML = "/permissioning_config.toml";

private static final JsonRpcConfiguration defaultJsonRpcConfiguration;
private static final WebSocketConfiguration defaultWebSocketConfiguration;
Expand Down Expand Up @@ -249,7 +249,7 @@ public void callingWithConfigOptionButInvalidValueTomlFileShouldDisplayHelp() th
public void overrideDefaultValuesIfKeyIsPresentInConfigFile() throws IOException {
assumeTrue(isFullInstantiation());

final URL configFile = Resources.getResource("complete_config.toml");
final URL configFile = this.getClass().getResource("/complete_config.toml");
final Path genesisFile = createFakeGenesisFile(GENESIS_VALID_JSON);
final String updatedConfig =
Resources.toString(configFile, UTF_8)
Expand Down Expand Up @@ -434,7 +434,7 @@ public void accountPermissioningEnabledWithNonexistentConfigFileMustError() {
@Test
public void nodePermissioningTomlFileWithNoPermissionsEnabledMustNotError() throws IOException {

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_TOML);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_TOML);
final Path permToml = createTempFile("toml", Resources.toByteArray(configFile));
parseCommand("--permissions-nodes-config-file", permToml.toString());

Expand All @@ -448,7 +448,7 @@ public void nodePermissioningTomlFileWithNoPermissionsEnabledMustNotError() thro
public void accountPermissioningTomlFileWithNoPermissionsEnabledMustNotError()
throws IOException {

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_TOML);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_TOML);
final Path permToml = createTempFile("toml", Resources.toByteArray(configFile));
parseCommand("--permissions-accounts-config-file", permToml.toString());

Expand Down Expand Up @@ -477,7 +477,7 @@ public void nodePermissioningTomlPathMustUseOption() throws IOException {
URI.create(
"enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@192.169.0.9:4568"));

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_TOML);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_TOML);
final Path permToml = createTempFile("toml", Resources.toByteArray(configFile));

final String whitelistedNodesString =
Expand Down Expand Up @@ -508,7 +508,7 @@ public void nodePermissioningTomlPathMustUseOption() throws IOException {
@Test
public void accountPermissioningTomlPathMustUseOption() throws IOException {

final URL configFile = Resources.getResource(PERMISSIONING_CONFIG_TOML);
final URL configFile = this.getClass().getResource(PERMISSIONING_CONFIG_TOML);
final Path permToml = createTempFile("toml", Resources.toByteArray(configFile));

parseCommand(
Expand Down Expand Up @@ -538,7 +538,7 @@ public void tomlThatConfiguresEverythingExceptPermissioningToml() throws IOExcep
assumeTrue(isFullInstantiation());

// Load a TOML that configures literally everything (except permissioning TOML config)
final URL configFile = Resources.getResource("everything_config.toml");
final URL configFile = this.getClass().getResource("/everything_config.toml");
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));

// Parse it.
Expand Down Expand Up @@ -581,7 +581,7 @@ public void tomlThatConfiguresEverythingExceptPermissioningToml() throws IOExcep
public void noOverrideDefaultValuesIfKeyIsNotPresentInConfigFile() throws IOException {
assumeTrue(isFullInstantiation());

final String configFile = Resources.getResource("partial_config.toml").getFile();
final String configFile = this.getClass().getResource("/partial_config.toml").getFile();

parseCommand("--config-file", configFile);
final JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault();
Expand Down Expand Up @@ -2129,7 +2129,7 @@ public void fullCLIOptionsShownWhenNotInDockerContainer() {

@Test
public void mustUseEnclaveUriAndOptions() throws IOException {
final URL configFile = Resources.getResource("orion_publickey.pub");
final URL configFile = this.getClass().getResource("/orion_publickey.pub");

parseCommand(
"--privacy-enabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.common.io.Resources;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -85,6 +84,6 @@ commandLine, getFilePath(DUPLICATE_USER_TOML), "HTTP"))
}

private String getFilePath(final String resourceName) {
return Resources.getResource(resourceName).getPath();
return this.getClass().getResource(resourceName).getPath();
}
}
Loading