Util { public static void closeSafely(Object closable, String msg) { if (closable == null) { return; } try { log.info("Going to close: " + msg); closeInvoker.execute(closable); } catch (Exception e) { log.warn(msg + ".close() was not successful!", e); } } private  Util(); static String generateUUID(); static String generateDbUUID(); static Set<T> asSet(T... values); static Date tomorrow(); static Date yesterday(); static Date midnight(); static Date addDaysToDt(int dayField); static Date addMinutesToDt(int minuteField); static Date toDate(String dt); static T assertNotNull(T value, String message); static boolean equals(String str, String str1); static void closeSafely(Object closable, String msg); static String capitalize(String str); static long generateUniqueLong(); static String toBase64(byte[] data); static SimpleDateFormat getUTCDateFormat(); static String readFile(InputStream is); static String hash(String password); static String toJson(Object anObject); static T fromJson(String json, Class<T> clazz); @SuppressWarnings("rawtypes") static String getClassName(Class c); static String getClassName(String fullClassName); static String reverseEndian(String in); static String transformUuid(String uuid); static List<String> getPossibleUuids(String... ids); static boolean isUuid(String uuid); static String collectionToString(Collection c); static boolean collectionsAreEqual(Collection<T> c1, Collection<T> c2); static boolean collectionsAreEqual(Collection<T> c1, Collection<T> c2, Comparator<T> comp); static String getHostname(); static final String UTC_STR; }
@Test public void closeSafelyShouldAcceptNull() { TestClosable closable = mock(TestClosable.class); Util.closeSafely(null, "passing in null"); verify(closable, never()).close(); verify(mockapp, never()).doAppend(null); }
@Test public void closeSafely() { ArgumentCaptor<LoggingEvent> message = ArgumentCaptor.forClass(LoggingEvent.class); TestClosable closable = mock(TestClosable.class); Util.closeSafely(closable, "objectname"); verify(closable, atLeastOnce()).close(); verify(mockapp, atLeastOnce()).doAppend(message.capture()); assertEquals("Going to close: objectname", message.getValue().getMessage()); }
@Test public void closeSafelyWithException() { ArgumentCaptor<LoggingEvent> message = ArgumentCaptor.forClass(LoggingEvent.class); TestClosable closable = mock(TestClosable.class); doThrow(new RuntimeException("booyah")).when(closable).close(); Util.closeSafely(closable, "objectname"); verify(closable, atLeastOnce()).close(); verify(mockapp, atLeastOnce()).doAppend(message.capture()); assertEquals("objectname.close() was not successful!", message.getValue().getMessage()); }