Skip to content

Commit

Permalink
Guard EncryptedProperties store operations due to Java 11 changes (GH E…
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Jul 21, 2022
1 parent a37e63b commit e4a5b83
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,27 @@ private final File getTempPropertiesFile() throws IOException {
Object prop2 = EncryptedPropertiesUtils.addProperty(props, KEY2, VALUE2);
assertNull("Expected null but returned: " + prop2, prop2);

//store the file
EncryptedPropertiesUtils.storeProperties(encryptedFile.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");

//try reading in the resulting file
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
loadedProps.load(new FileReader(encryptedFile.getAbsolutePath()));

assertEquals(VALUE1, loadedProps.getProperty(KEY1));
assertEquals(VALUE2, loadedProps.getProperty(KEY2));
// Probing for support of storeProperties due to Java 11 and GH #721
boolean supported = true;
try {
//store the file
EncryptedPropertiesUtils.storeProperties(encryptedFile.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
}
catch ( UnsupportedOperationException ex ) {
supported = false;
}

if ( supported ) {
//try reading in the resulting file
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
loadedProps.load(new FileReader(encryptedFile.getAbsolutePath()));

assertEquals(VALUE1, loadedProps.getProperty(KEY1));
assertEquals(VALUE2, loadedProps.getProperty(KEY2));
}
else {
System.out.println("Skipping testCreateNew... storeProperties is not supported.");
}
}

/**
Expand All @@ -113,7 +125,7 @@ private final File getTempPropertiesFile() throws IOException {
File encryptedFile = getTempPropertiesFile();
File plainTextFile = getTempPropertiesFile();

//write an initial plaintext properties file
//write an initial plaintext properties file
Properties props = new Properties();
props.setProperty(KEY3, VALUE3);
props.setProperty(KEY4, VALUE4);
Expand All @@ -127,15 +139,27 @@ private final File getTempPropertiesFile() throws IOException {
assertEquals(VALUE3, props.getProperty(KEY3));
assertEquals(VALUE4, props.getProperty(KEY4));

//store the file
EncryptedPropertiesUtils.storeProperties(encryptedFile.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");

//try reading in the resulting file
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
loadedProps.load(new FileReader(encryptedFile.getAbsolutePath()));

assertEquals(VALUE3, loadedProps.getProperty(KEY3));
assertEquals(VALUE4, loadedProps.getProperty(KEY4));
// Probing for support of storeProperties due to Java 11 and GH #721
boolean supported = true;
try {
//store the file
EncryptedPropertiesUtils.storeProperties(encryptedFile.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
}
catch ( UnsupportedOperationException ex ) {
supported = false;
}

if ( supported ) {
//try reading in the resulting file
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
loadedProps.load(new FileReader(encryptedFile.getAbsolutePath()));

assertEquals(VALUE3, loadedProps.getProperty(KEY3));
assertEquals(VALUE4, loadedProps.getProperty(KEY4));
}
else {
System.out.println("Skipping testLoadPlaintextAndEncrypt... storeProperties is not supported.");
}
}

/**
Expand All @@ -161,16 +185,28 @@ private final File getTempPropertiesFile() throws IOException {
assertEquals(VALUE1, props.getProperty(KEY1));
assertEquals(VALUE2, props.getProperty(KEY2));

//store the file
EncryptedPropertiesUtils.storeProperties(encryptedFile2.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");

//try reading in the resulting file
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
loadedProps.load(new FileReader(encryptedFile2.getAbsolutePath()));

//test the values read in
assertEquals(VALUE1, loadedProps.getProperty(KEY1));
assertEquals(VALUE2, loadedProps.getProperty(KEY2));
// Probing for support of storeProperties due to Java 11 and GH #721
boolean supported = true;
try {
//store the file
EncryptedPropertiesUtils.storeProperties(encryptedFile2.getAbsolutePath(), props, "Encrypted Properties File generated by EncryptedPropertiesUtilsTest");
}
catch ( UnsupportedOperationException ex ) {
supported = false;
}

if ( supported ) {
//try reading in the resulting file
ReferenceEncryptedProperties loadedProps = new ReferenceEncryptedProperties();
loadedProps.load(new FileReader(encryptedFile2.getAbsolutePath()));

//test the values read in
assertEquals(VALUE1, loadedProps.getProperty(KEY1));
assertEquals(VALUE2, loadedProps.getProperty(KEY2));
}
else {
System.out.println("Skipping testLoadEncryptedAndAdd... storeProperties is not supported.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -154,58 +154,71 @@ else if(key.equals("two"))
boolean sawSeuss = false;

ReferenceEncryptedProperties toStore = new ReferenceEncryptedProperties();
toStore.setProperty("one", "two");
toStore.setProperty("two", "three");
toStore.setProperty("seuss.schneier", "one fish, twofish, red fish, blowfish");
toStore.store(baos, "testStore");

bais = new ByteArrayInputStream(baos.toByteArray());
toLoad.load(bais);
// Probing for support of store due to Java 11 and GH #721
boolean supported = true;
try {
toStore.setProperty("one", "two");
toStore.setProperty("two", "three");
toStore.setProperty("seuss.schneier", "one fish, twofish, red fish, blowfish");
toStore.store(baos, "testStore");
}
catch ( UnsupportedOperationException ex ) {
supported = false;
}

for(Iterator i=toLoad.keySet().iterator();i.hasNext();)
{
String key = (String)i.next();
if ( supported ) {
bais = new ByteArrayInputStream(baos.toByteArray());
toLoad.load(bais);

assertNotNull("key returned from keySet() iterator was null", key);
if(key.equals("one"))
if(sawOne)
fail("Key one seen more than once.");
else
{
sawOne = true;
assertEquals("Key one's value was not two", "two", toLoad.getProperty("one"));
}
else if(key.equals("two"))
if(sawTwo)
fail("Key two seen more than once.");
else
{
sawTwo = true;
assertEquals("Key two's value was not three", "three", toLoad.getProperty("two"));
}
else if(key.equals("seuss.schneier"))
if(sawSeuss)
fail("Key seuss.schneier seen more than once.");
for(Iterator i=toLoad.keySet().iterator();i.hasNext();)
{
String key = (String)i.next();

assertNotNull("key returned from keySet() iterator was null", key);
if(key.equals("one"))
if(sawOne)
fail("Key one seen more than once.");
else
{
sawSeuss = true;
assertEquals("Key seuss.schneier's value was not expected value",
"one fish, twofish, red fish, blowfish",
toStore.getProperty("seuss.schneier"));
sawOne = true;
assertEquals("Key one's value was not two", "two", toLoad.getProperty("one"));
}
else
fail("Unset key " + key + " returned from keySet().iterator()");
else if(key.equals("two"))
if(sawTwo)
fail("Key two seen more than once.");
else
{
sawTwo = true;
assertEquals("Key two's value was not three", "three", toLoad.getProperty("two"));
}
else if(key.equals("seuss.schneier"))
if(sawSeuss)
fail("Key seuss.schneier seen more than once.");
else
{
sawSeuss = true;
assertEquals("Key seuss.schneier's value was not expected value",
"one fish, twofish, red fish, blowfish",
toStore.getProperty("seuss.schneier"));
}
else
fail("Unset key " + key + " returned from keySet().iterator()");
}
assertTrue("Key one was never seen", sawOne);
assertTrue("Key two was never seen", sawTwo);
}
else {
System.out.println("Skipping testStoreLoad... store is not supported.");
}
assertTrue("Key one was never seen", sawOne);
assertTrue("Key two was never seen", sawTwo);
}

/**
* Test storing and loading of encrypted properties.
*/
@Test public void testStoreLoadWithReader() throws Exception
{
/*
/*
//create an EncryptedProperties to store
ReferenceEncryptedProperties toStore = new ReferenceEncryptedProperties();
toStore.setProperty("one", "two");
Expand Down Expand Up @@ -263,7 +276,7 @@ else if(key.equals("seuss.schneier"))
}
assertTrue("Key one was never seen", sawOne);
assertTrue("Key two was never seen", sawTwo);
*/
*/
}

/**
Expand Down

0 comments on commit e4a5b83

Please sign in to comment.