DecimalColumnFormatter implements ColumnFormatter { @Override public String format(Object t, Method method, String columnFormat) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Object column = method.invoke(t); if (column == null) { return ""; } if (columnFormat != null && !"".equals(columnFormat)) { DecimalFormatLocal dfLocal = dfMap.get(columnFormat); if (dfLocal == null) { dfLocal = new DecimalFormatLocal(columnFormat); dfMap.put(columnFormat, dfLocal); } return dfLocal.get().format(column); } else { return BigDecimal.class.cast(column).toPlainString(); } }  @Override String format(Object t, Method method, String columnFormat);  }
@Test public void testFormat01() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal01(new BigDecimal(1000000)); Method method = stub.getClass().getMethod("getDecimal01"); String columnFormat = null; String result = decimalColumnFormatter.format(stub, method, columnFormat); assertNotNull(result); assertEquals("1000000", result); }
@Test public void testFormat02() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal02(new BigDecimal(1000000)); Method method = stub.getClass().getMethod("getDecimal02"); String columnFormat = "\\##,###,###.00"; String result = decimalColumnFormatter.format(stub, method, columnFormat); assertNotNull(result); assertEquals("\\1,000,000.00", result); }
@Test public void testFormat03() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal03(new BigDecimal(1000000)); Method method = stub.getClass().getDeclaredMethod("getDecimal03"); String columnFormat = "\\##,###,###.00"; try { decimalColumnFormatter.format(stub, method, columnFormat); fail("IllegalAccessExceptionが発生しませんでした。"); } catch (IllegalAccessException e) { assertTrue(e instanceof IllegalAccessException); } }
@Test public void testFormat04() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal04(new BigDecimal(1000000)); Method method = stub.getClass().getMethod("getDecimal04"); String columnFormat = "\\##,###,###.00"; try { decimalColumnFormatter.format(stub, method, columnFormat); fail("InvocationTargetExceptionが発生しませんでした。"); } catch (InvocationTargetException e) { assertTrue(e instanceof InvocationTargetException); } }
@Test public void testFormat05() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal05(new BigDecimal(1000000)); Method method = stub.getClass().getMethod("getDecimal05", new Class[] { BigDecimal.class }); String columnFormat = new String(); try { decimalColumnFormatter.format(stub, method, columnFormat); fail("IllegalArgumentExceptionが発生しませんでした。"); } catch (IllegalArgumentException e) { assertTrue(e instanceof IllegalArgumentException); } }
@Test public void testFormat06() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal06(null); Method method = stub.getClass().getMethod("getDecimal06"); String columnFormat = null; String result = decimalColumnFormatter.format(stub, method, columnFormat); assertNotNull(result); assertEquals("", result); }
@Test public void testFormat07() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal07(new BigDecimal(1000000)); Method method = stub.getClass().getMethod("getDecimal07"); String columnFormat = ""; String result = decimalColumnFormatter.format(stub, method, columnFormat); assertNotNull(result); assertEquals("1000000", result); }
@Test public void testFormat08() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal08(new BigDecimal(1000000)); Method method = stub.getClass().getMethod("getDecimal08"); String columnFormat = "\\##,###,###.00"; ConcurrentHashMap<String, DecimalFormatLocal> dfMap = new ConcurrentHashMap<String, DecimalFormatLocal>(); ReflectionTestUtils.setField(decimalColumnFormatter, "dfMap", dfMap); dfMap.clear(); DecimalFormatLocal dfl = Mockito.spy( new DecimalFormatLocal(columnFormat)); PowerMockito.whenNew(DecimalFormatLocal.class).withArguments( columnFormat).thenReturn(dfl); String result = decimalColumnFormatter.format(stub, method, columnFormat); assertNotNull(result); assertEquals("\\1,000,000.00", result); assertSame(dfMap, ReflectionTestUtils.getField(decimalColumnFormatter, "dfMap")); assertEquals(1, dfMap.size()); assertTrue(dfMap.containsKey(columnFormat)); DecimalFormatLocal dfMapValue = dfMap.get(columnFormat); assertNotNull(dfMapValue); assertEquals(columnFormat, ReflectionTestUtils.getField(dfMapValue, "pattern")); PowerMockito.verifyNew(DecimalFormatLocal.class).withArguments( columnFormat); }
@Test public void testFormat09() throws Exception { DecimalColumnFormatter decimalColumnFormatter = new DecimalColumnFormatter(); DecimalColumnFormatter_FileLineObjectStub01 stub = new DecimalColumnFormatter_FileLineObjectStub01(); stub.setDecimal09(new BigDecimal(1000000)); Method method = stub.getClass().getMethod("getDecimal09"); String columnFormat = "\\##,###,###.00"; ConcurrentHashMap<String, DecimalFormatLocal> dfMap = new ConcurrentHashMap<String, DecimalFormatLocal>(); DecimalFormatLocal dfMapValue = Mockito.spy( new DecimalFormatLocal(columnFormat)); dfMap.put(columnFormat, dfMapValue); ReflectionTestUtils.setField(decimalColumnFormatter, "dfMap", dfMap); PowerMockito.whenNew(DecimalFormatLocal.class).withArguments( columnFormat).thenReturn(dfMapValue); String result = decimalColumnFormatter.format(stub, method, columnFormat); assertNotNull(result); assertEquals("\\1,000,000.00", result); assertSame(dfMap, ReflectionTestUtils.getField(decimalColumnFormatter, "dfMap")); assertEquals(1, dfMap.size()); assertTrue(dfMap.containsKey(columnFormat)); DecimalFormatLocal formatLocal = dfMap.get(columnFormat); assertSame(dfMapValue, formatLocal); PowerMockito.verifyNew(DecimalFormatLocal.class, Mockito.never()) .withArguments(columnFormat); }