|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
21 | 21 |
|
22 | 22 | import java.lang.reflect.InvocationTargetException;
|
| 23 | +import java.util.ArrayList; |
23 | 24 | import java.util.Calendar;
|
24 | 25 | import java.util.HashMap;
|
25 | 26 | import java.util.Locale;
|
|
31 | 32 | import junit.framework.Test;
|
32 | 33 | import junit.framework.TestCase;
|
33 | 34 | import junit.framework.TestSuite;
|
| 35 | +import org.apache.commons.logging.Log; |
| 36 | +import org.apache.commons.logging.LogFactory; |
34 | 37 |
|
35 | 38 | /**
|
36 | 39 | * <p>
|
@@ -1431,10 +1434,152 @@ public void testSetPropertyWriteOnly() throws Exception {
|
1431 | 1434 |
|
1432 | 1435 | }
|
1433 | 1436 |
|
| 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 | + |
1434 | 1466 | /**
|
1435 | 1467 | * Throw an exception with the specified message.
|
1436 | 1468 | */
|
1437 | 1469 | private void throwException(final String msg) throws Throwable {
|
1438 | 1470 | throw new Exception(msg);
|
1439 | 1471 | }
|
| 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 | + } |
1440 | 1585 | }
|
0 commit comments