[java-identity-provider] 01/02: IDP-1299 Name Replacement Property rewriter

Rod Widdowson rdw at steadingsoftware.com
Wed Jun 6 11:57:37 EDT 2018


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=901ce91ee1bd74d9a28dda7efa4144e13627fe9b

commit 901ce91ee1bd74d9a28dda7efa4144e13627fe9b
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Wed Jun 6 14:30:46 2018 +0100

    IDP-1299 Name Replacement Property rewriter
    
    https://issues.shibboleth.net/jira/browse/IDP-1299
    
    Amendments to the PropertiesWithComments class to allow a property
    file to be rewritten with different property names.
    
    In theory it can do value replacement at the same time.
    
    Also tests for same.
---
 .../idp/installer/impl/PropertiesWithComments.java | 52 +++++++++++++++--
 .../idp/installer/TestPropertiesWithComments.java  | 66 +++++++++++++++++-----
 .../net/shibboleth/idp/installer/file.properties   |  2 +-
 .../idp/installer/nameReplace.properties           |  4 ++
 4 files changed, 106 insertions(+), 18 deletions(-)

diff --git a/idp-installer/src/main/java/net/shibboleth/idp/installer/impl/PropertiesWithComments.java b/idp-installer/src/main/java/net/shibboleth/idp/installer/impl/PropertiesWithComments.java
index 6537c18..4f46ff0 100644
--- a/idp-installer/src/main/java/net/shibboleth/idp/installer/impl/PropertiesWithComments.java
+++ b/idp-installer/src/main/java/net/shibboleth/idp/installer/impl/PropertiesWithComments.java
@@ -53,6 +53,15 @@ public class PropertiesWithComments {
     /** The properties bit. */
     private Map<String, CommentedProperty> properties;
 
+    /** Name Replacement info. */
+    private final Properties nameReplacement = new Properties();
+
+    /** Have we loaded data?.
+     *
+     * We cannot load the replacement names after the file load.
+     * */
+    private boolean loadedData;
+
     /**
      * Add a property, either as a key/value pair or as a key/comment pair.
      * 
@@ -73,15 +82,38 @@ public class PropertiesWithComments {
 
         parser.load(new ByteArrayInputStream(modifiedLine.getBytes()));
         if (!parser.isEmpty()) {
-            final String propName = StringSupport.trimOrNull(parser.stringPropertyNames().iterator().next());
+            String propName = StringSupport.trimOrNull(parser.stringPropertyNames().iterator().next());
             if (propName != null) {
+                
+                String outputLine = line;
+                final String value = parser.getProperty(propName);
+                
+                final String newPropName = StringSupport.trimOrNull(nameReplacement.getProperty(propName));
+
+                if (newPropName != null && !newPropName.isEmpty()) {
+                    // Change the line
+                    if (isComment) {
+                        if (newPropName.contains(propName)) {
+                            // We can only replace once
+                            outputLine = outputLine.replace(propName, newPropName);
+                        } else {
+                            while (outputLine.contains(propName)) {
+                                outputLine = outputLine.replace(propName, newPropName);
+                            }
+                        }
+                    }
+                    // and the property name
+                    propName = newPropName;
+                }
+                
+                
                 final CommentedProperty commentedProperty;
 
                 if (isComment) {
-                    commentedProperty = new CommentedProperty(propName, line, true);
+                    commentedProperty = new CommentedProperty(propName, outputLine, true);
 
                 } else {
-                    commentedProperty = new CommentedProperty(propName, parser.getProperty(propName), false);
+                    commentedProperty = new CommentedProperty(propName, value, false);
 
                 }
                 properties.put(propName, commentedProperty);
@@ -91,7 +123,18 @@ public class PropertiesWithComments {
             contents.add(line);
         }
         parser.clear();
-
+    }
+    
+    /** Read the name replacement data. 
+    * 
+    * @param input what to read
+    * @throws IOException if readline fails
+    */
+    public void loadNameReplacement(final InputStream input) throws IOException {
+        if (loadedData) {
+            throw new IOException("Cannot load name replacement after the data");
+        }
+        nameReplacement.load(input);
     }
 
     /**
@@ -123,6 +166,7 @@ public class PropertiesWithComments {
             }
             s = reader.readLine();
         }
+        loadedData = true;
     }
 
     /**
diff --git a/idp-installer/src/test/java/net/shibboleth/idp/installer/TestPropertiesWithComments.java b/idp-installer/src/test/java/net/shibboleth/idp/installer/TestPropertiesWithComments.java
index c384365..87a5c8e 100644
--- a/idp-installer/src/test/java/net/shibboleth/idp/installer/TestPropertiesWithComments.java
+++ b/idp-installer/src/test/java/net/shibboleth/idp/installer/TestPropertiesWithComments.java
@@ -17,6 +17,8 @@
 
 package net.shibboleth.idp.installer;
 
+import static org.testng.Assert.fail;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -28,8 +30,8 @@ import java.util.Properties;
 import net.shibboleth.idp.installer.impl.PropertiesWithComments;
 
 import org.testng.Assert;
-import org.testng.annotations.AfterClass;
-import org.testng.annotations.BeforeClass;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 /**
@@ -37,26 +39,31 @@ import org.testng.annotations.Test;
  */
 public class TestPropertiesWithComments {
 
+    // We use a File to aid scrutabiloty in testing
     private File testFile;
 
-    @BeforeClass public void setup() throws IOException {
+    @BeforeMethod public void setup() throws IOException {
         testFile = File.createTempFile("test", ".properties");
     }
 
-    @AfterClass public void tearDown() {
+    @AfterMethod public void tearDown() {
         testFile.delete();
     }
+    
+    private InputStream getInputStream() {
+        return getClass().getClassLoader().getResourceAsStream("net/shibboleth/idp/installer/file.properties");
+    }
+    
+    private InputStream getNameReplacementStream() {
+        return getClass().getClassLoader().getResourceAsStream("net/shibboleth/idp/installer/nameReplace.properties");
+    }
 
-    @Test public void test() throws FileNotFoundException, IOException {
-        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("net/shibboleth/idp/installer/file.properties");
-        
+
+    @Test public void testReplaceValues() throws FileNotFoundException, IOException {
         final PropertiesWithComments pwc = new PropertiesWithComments();
-        try {
-            pwc.load(stream);
-        } catch (IOException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
+
+        pwc.load(getInputStream());
+
         Assert.assertTrue(pwc.replaceProperty("p", "321"));
         pwc.addComment("Comment");
         Assert.assertFalse(pwc.replaceProperty("nn", "123"));
@@ -75,4 +82,37 @@ public class TestPropertiesWithComments {
         Assert.assertEquals(p.getProperty("q"), "elephants");
         
     }
+
+    @Test public void testReplaceNamesFail() throws FileNotFoundException, IOException {
+        final PropertiesWithComments pwc = new PropertiesWithComments();
+        pwc.load(getInputStream());
+        try {
+            pwc.loadNameReplacement(getInputStream());
+            fail("Expected an IO Exception");
+        } 
+        catch (IOException e) {
+            // Expected
+        }
+    }
+
+
+    @Test public void testReplaceNames() throws FileNotFoundException, IOException {
+        
+        final PropertiesWithComments pwc = new PropertiesWithComments();
+        pwc.loadNameReplacement(getNameReplacementStream());
+        pwc.load(getInputStream());
+        
+        pwc.store(new FileOutputStream(testFile));
+        
+        final Properties p = new Properties();
+        
+        p.load(new FileInputStream(testFile));
+        
+        Assert.assertEquals(p.stringPropertyNames().size(), 2);
+        Assert.assertEquals(p.getProperty("p"), "123");
+        Assert.assertEquals(p.getProperty("elephantName"), "elephants");
+        
+    }
+
+    
 }
diff --git a/idp-installer/src/test/resources/net/shibboleth/idp/installer/file.properties b/idp-installer/src/test/resources/net/shibboleth/idp/installer/file.properties
index 4892da1..e08a078 100644
--- a/idp-installer/src/test/resources/net/shibboleth/idp/installer/file.properties
+++ b/idp-installer/src/test/resources/net/shibboleth/idp/installer/file.properties
@@ -1,5 +1,5 @@
 #Start with a Comment
-# yy   = elephant
+# yy   = elephant and put in some more crap about yy
 p=123
 q=elephants
 #End with a Comment
\ No newline at end of file
diff --git a/idp-installer/src/test/resources/net/shibboleth/idp/installer/nameReplace.properties b/idp-installer/src/test/resources/net/shibboleth/idp/installer/nameReplace.properties
new file mode 100644
index 0000000..4f13f8f
--- /dev/null
+++ b/idp-installer/src/test/resources/net/shibboleth/idp/installer/nameReplace.properties
@@ -0,0 +1,4 @@
+# name replacement
+yy=zz
+ll=zz
+q=elephantName
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list