details:   http://code.openbravo.com/erp/devel/pi/rev/7b7927b8293f
changeset: 3609:7b7927b8293f
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Tue Apr 21 15:00:46 2009 +0200
summary:   fixed bug 0008639: Upgrading core does not update 
Openbravo.properties

details:   http://code.openbravo.com/erp/devel/pi/rev/f419abea42e6
changeset: 3610:f419abea42e6
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Tue Apr 21 15:01:22 2009 +0200
summary:   added test case for issue 0008639

diffstat:

 src-test/org/openbravo/test/modularity/MergePropertiesTest.java |  112 
++++++++++++++
 src/org/openbravo/erpCommon/modules/ImportModule.java           |    9 +
 src/org/openbravo/erpCommon/utility/Utility.java                |   43 +++++
 3 files changed, 164 insertions(+), 0 deletions(-)

diffs (213 lines):

diff --git a/src-test/org/openbravo/test/modularity/MergePropertiesTest.java 
b/src-test/org/openbravo/test/modularity/MergePropertiesTest.java
new file mode 100644
--- /dev/null
+++ b/src-test/org/openbravo/test/modularity/MergePropertiesTest.java
@@ -0,0 +1,112 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SL 
+ * All portions are Copyright (C) 2009 Openbravo SL 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.test.modularity;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.openbravo.erpCommon.utility.Utility;
+
+/**
+ * This test case checks the properties merge functionality used when 
upgrading core
+ * 
+ */
+public class MergePropertiesTest extends TestCase {
+  private static String ORIGINAL_FILE = "test-orig.properties";
+  private static String NEW_FILE = "test-new.properties";
+
+  // crates original properties file to be tested later
+  public void testCreateOriginalFile1() throws FileNotFoundException, 
IOException {
+    Properties prop = new Properties();
+    prop.setProperty("test1", "value1.custom");
+    prop.setProperty("test2", "value2.custom");
+    prop.store(new FileOutputStream(ORIGINAL_FILE), "Original properties 
file");
+  }
+
+  // creates new properties file to be tested later
+  public void testCreateNewFile1() throws FileNotFoundException, IOException {
+    Properties prop = new Properties();
+    prop.setProperty("test1", "value1.default");
+    prop.setProperty("test2", "value2.default");
+    prop.setProperty("test3", "value3.default");
+    prop.store(new FileOutputStream(NEW_FILE), "New properties file");
+  }
+
+  // merges previously created files and checks expected values
+  public void testMerge() throws FileNotFoundException, IOException {
+    boolean modified = Utility.mergeOpenbravoProperties(ORIGINAL_FILE, 
NEW_FILE);
+    assertTrue("File has not been modified while it should be", modified);
+
+    Properties prop = new Properties();
+    prop.load(new FileInputStream(ORIGINAL_FILE));
+    assertTrue("Not correctly merged test1 property", 
prop.getProperty("test1").equals(
+        "value1.custom"));
+    assertTrue("Not correctly merged test2 property", 
prop.getProperty("test2").equals(
+        "value2.custom"));
+    assertTrue("Not correctly merged test3 property", 
prop.getProperty("test3").equals(
+        "value3.default"));
+  }
+
+  // deletes testing files
+  public void testDeleteFiles1() {
+    assertTrue("couldn't delete " + ORIGINAL_FILE, new 
File(ORIGINAL_FILE).delete());
+    assertTrue("couldn't delete " + ORIGINAL_FILE, new 
File(NEW_FILE).delete());
+  }
+
+  public void testCreateOriginalFile2() throws FileNotFoundException, 
IOException {
+    Properties prop = new Properties();
+    prop.setProperty("test1", "value1.custom");
+    prop.setProperty("test2", "value2.custom");
+    prop.store(new FileOutputStream(ORIGINAL_FILE), "Original properties 
file");
+  }
+
+  // creates new properties file to be tested later
+  public void testCreateNewFile2() throws FileNotFoundException, IOException {
+    Properties prop = new Properties();
+    prop.setProperty("test1", "value1.default");
+    prop.setProperty("test2", "value2.default");
+    prop.store(new FileOutputStream(NEW_FILE), "New properties file");
+  }
+
+  // merges previously created files, in this case there should not be 
modifications
+  public void testMerge2() throws FileNotFoundException, IOException {
+    boolean modified = Utility.mergeOpenbravoProperties(ORIGINAL_FILE, 
NEW_FILE);
+    assertFalse("File has been modified while it shouldn't be", modified);
+
+    Properties prop = new Properties();
+    prop.load(new FileInputStream(ORIGINAL_FILE));
+    assertTrue("Not correctly merged test1 property", 
prop.getProperty("test1").equals(
+        "value1.custom"));
+    assertTrue("Not correctly merged test2 property", 
prop.getProperty("test2").equals(
+        "value2.custom"));
+
+  }
+
+  // deletes testing files
+  public void testDeleteFiles2() {
+    assertTrue("couldn't delete " + ORIGINAL_FILE, new 
File(ORIGINAL_FILE).delete());
+    assertTrue("couldn't delete " + ORIGINAL_FILE, new 
File(NEW_FILE).delete());
+  }
+}
diff --git a/src/org/openbravo/erpCommon/modules/ImportModule.java 
b/src/org/openbravo/erpCommon/modules/ImportModule.java
--- a/src/org/openbravo/erpCommon/modules/ImportModule.java
+++ b/src/org/openbravo/erpCommon/modules/ImportModule.java
@@ -282,6 +282,10 @@
 
           installModule(file, moduleToInstallID);
 
+          if (moduleToInstallID.equals("0"))
+            Utility.mergeOpenbravoProperties(obDir + 
"/config/Openbravo.properties", obDir
+                + "/config/Openbravo.properties.template");
+
           final Vector<DynaBean> allModules = new Vector<DynaBean>(); // all
           // modules
           // include
@@ -388,6 +392,10 @@
             getModulesFromObx(dynMod, dynDep, dynDBPrefix, new 
ByteArrayInputStream(getMod));
             insertDynaModulesInDB(dynMod, dynDep, dynDBPrefix);
 
+            if (modulesToUpdate[i].getModuleID().equals("0"))
+              Utility.mergeOpenbravoProperties(obDir + 
"/config/Openbravo.properties", obDir
+                  + "/config/Openbravo.properties.template");
+
             // Entries for .classpath should be there, do not try to
             // insert them
           } catch (final Exception e) {
@@ -1133,4 +1141,5 @@
       super(msg);
     }
   }
