Search in sources :

Example 1 with MTest

use of org.compiere.model.MTest in project adempiere by adempiere.

the class POTest method testTruncatedStrings.

/**
	 * <li>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());
        //
        testPO.setName(bigString.substring(0, maxLength - 1));
        assertEquals("String was not truncated correctly (2)", maxLength - 1, testPO.getName().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 (3)", maxLength, resultString.length());
        //
        testPO.setName(bigString.substring(0, maxLength));
        assertEquals("String was not truncated correctly (4)", maxLength, testPO.getName().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 (5)", maxLength, resultString.length());
        //
        testPO.setName(bigString);
        assertEquals("String was not truncated correctly (6)", maxLength, testPO.getName().length());
    }
}
Also used : POInfo(org.compiere.model.POInfo) MTest(org.compiere.model.MTest)

Example 2 with MTest

use of org.compiere.model.MTest in project adempiere by adempiere.

the class TrxTest method testRunTrxRunnable.

/**
	 * Test {@link Trx#run(TrxRunnable)} and {@link Trx#run(String, TrxRunnable)} methods
	 */
public void testRunTrxRunnable() throws Exception {
    //
    // Create test outside trx - success
    m_id2 = -1;
    Trx.run(new TrxRunnable() {

        public void run(String trxName) {
            m_id2 = createTest(trxName).get_ID();
        }
    });
    assertTestExists(m_id2, true, null);
    new MTest(getCtx(), m_id2, null).deleteEx(true);
    //
    // Create test outside trx - fail
    m_id2 = -1;
    try {
        Trx.run(new TrxRunnable() {

            public void run(String trxName) {
                m_id2 = createTest(trxName).get_ID();
                throw new AdempiereException("FORCE");
            }
        });
        //
        assertTrue("Should not happen because previous code is throwing exception", false);
    } catch (AdempiereException e) {
    }
    assertTestExists(m_id2, false, null);
    //
    // Create test1
    String trxName = getTrxName();
    MTest test1 = createTest(trxName);
    //
    // Fail creating test2
    m_id2 = -1;
    try {
        Trx.run(trxName, new TrxRunnable() {

            public void run(String trxName) {
                m_id2 = createTest(trxName).get_ID();
                throw new AdempiereException("FORCE");
            }
        });
        //
        assertTrue("Should not happen because previous code is throwing exception", false);
    } catch (AdempiereException e) {
    }
    assertTestExists(m_id2, false, trxName);
    assertTestExists(test1.get_ID(), true, trxName);
    //
    // Success creating test2
    m_id2 = -1;
    Trx.run(trxName, new TrxRunnable() {

        public void run(String trxName) {
            m_id2 = createTest(trxName).get_ID();
        }
    });
    assertTestExists(m_id2, true, trxName);
    assertTestExists(test1.get_ID(), true, trxName);
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) TrxRunnable(org.compiere.util.TrxRunnable) MTest(org.compiere.model.MTest)

Example 3 with MTest

use of org.compiere.model.MTest in project adempiere by adempiere.

the class POTest method test_Changed.

/**
	 * Tests the following methods:
	 * <ul> 
	 * <li>{@link org.compiere.model.PO#is_Changed()}
	 * <li>{@link org.compiere.model.PO#is_ValueChanged(String)}
	 * </ul> 
	 * Applies to following bugs:
	 * <ul>
	 * <li>[ 1704828 ] PO.is_Changed() and PO.is_ValueChanged are not consistent
	 * </ul>
	 * @throws Exception
	 */
public void test_Changed() throws Exception {
    String[] testStrings = new String[] { "a", "test" };
    // Create the test PO and save
    MTest testPO = new MTest(getCtx(), getClass().getName(), 1);
    testPO.set_TrxName(getTrxName());
    for (String str : testStrings) {
        testPO.setHelp(str);
        testPO.saveEx();
        String originalString = testPO.getHelp();
        String info = "testString=[" + str + "]" + ", originalString=[" + originalString + "]";
        // Initial asserts (nothing changed)
        assertFalse(info, testPO.is_ValueChanged(MTest.COLUMNNAME_Help));
        assertFalse(info, testPO.is_Changed());
        // Set the same name
        testPO.setHelp(originalString);
        assertFalse(info, testPO.is_ValueChanged(MTest.COLUMNNAME_Help));
        assertFalse(info, testPO.is_Changed());
        // Set a new name
        testPO.setHelp(originalString + "-changed");
        assertTrue(info, testPO.is_ValueChanged(MTest.COLUMNNAME_Help));
        assertTrue(info, testPO.is_Changed());
        // Set the original name back
        testPO.setHelp(originalString);
        assertFalse(info, testPO.is_ValueChanged(MTest.COLUMNNAME_Help));
        assertFalse(info, testPO.is_Changed());
    }
    // Finally, delete the testPO
    testPO.delete(true, getTrxName());
}
Also used : MTest(org.compiere.model.MTest)

Example 4 with MTest

use of org.compiere.model.MTest in project adempiere by adempiere.

the class TrxTest method createTest.

private final MTest createTest(String trxName) {
    MTest test = new MTest(getCtx(), "test-" + getClass(), 10);
    test.set_TrxName(trxName);
    test.saveEx();
    return test;
}
Also used : MTest(org.compiere.model.MTest)

Aggregations

MTest (org.compiere.model.MTest)4 AdempiereException (org.adempiere.exceptions.AdempiereException)1 POInfo (org.compiere.model.POInfo)1 TrxRunnable (org.compiere.util.TrxRunnable)1