DateColumnFormatter implements ColumnFormatter { @Override public String format(Object t, Method method, String columnFormat) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Date column = (Date) method.invoke(t); if (column == null) { return ""; } String pattern = columnFormat; if (pattern == null || "".equals(pattern)) { pattern = "yyyyMMdd"; } DateFormatLocal dflocal = map.get(pattern); if (dflocal == null) { dflocal = new DateFormatLocal(pattern); map.put(pattern, dflocal); } DateFormat df = dflocal.get(); return df.format(column); }  @Override String format(Object t, Method method, String columnFormat);  }
@Test public void testFormat01() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = null; Map<String, DateFormatLocal> map = Mockito.spy( new ConcurrentHashMap<String, DateFormatLocal>()); ReflectionTestUtils.setField(columnFormatter, "map", map); DateFormatLocal dfl = new DateFormatLocal("yyyyMMdd"); PowerMockito.whenNew(DateFormatLocal.class).withArguments("yyyyMMdd") .thenReturn(dfl); String testResult = columnFormatter.format(stub, method, columnFormat); assertEquals("19700101", testResult); Mockito.verify(map).get("yyyyMMdd"); Mockito.verify(map).put(Mockito.eq("yyyyMMdd"), Mockito.any( DateFormatLocal.class)); }
@Test public void testFormat02() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = ""; Map<String, DateFormatLocal> map = Mockito.spy( new ConcurrentHashMap<String, DateFormatLocal>()); map.put("yyyyMMdd", new DateFormatLocal("yyyyMMdd")); ReflectionTestUtils.setField(columnFormatter, "map", map); DateFormatLocal dfl = new DateFormatLocal("yyyyMMdd"); PowerMockito.whenNew(DateFormatLocal.class).withArguments("yyyyMMdd") .thenReturn(dfl); String testResult = columnFormatter.format(stub, method, columnFormat); assertEquals("19700101", testResult); Mockito.verify(map).get("yyyyMMdd"); Mockito.verify(map).put(Mockito.anyString(), Mockito.any( DateFormatLocal.class)); PowerMockito.verifyNew(DateFormatLocal.class, Mockito.never()) .withArguments("yyyyMMdd"); }
@Test public void testFormat03() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = ""; Map<String, DateFormatLocal> map = Mockito.spy( new ConcurrentHashMap<String, DateFormatLocal>()); map.put("yyyy-MM-dd", new DateFormatLocal("yyyy-MM-dd")); ReflectionTestUtils.setField(columnFormatter, "map", map); DateFormatLocal dfl = new DateFormatLocal("yyyyMMdd"); PowerMockito.whenNew(DateFormatLocal.class).withArguments("yyyyMMdd") .thenReturn(dfl); String testResult = columnFormatter.format(stub, method, columnFormat); assertEquals("19700101", testResult); Mockito.verify(map).get("yyyyMMdd"); Mockito.verify(map).put(Mockito.eq("yyyyMMdd"), Mockito.any( DateFormatLocal.class)); Map<?, ?> getMap = (Map<?, ?>) ReflectionTestUtils.getField( columnFormatter, "map"); assertEquals(2, getMap.size()); PowerMockito.verifyNew(DateFormatLocal.class).withArguments("yyyyMMdd"); }
@Test public void testFormat04() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = "yyyy/MM/dd"; Map<String, DateFormatLocal> map = Mockito.spy( new ConcurrentHashMap<String, DateFormatLocal>()); ReflectionTestUtils.setField(columnFormatter, "map", map); DateFormatLocal dfl = new DateFormatLocal("yyyy/MM/dd"); PowerMockito.whenNew(DateFormatLocal.class).withArguments("yyyy/MM/dd") .thenReturn(dfl); String testResult = columnFormatter.format(stub, method, columnFormat); assertEquals("1970/01/01", testResult); Mockito.verify(map).get(columnFormat); Mockito.verify(map).put(Mockito.eq(columnFormat), Mockito.any( DateFormatLocal.class)); Map<?, ?> getMap = (Map<?, ?>) ReflectionTestUtils.getField( columnFormatter, "map"); assertEquals(1, getMap.size()); PowerMockito.verifyNew(DateFormatLocal.class).withArguments( "yyyy/MM/dd"); }
@Test public void testFormat05() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = "yyyy/MM/dd"; Map<String, DateFormatLocal> map = Mockito.spy( new ConcurrentHashMap<String, DateFormatLocal>()); map.put(columnFormat, new DateFormatLocal(columnFormat)); ReflectionTestUtils.setField(columnFormatter, "map", map); DateFormatLocal dfl = new DateFormatLocal("yyyy/MM/dd"); PowerMockito.whenNew(DateFormatLocal.class).withArguments("yyyy/MM/dd") .thenReturn(dfl); String testResult = columnFormatter.format(stub, method, columnFormat); assertEquals("1970/01/01", testResult); Mockito.verify(map).get(columnFormat); Mockito.verify(map).put(Mockito.anyString(), Mockito.any( DateFormatLocal.class)); PowerMockito.verifyNew(DateFormatLocal.class, Mockito.never()) .withArguments("yyyy/MM/dd"); }
@Test public void testFormat06() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = "yyyy/MM/dd"; Map<String, DateFormatLocal> map = Mockito.spy( new ConcurrentHashMap<String, DateFormatLocal>()); map.put("yyyy-MM-dd", new DateFormatLocal("yyyy-MM-dd")); ReflectionTestUtils.setField(columnFormatter, "map", map); DateFormatLocal dfl = new DateFormatLocal("yyyy/MM/dd"); PowerMockito.whenNew(DateFormatLocal.class).withArguments("yyyy/MM/dd") .thenReturn(dfl); String testResult = columnFormatter.format(stub, method, columnFormat); assertEquals("1970/01/01", testResult); Mockito.verify(map).get(columnFormat); Mockito.verify(map).put(Mockito.eq(columnFormat), Mockito.any( DateFormatLocal.class)); Map<?, ?> getMap = (Map<?, ?>) ReflectionTestUtils.getField( columnFormatter, "map"); assertEquals(2, getMap.size()); PowerMockito.verifyNew(DateFormatLocal.class).withArguments( "yyyy/MM/dd"); }
@Test public void testFormat07() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub02 stub = new DateColumnFormatter_Stub02(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getDeclaredMethod("getDate"); String columnFormat = "yyyy/MM/dd"; try { columnFormatter.format(stub, method, columnFormat); fail("IllegalAccessExceptionが発生しませんでした。失敗です。"); } catch (Exception e) { assertEquals(IllegalAccessException.class, e.getClass()); } }
@Test public void testFormat08() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub03 stub = new DateColumnFormatter_Stub03(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = "yyyy/MM/dd"; try { columnFormatter.format(stub, method, columnFormat); fail("InvocationTargetExceptionが発生しませんでした。失敗です。"); } catch (Exception e) { assertEquals(InvocationTargetException.class, e.getClass()); } }
@Test public void testFormat09() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub04 stub = new DateColumnFormatter_Stub04(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate", new Class[] { Date.class }); String columnFormat = "yyyy/MM/dd"; try { columnFormatter.format(stub, method, columnFormat); fail("IllegalArgumentExceptionが発生しませんでした。失敗です。"); } catch (Exception e) { assertEquals(IllegalArgumentException.class, e.getClass()); } }
@Test public void testFormat10() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = "AAA"; Map<String, DateFormatLocal> map = new ConcurrentHashMap<String, DateFormatLocal>(); ReflectionTestUtils.setField(columnFormatter, "map", map); try { columnFormatter.format(stub, method, columnFormat); fail("IllegalArgumentExceptionが発生しませんでした。失敗です。"); } catch (Exception e) { assertEquals(IllegalArgumentException.class, e.getClass()); } }
@Test public void testFormat11() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); Method method = stub.getClass().getMethod("getDate"); String columnFormat = "yyyy/MM/dd"; Map<String, DateFormatLocal> map = Mockito.spy( new ConcurrentHashMap<String, DateFormatLocal>()); ReflectionTestUtils.setField(columnFormatter, "map", map); DateFormatLocal dfl = new DateFormatLocal("yyyy/MM/dd"); PowerMockito.whenNew(DateFormatLocal.class).withArguments("yyyy/MM/dd") .thenReturn(dfl); String testResult = columnFormatter.format(stub, method, columnFormat); assertEquals("", testResult); Mockito.verify(map, Mockito.never()).get(Mockito.anyString()); Mockito.verify(map, Mockito.never()).put(Mockito.anyString(), Mockito .any(DateFormatLocal.class)); PowerMockito.verifyNew(DateFormatLocal.class, Mockito.never()) .withArguments("yyyy/MM/dd"); }
@Test public void testFormat12() throws Exception { ColumnFormatter columnFormatter = new DateColumnFormatter(); DateColumnFormatter_Stub01 stub = new DateColumnFormatter_Stub01(); ReflectionTestUtils.setField(stub, "date", new Date(0)); Method method = stub.getClass().getMethod("getDate"); String columnFormat = "yyyy/MM/dd"; Map<String, DateFormatLocal> map = new ConcurrentHashMap<String, DateFormatLocal>(); ReflectionTestUtils.setField(columnFormatter, "map", map); try { columnFormatter.format(null, method, columnFormat); fail("NullPointerExceptionが発生しませんでした。失敗です。"); } catch (Exception e) { assertEquals(NullPointerException.class, e.getClass()); } }