+
 }
diff --git a/src/org/openbravo/erpCommon/utility/Utility.java 
b/src/org/openbravo/erpCommon/utility/Utility.java
--- a/src/org/openbravo/erpCommon/utility/Utility.java
+++ b/src/org/openbravo/erpCommon/utility/Utility.java
@@ -23,6 +23,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
@@ -41,6 +42,7 @@
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -2183,4 +2185,45 @@
     }
   }
 
+  /**
+   * When updating core it is necessary to update Openbravo properties 
maintaining already assigned
+   * properties. Thus properties in original file are preserved but the new 
ones in
+   * Openbravo.properties.template file are added with the default value.
+   * 
+   * @throws IOException
+   * @throws FileNotFoundException
+   * @return false in case no changes where needed, true in case the merge 
includes some changes and
+   *         the original file is modified
+   */
+  public static boolean mergeOpenbravoProperties(String originalFile, String 
newFile)
+      throws FileNotFoundException, IOException {
+    Properties origOBProperties = new Properties();
+    Properties newOBProperties = new Properties();
+    boolean modified = false;
+
+    // load both files
+    origOBProperties.load(new FileInputStream(originalFile));
+    newOBProperties.load(new FileInputStream(newFile));
+
+    Enumeration<?> newProps = newOBProperties.propertyNames();
+    while (newProps.hasMoreElements()) {
+      String propName = (String) newProps.nextElement();
+      String origValue = origOBProperties.getProperty(propName);
+
+      // try to get original value for new property, if it does not exist add 
it to original
+      // properties with its default value
+      if (origValue == null) {
+        String newValue = newOBProperties.getProperty(propName);
+        origOBProperties.setProperty(propName, newValue);
+        modified = true;
+      }
+    }
+
+    // save original file only in case it has modifications
+    if (modified) {
+      origOBProperties
+          .store(new FileOutputStream(originalFile), "Automatically updated 
properties");
+    }
+    return modified;
+  }
 }

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to