Skip to content

Commit fb1f217

Browse files
committed
Add test cases for apache#276
1 parent 1e2e20c commit fb1f217

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed

src/main/java/org/apache/commons/beanutils2/BeanUtilsBean.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected BeanUtilsBean initialValue() {
6060
/**
6161
* Logging for this instance
6262
*/
63-
private static final Log LOG = LogFactory.getLog(BeanUtilsBean.class);
63+
private static Log LOG = LogFactory.getLog(BeanUtilsBean.class);
6464

6565
/** A reference to Throwable's initCause method, or null if it's not there in this JVM */
6666
private static final Method INIT_CAUSE_METHOD = getInitCauseMethod();
@@ -172,6 +172,12 @@ public BeanUtilsBean(final ConvertUtilsBean convertUtilsBean,
172172
this.propertyUtilsBean = propertyUtilsBean;
173173
}
174174

175+
public BeanUtilsBean(final ConvertUtilsBean convertUtilsBean, final PropertyUtilsBean propertyUtilsBean, final Log log) {
176+
this.convertUtilsBean = convertUtilsBean;
177+
this.propertyUtilsBean = propertyUtilsBean;
178+
LOG = log;
179+
}
180+
175181
/**
176182
* <p>Clone a bean based on the available property getters and setters,
177183
* even if the bean class itself does not implement Cloneable.</p>

src/test/java/org/apache/commons/beanutils2/BeanUtilsBeanTestCase.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
2121

2222
import java.lang.reflect.InvocationTargetException;
23+
import java.util.ArrayList;
2324
import java.util.Calendar;
2425
import java.util.HashMap;
2526
import java.util.Locale;
@@ -31,6 +32,8 @@
3132
import junit.framework.Test;
3233
import junit.framework.TestCase;
3334
import junit.framework.TestSuite;
35+
import org.apache.commons.logging.Log;
36+
import org.apache.commons.logging.LogFactory;
3437

3538
/**
3639
* <p>
@@ -1431,10 +1434,152 @@ public void testSetPropertyWriteOnly() throws Exception {
14311434

14321435
}
14331436

1437+
public void testCopyPropertyLogTrace() throws Exception {
1438+
LogSpy logSpy = new LogSpy(LogFactory.getLog(BeanUtilsBean.class));
1439+
final BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), new PropertyUtilsBean(), logSpy);
1440+
1441+
beanUtils.copyProperty(bean, "writeOnlyProperty", "New value");
1442+
1443+
assertEquals(" copyProperty(" + bean.toString() + ", writeOnlyProperty, New value)", logSpy.getTraceMessages().get(0));
1444+
}
1445+
1446+
public void testCopyPropertyLogTraceWithNull() throws Exception {
1447+
LogSpy logSpy = new LogSpy(LogFactory.getLog(BeanUtilsBean.class));
1448+
final BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), new PropertyUtilsBean(), logSpy);
1449+
1450+
beanUtils.copyProperty(bean, "writeOnlyProperty", null);
1451+
1452+
assertEquals(" copyProperty(" + bean.toString() + ", writeOnlyProperty, <NULL>)", logSpy.getTraceMessages().get(0));
1453+
}
1454+
1455+
public void testCopyPropertyLogTraceWhenBeanOverRidesToString() throws Exception {
1456+
final TestBean2 bean = new TestBean2();
1457+
1458+
LogSpy logSpy = new LogSpy(LogFactory.getLog(BeanUtilsBean.class));
1459+
final BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), new PropertyUtilsBean(), logSpy);
1460+
1461+
beanUtils.copyProperty(bean, "writeOnlyProperty", "New value");
1462+
1463+
assertEquals(" copyProperty(" + bean.toString() + ", writeOnlyProperty, has been set)", logSpy.getTraceMessages().get(0));
1464+
}
1465+
14341466
/**
14351467
* Throw an exception with the specified message.
14361468
*/
14371469
private void throwException(final String msg) throws Throwable {
14381470
throw new Exception(msg);
14391471
}
1472+
1473+
public class LogSpy implements Log {
1474+
private final Log originalLog;
1475+
private final ArrayList<Object> traceMessages = new ArrayList<>();
1476+
1477+
public ArrayList<Object> getTraceMessages() {
1478+
return traceMessages;
1479+
}
1480+
1481+
public LogSpy(Log originalLog) {
1482+
this.originalLog = originalLog;
1483+
}
1484+
1485+
@Override
1486+
public void debug(Object message) {
1487+
originalLog.debug(message);
1488+
}
1489+
1490+
// Implement or delegate other methods in the Log interface
1491+
@Override
1492+
public void debug(Object message, Throwable t) {
1493+
originalLog.debug(message, t);
1494+
}
1495+
1496+
@Override
1497+
public void error(Object message) {
1498+
originalLog.error(message);
1499+
}
1500+
1501+
@Override
1502+
public void error(Object message, Throwable t) {
1503+
originalLog.error(message, t);
1504+
}
1505+
1506+
@Override
1507+
public void fatal(Object message) {
1508+
originalLog.fatal(message);
1509+
}
1510+
1511+
@Override
1512+
public void fatal(Object message, Throwable t) {
1513+
originalLog.fatal(message, t);
1514+
}
1515+
1516+
@Override
1517+
public void info(Object message) {
1518+
originalLog.info(message);
1519+
}
1520+
1521+
@Override
1522+
public void info(Object message, Throwable t) {
1523+
originalLog.info(message, t);
1524+
}
1525+
1526+
@Override
1527+
public boolean isDebugEnabled() {
1528+
return originalLog.isDebugEnabled();
1529+
}
1530+
1531+
@Override
1532+
public boolean isErrorEnabled() {
1533+
return originalLog.isErrorEnabled();
1534+
}
1535+
1536+
@Override
1537+
public boolean isFatalEnabled() {
1538+
return originalLog.isFatalEnabled();
1539+
}
1540+
1541+
@Override
1542+
public boolean isInfoEnabled() {
1543+
return originalLog.isInfoEnabled();
1544+
}
1545+
1546+
@Override
1547+
public boolean isTraceEnabled() {
1548+
return true;
1549+
// return originalLog.isTraceEnabled();
1550+
}
1551+
1552+
@Override
1553+
public boolean isWarnEnabled() {
1554+
return originalLog.isWarnEnabled();
1555+
}
1556+
1557+
@Override
1558+
public void trace(Object message) {
1559+
traceMessages.add(message);
1560+
originalLog.trace(message);
1561+
}
1562+
1563+
@Override
1564+
public void trace(Object message, Throwable t) {
1565+
originalLog.trace(message, t);
1566+
}
1567+
1568+
@Override
1569+
public void warn(Object message) {
1570+
originalLog.warn(message);
1571+
}
1572+
1573+
@Override
1574+
public void warn(Object message, Throwable t) {
1575+
originalLog.warn(message, t);
1576+
}
1577+
}
1578+
1579+
class TestBean2 extends TestBean {
1580+
@Override
1581+
public String toString() {
1582+
return super.toString();
1583+
}
1584+
}
14401585
}

0 commit comments

Comments
 (0)