@Test public void testFormatting() throws Exception { doTestFormatting("", ""); doTestFormatting(" 00:00", ""); doTestFormatting(" 13:24", " 13:24"); doTestFormatting(" 00:39", " 00:39"); }
@Test public void testDateUtil() { testDate(now()); testDate(today()); testDate("T", today(), 0); testDate("N", now(), 100); testDate("T+30", DateUtil.addDays(today(), 30, false), 0); testDate("N+30", DateUtil.addDays(now(), 30, false), 100); testDate("T-4", DateUtil.addDays(today(), -4, false), 0); testDate("T-50s", new Date(today().getTime() - 50000), 0); testDate("N-50s", new Date(now().getTime() - 50000), 100); testDate("T-50h", new Date(today().getTime() - 50 * 60 * 60 * 1000), 0); testDate("T-50n", new Date(today().getTime() - 50 * 60 * 1000), 0); Date date = DateUtil.parseDate("19880302"); testDate(date.toString(), date, 0); }
@Test public void testCamelCase() { testCamelCase(" test of _camel_case "); testCamelCase("TEST OF CAMEL CASE"); testCamelCase("test_of_camel_case"); testCamelCase("test__of_ camel\t\ncase"); testCamelCase("test of camel case"); }
private void testSerializer(String typeProperty) { TestPerson obj = new TestPerson(); String s = JSONUtil.serialize(typeProperty, obj); print(s); TestPerson obj2 = (TestPerson) JSONUtil.deserialize(typeProperty, s); assertTrue(obj.equals(obj2)); List<TestPerson> list = new ArrayList<>(); list.add(obj); list.add(obj); s = JSONUtil.serialize(typeProperty, list); print(s); List<TestPerson> list2 = JSONUtil.deserializeList(typeProperty, s, TestPerson.class); assertEquals(list, list2); @SuppressWarnings("unchecked") List<TestPerson> list3 = (List<TestPerson>) JSONUtil.deserialize(typeProperty, s); assertEquals(list, list3); }
@Test public void testStripQuotes() { testStripQuotes(QT_NONE, QT_NONE); testStripQuotes(QT_NONE, QT_SINGLE); testStripQuotes(QT_NONE, QT_DOUBLE); testStripQuotes(QT_OTHER1, QT_OTHER1); testStripQuotes(QT_OTHER2, QT_OTHER2); testStripQuotes(QT_OTHER3, QT_OTHER3); }
@Test public void testEnquote() { testEnquote(QT_DOUBLE, QT_NONE, false); testEnquote(QT_SINGLE, QT_NONE, true); testEnquote(QT_DOUBLE, QT_DOUBLE, false); testEnquote(QT_SINGLE, QT_SINGLE, true); }
@Test public void testDefaultTimeZone() throws Exception { testDateFormat("EST", "13:04"); testDateFormat("GMT", "18:04"); testDateFormat("CST", "12:04"); }
@Test public void testColorUtil() { testColorUtil("darkorchid", "#9932CC"); testColorUtil("azure", "#F0FFFF"); }
@Test public void testSerializer() { JSONUtil.registerAlias("TestPerson", TestPerson.class); testSerializer(null); testSerializer("resourceType"); }
private Date today() { return DateUtil.stripTime(now()); }
private void testDate(Date date, boolean showTimezone, boolean ignoreTime) { String text = DateUtil.formatDate(date, showTimezone, ignoreTime); print(text); Date date2 = DateUtil.parseDate(text); String text2 = DateUtil.formatDate(date2, showTimezone, ignoreTime); assertEquals(text, text2); }
private void testDate(String value, Date expected, int threshold) { Date actual = DateUtil.parseDate(value); testDate(actual); long diff = Math.abs(expected.getTime() - actual.getTime()); assertTrue("Difference exceeded threshold " + diff + " (" + threshold + ")", diff <= threshold); }
@Test public void testQueryStringBuilder() { QueryStringBuilder sb = new QueryStringBuilder(); assertEquals(0, sb.length()); sb.append("q1", "value 1", "value 2"); List<String> list = new ArrayList<>(); list.add("value 1"); list.add("value 2"); list.add("value 3"); sb.append("q2", list); sb.append("q3", "single"); sb.append("q4<>", "escape+name"); String qs = sb.toString(); print(qs); assertEquals("q1=value+1,value+2&q2=value+1&q2=value+2&q2=value+3&q3=single&q4%3C%3E=escape%2Bname", qs); sb.clear(); assertTrue(sb.toString().isEmpty()); }