下面列出了怎么用org.junit.contrib.java.lang.system.EnvironmentVariables的API类实例代码及写法,或者点击链接到github查看源代码。
private void initEnv() {
if (!environmentInitialized) {
final String internalEmulatorProp = System.getProperty(BigtableEmulator.INTERNAL_PROPERTY);
if (TestUtils.isSet(internalEmulatorProp)) {
internalEmulator = Boolean.parseBoolean(internalEmulatorProp);
LOGGER.warn("Bigtable internal emulator enabled: " + internalEmulator);
} else {
LOGGER.warn("Bigtable internal emulator disabled by default");
}
final String hostPortProp = System.getProperty(BigtableEmulator.HOST_PORT_PROPERTY);
if (TestUtils.isSet(hostPortProp)) {
emulatorHostPort = hostPortProp;
LOGGER.warn("Bigtable emulator will run at: " + emulatorHostPort);
} else {
LOGGER.warn("Bigtable emulator will run at default location: " + emulatorHostPort);
}
// Set the host:port property in the junit env, even if external
// gcloud emulator
final EnvironmentVariables environmentVariables = new EnvironmentVariables();
environmentVariables.set("BIGTABLE_EMULATOR_HOST", emulatorHostPort);
environmentInitialized = true;
}
}
@Before
public void init() {
final String filePath = getClass().getResource("/key.secret").getPath();
envVariables.set(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH, filePath);
resolver = new EncryptedStringResolver();
}
public static void setLogLevelForChaincode(final EnvironmentVariables environmentVariables, final ChaincodeBase cb, final String shimLevel,
final String chaincodeLevel) {
environmentVariables.set(ChaincodeBase.CORE_CHAINCODE_LOGGING_SHIM, shimLevel);
environmentVariables.set(ChaincodeBase.CORE_CHAINCODE_LOGGING_LEVEL, chaincodeLevel);
cb.processEnvironmentOptions();
cb.initializeLogging();
}
@Test
public void testNotAbleToReadSecret() {
envVariables.set(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH, "not-existed");
assertThat(ConfigSecretReader.readSecretFromFile()).isEmpty();
}
@Test
public void envNotSet() {
envVariables.clear(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH);
assertThat(ConfigSecretReader.readSecretFromFile()).isEmpty();
}
SolaceJavaAutoConfigurationTestBase(Class<? extends SpringJCSMPFactoryCloudFactory> configClass) {
this.configClass = configClass;
this.environmentVariables = new EnvironmentVariables();
}
@Test
public void testReadSecret() {
envVariables.set(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH, filePath);
Optional<char[]> secret = ConfigSecretReader.readSecretFromFile();
assertThat(secret).isPresent();
assertThat(secret.get()).isEqualTo("quorum".toCharArray());
}
@Test
public void testReadException() throws IOException {
envVariables.clear(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH);
final File tempFile = tempDir.newFile("key.secret");
tempFile.setReadable(false);
envVariables.set(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH,
tempFile.getAbsolutePath());
assertThat(ConfigSecretReader.readSecretFromFile()).isEmpty();
}
@Test
public void testUnMarshallWithUserInputSecret() {
envVariables.clear(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH);
resolver = new EncryptedStringResolver();
ByteArrayInputStream in = new ByteArrayInputStream(("quorum" + System.lineSeparator() + "quorum").getBytes());
System.setIn(in);
assertThat(resolver.resolve("ENC(KLa6pRQpxI8Ez3Bo6D3cI6y13YYdntu7)")).isEqualTo("password");
System.setIn(System.in);
}
@Test
public void testUnMarshallWrongPassword() {
envVariables.clear(com.quorum.tessera.config.util.EnvironmentVariables.CONFIG_SECRET_PATH);
resolver = new EncryptedStringResolver();
ByteArrayInputStream in = new ByteArrayInputStream(("bogus" + System.lineSeparator() + "bogus").getBytes());
System.setIn(in);
assertThatExceptionOfType(EncryptionOperationNotPossibleException.class)
.isThrownBy(() -> resolver.resolve("ENC(KLa6pRQpxI8Ez3Bo6D3cI6y13YYdntu7)"));
System.setIn(System.in);
}