diff --git a/base/src/org/compiere/model/PO.java b/base/src/org/compiere/model/PO.java index e55daa5f94..418e73d358 100644 --- a/base/src/org/compiere/model/PO.java +++ b/base/src/org/compiere/model/PO.java @@ -69,6 +69,7 @@ import org.w3c.dom.Element; *
  • FR [ 1675490 ] ModelValidator on modelChange after events *
  • BF [ 1704828 ] PO.is_Changed() and PO.is_ValueChanged are not consistent *
  • FR [ 1720995 ] Add PO.saveEx() and PO.deleteEx() methods + *
  • BF [ 1990856 ] PO.set_Value* : truncate string more than needed */ public abstract class PO implements Serializable, Comparator, Evaluatee @@ -765,10 +766,11 @@ public abstract class PO if (stringValue.length() > length && length > 0) { log.warning(ColumnName + " - Value too long - truncated to length=" + length); - m_newValues[index] = stringValue.substring(0,length-1); + m_newValues[index] = stringValue.substring(0,length); } } - log.finest(ColumnName + " = " + m_newValues[index]); + if (CLogMgt.isLevelFinest()) + log.finest(ColumnName + " = " + m_newValues[index] + " (OldValue="+m_oldValues[index]+")"); } set_Keys (ColumnName, m_newValues[index]); return true; @@ -841,7 +843,7 @@ public abstract class PO if (stringValue.length() > length && length > 0) { log.warning(ColumnName + " - Value too long - truncated to length=" + length); - m_newValues[index] = stringValue.substring(0,length-1); + m_newValues[index] = stringValue.substring(0,length); } } } @@ -1965,7 +1967,7 @@ public abstract class PO ValueNamePair err = CLogger.retrieveError(); if (err != null) msg = err.getName(); - if (msg == null) + if (msg == null || msg.length() == 0) msg = "SaveError"; throw new AdempiereException(msg); } @@ -2842,7 +2844,7 @@ public abstract class PO ValueNamePair err = CLogger.retrieveError(); if (err != null) msg = err.getName(); - if (msg == null) + if (msg == null || msg.length() == 0) msg = "DeleteError"; throw new AdempiereException(msg); } diff --git a/extend/src/test/functional/POTest.java b/extend/src/test/functional/POTest.java index fd0293d577..34ab31f974 100644 --- a/extend/src/test/functional/POTest.java +++ b/extend/src/test/functional/POTest.java @@ -1,13 +1,13 @@ package test.functional; import org.compiere.model.MTest; +import org.compiere.model.POInfo; import test.AdempiereTestCase; /** * Tests for {@link org.compiere.model.PO} class. - * @author Teo Sarca - * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL */ public class POTest extends AdempiereTestCase { @@ -58,4 +58,51 @@ public class POTest extends AdempiereTestCase // Finally, delete the testPO testPO.delete(true, getTrxName()); } + + + /** + *
  • BF [ 1990856 ] PO.set_Value* : truncate string more than needed + */ + public void testTruncatedStrings() { + // + // Creating a huge string for testing: + StringBuffer sb = new StringBuffer(); + for (int i = 1; i <= 1000; i++) { + sb.append("0123456789"); + } + String bigString = sb.toString(); + // + // Create the test PO: + MTest testPO = new MTest(getCtx(), getClass().getName(), 1); + testPO.set_TrxName(getTrxName()); + // + // Getting Max Length: + POInfo info = POInfo.getPOInfo(getCtx(), MTest.Table_ID); + int maxLength = info.getFieldLength(info.getColumnIndex(MTest.COLUMNNAME_Name)); + // + // Test with a string that has less then maxLength + { + testPO.set_ValueOfColumn(MTest.COLUMNNAME_Name, bigString.substring(0, maxLength - 1)); + String resultString = (String) testPO.get_Value(MTest.COLUMNNAME_Name); + assertEquals("String was not truncated correctly (1)", maxLength - 1, resultString.length()); + } + // + // Test with a string that has maxLength + { + testPO.set_ValueOfColumn(MTest.COLUMNNAME_Name, bigString.substring(0, maxLength)); + String resultString = (String) testPO.get_Value(MTest.COLUMNNAME_Name); + assertEquals("String was not truncated correctly (2)", maxLength, resultString.length()); + } + // + // Test with a string that has more than maxLength + { + testPO.set_ValueOfColumn(MTest.COLUMNNAME_Name, bigString); + String resultString = (String) testPO.get_Value(MTest.COLUMNNAME_Name); + assertEquals("String was not truncated correctly (3)", maxLength, resultString.length()); + } + // + // Finally, delete the testPO + testPO.delete(true, getTrxName()); + } + }