changeset 1231:a094a9b3a0bf

Comments in deployment.properties now persists saving via itweb-settings
author Jiri Vanek <jvanek@redhat.com>
date Fri, 22 May 2015 19:32:35 +0200
parents 1b191e3c2f91
children 6e7d203b9374
files ChangeLog netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java tests/netx/unit/net/sourceforge/jnlp/config/DeploymentConfigurationTest.java
diffstat 4 files changed, 157 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri May 22 13:09:52 2015 +0200
+++ b/ChangeLog	Fri May 22 19:32:35 2015 +0200
@@ -1,3 +1,17 @@
+2015-05-22  Jiri Vanek  <jvanek@redhat.com>
+
+	Comments in deployment.properties now persists saving via itweb-settings
+	* netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java: (loadComments)
+	new method. Saves every line starting with # char except date and
+	DEPLOYMENT_COMMENT. (load) This method is called before every load, and read lines
+	are stored. (save) Saved comments are inserted to other comments before call to 
+	store.
+	* tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java: (countOccurences)
+	made public so it can be reused
+	* tests/netx/unit/net/sourceforge/jnlp/config/DeploymentConfigurationTest.java:
+	added tests to verify preserving of comments and non-multiplying date and
+	DEPLOYMENT_COMMENT
+
 2015-05-22  Jiri Vanek  <jvanek@redhat.com>
 
 	Removed garbage from htmls' width and height
--- a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java	Fri May 22 13:09:52 2015 +0200
+++ b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java	Fri May 22 19:32:35 2015 +0200
@@ -30,6 +30,8 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.channels.FileLock;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -55,6 +57,8 @@
     public static final String APPLET_TRUST_SETTINGS = ".appletTrustSettings";
 
     public static final String DEPLOYMENT_COMMENT = "Netx deployment configuration";
+    public String userComments;
+    public String systemComments;
 
     public static final int JNLP_ASSOCIATION_NEVER = 0;
     public static final int JNLP_ASSOCIATION_NEW_ONLY = 1;
@@ -231,7 +235,7 @@
     public void resetToDefaults() {
         currentConfiguration = Defaults.getDefaults();
     }
-    
+
 
     public enum ConfigType {
         System, User
@@ -308,6 +312,7 @@
                 /* Second, read the System level deployment.properties file */
                 systemProperties = loadProperties(ConfigType.System, systemPropertiesFile,
                         systemPropertiesMandatory);
+                systemComments=loadComments(systemPropertiesFile);
             }
             if (systemProperties != null) {
                 mergeMaps(initialProperties, systemProperties);
@@ -326,6 +331,7 @@
          */
         userPropertiesFile = userFile;
         Map<String, Setting<String>> userProperties = loadProperties(ConfigType.User, userPropertiesFile, false);
+        userComments=loadComments(userPropertiesFile);
         if (userProperties != null) {
             mergeMaps(initialProperties, userProperties);
         }
@@ -631,7 +637,11 @@
 
         FileUtils.createParentDir(userPropertiesFile);
         try (OutputStream out = new BufferedOutputStream(new FileOutputStream(userPropertiesFile))) {
-            toSave.store(out, DEPLOYMENT_COMMENT);
+            String comments = DEPLOYMENT_COMMENT;
+            if (userComments.length() > 0) {
+                comments = comments + System.lineSeparator() + userComments;
+            }
+            toSave.store(out, comments); ;
         }
     }
 
@@ -856,4 +866,43 @@
             return 1;
         }
     }
+    
+    //standard date.toString format
+    public static final SimpleDateFormat pattern = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
+    
+    private static String loadComments(File path) {
+        StringBuilder r = new StringBuilder();
+        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
+            while (true) {
+                String s = br.readLine();
+                if (s == null) {
+                    break;
+                }
+                s = s.trim();
+                if (s.startsWith("#")) {
+                    String decommented = s.substring(1);
+                    if (decommented.isEmpty()){
+                        continue;
+                    }
+                    if (decommented.equals(DEPLOYMENT_COMMENT)){
+                        continue;
+                    }
+                    //there is always also date
+                    Date dd = null;
+                    try {
+                        dd = pattern.parse(decommented);
+                    } catch (Exception ex) {
+                        //we really dont care, failure is our decision point
+                    }
+                    if (dd == null){
+                        r.append(decommented).append("\n");
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            OutputController.getLogger().log(ex);
+        }
+        
+        return r.toString().trim();
+    }
 }
--- a/tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java	Fri May 22 13:09:52 2015 +0200
+++ b/tests/netx/unit/net/sourceforge/jnlp/PluginBridgeTest.java	Fri May 22 19:32:35 2015 +0200
@@ -362,7 +362,7 @@
 
     ;
     
-    private static int countOccurences(String str, String findStr) {
+    public static int countOccurences(String str, String findStr) {
         int lastIndex = 0;
         int count = 0;
         while (lastIndex != -1) {
--- a/tests/netx/unit/net/sourceforge/jnlp/config/DeploymentConfigurationTest.java	Fri May 22 13:09:52 2015 +0200
+++ b/tests/netx/unit/net/sourceforge/jnlp/config/DeploymentConfigurationTest.java	Fri May 22 19:32:35 2015 +0200
@@ -37,15 +37,22 @@
 
 package net.sourceforge.jnlp.config;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Properties;
 
 import javax.naming.ConfigurationException;
+import net.sourceforge.jnlp.PluginBridgeTest;
+import net.sourceforge.jnlp.util.FileUtils;
+import net.sourceforge.jnlp.util.logging.NoStdOutErrTest;
+import org.junit.Assert;
 
 import org.junit.Test;
 
-public class DeploymentConfigurationTest {
+public class DeploymentConfigurationTest extends NoStdOutErrTest{
 
     @Test
     public void testLoad() throws ConfigurationException {
@@ -68,5 +75,88 @@
 
         assertTrue(target.size() != 0);
     }
+    
+    @Test
+    public void testPersistedComments() throws ConfigurationException, IOException {
+        final File f = File.createTempFile("proeprties", "withComments");
+        f.deleteOnExit();
+        FileUtils.saveFile("#commented1=val1\nproeprty2=val2\n#commented3=val3\nproeprty4=val4", f);
+        DeploymentConfiguration dc = new DeploymentConfiguration(new InfrastructureFileDescriptor(){
+            
+            @Override
+            public String getFullPath() {
+                return f.getAbsolutePath();
+            }
+            
+        });
+        dc.load();
+        Assert.assertEquals("val2", dc.getProperty("proeprty2"));
+        Assert.assertEquals("val4", dc.getProperty("proeprty4"));
+        Assert.assertEquals(null, dc.getProperty("commented1"));
+        Assert.assertEquals(null, dc.getProperty("commented3"));
+        
+        dc.save();
+        
+        String s = FileUtils.loadFileAsString(f);
+        Assert.assertTrue(s.contains("#"+DeploymentConfiguration.DEPLOYMENT_COMMENT));
+        String date = new Date().toString().substring(0, 10); //every propertiews file have header and date by default
+        Assert.assertTrue(s.contains("#"+date)); //check day part of date...
+        Assert.assertTrue(s.contains("#commented1"));
+        Assert.assertTrue(s.contains("proeprty2"));
+        Assert.assertTrue(s.contains("#commented3"));
+        Assert.assertTrue(s.contains("proeprty4"));
+        Assert.assertTrue(s.contains("val1"));
+        Assert.assertTrue(s.contains("val2"));
+        Assert.assertTrue(s.contains("val3"));
+        Assert.assertTrue(s.contains("val4"));
+      
+        }
+    
+    
+    
+    @Test
+    public void testEnsurePersistedCommentsDoNotMultiplyHeaderAndDate() throws ConfigurationException, IOException {
+        final File f = File.createTempFile("proeprties", "withComments");
+        f.deleteOnExit();
+        FileUtils.saveFile("#commented1=val1\nproeprty2=val2\n#commented3=val3\nproeprty4=val4", f);
+        DeploymentConfiguration dc = new DeploymentConfiguration(new InfrastructureFileDescriptor() {
 
+            @Override
+            public String getFullPath() {
+                return f.getAbsolutePath();
+            }
+
+        });
+        String s = null;
+        for (int x = 0; x < 10; x++) {
+            dc.load();
+            Assert.assertEquals("val2", dc.getProperty("proeprty2"));
+            Assert.assertEquals("val4", dc.getProperty("proeprty4"));
+            Assert.assertEquals(null, dc.getProperty("commented1"));
+            Assert.assertEquals(null, dc.getProperty("commented3"));
+
+            dc.save();
+
+            s = FileUtils.loadFileAsString(f);
+            for (int y = 0; x < x; x++) {
+                //ensure salt
+                Assert.assertTrue(s.contains("#id" + y + "id"));
+            }
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, DeploymentConfiguration.DEPLOYMENT_COMMENT));
+            String date = new Date().toString().substring(0, 10); //every propertiews file have header and date by default
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, date)); //check day part of date...
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "#commented1"));
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "proeprty2"));
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "#commented3"));
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "proeprty4"));
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "val1"));
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "val2"));
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "val3"));
+            Assert.assertEquals(1, PluginBridgeTest.countOccurences(s, "val4"));
+            //insert some salt to check if it really iterates
+            FileUtils.saveFile(s + "\n#id" + x + "id", f);
+        }
+        System.out.println(s);
     }
+
+}