BucketPath { public static String escapeString(String in, Map<String, String> headers) { return escapeString(in, headers, false, 0, 0); }  @VisibleForTesting @Deprecated static boolean containsTag(String in); @VisibleForTesting @Deprecated static String expandShorthand(char c); @VisibleForTesting @Deprecated static String replaceShorthand(char c, Map<String, String> headers); @VisibleForTesting @Deprecated static String replaceShorthand(char c, Map<String, String> headers,
      boolean needRounding, int unit, int roundDown); @VisibleForTesting @Deprecated static String replaceShorthand(char c, Map<String, String> headers,
      TimeZone timeZone, boolean needRounding, int unit, int roundDown,
      boolean useLocalTimestamp); static String escapeString(String in, Map<String, String> headers); static String escapeString(String in, Map<String, String> headers,
      boolean needRounding, int unit, int roundDown); static String escapeString(String in, Map<String, String> headers,
      TimeZone timeZone, boolean needRounding, int unit, int roundDown,
      boolean useLocalTimeStamp); @VisibleForTesting @Deprecated static Map<String, String> getEscapeMapping(String in,
      Map<String, String> headers); @VisibleForTesting @Deprecated static Map<String, String> getEscapeMapping(String in,
      Map<String, String> headers, boolean needRounding,
      int unit, int roundDown); @VisibleForTesting static void setClock(Clock clk); @VisibleForTesting static Clock getClock(); static final String TAG_REGEX; static final Pattern tagPattern; }
@Test public void testDateFormatCache() { TimeZone utcTimeZone = TimeZone.getTimeZone("UTC"); String test = "%c"; BucketPath.escapeString( test, headers, utcTimeZone, false, Calendar.HOUR_OF_DAY, 12, false); String escapedString = BucketPath.escapeString( test, headers, false, Calendar.HOUR_OF_DAY, 12); System.out.println("Escaped String: " + escapedString); SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy"); Date d = new Date(cal.getTimeInMillis()); String expectedString = format.format(d); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testDateFormatHours() { String test = "%c"; String escapedString = BucketPath.escapeString( test, headers, true, Calendar.HOUR_OF_DAY, 12); System.out.println("Escaped String: " + escapedString); Calendar cal2 = createCalendar(2012, 5, 23, 12, 0, 0, 0, null); SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy"); Date d = new Date(cal2.getTimeInMillis()); String expectedString = format.format(d); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testDateFormatHoursTimeZone() { String test = "%c"; String escapedString = BucketPath.escapeString( test, headersWithTimeZone, CUSTOM_TIMEZONE, true, Calendar.HOUR_OF_DAY, 12, false); System.out.println("Escaped String: " + escapedString); Calendar cal2 = createCalendar(2012, 5, 23, 12, 0, 0, 0, CUSTOM_TIMEZONE); SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy"); format.setTimeZone(CUSTOM_TIMEZONE); Date d = new Date(cal2.getTimeInMillis()); String expectedString = format.format(d); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testDateFormatMinutes() { String test = "%s"; String escapedString = BucketPath.escapeString( test, headers, true, Calendar.MINUTE, 5); System.out.println("Escaped String: " + escapedString); Calendar cal2 = createCalendar(2012, 5, 23, 13, 45, 0, 0, null); String expectedString = String.valueOf(cal2.getTimeInMillis() / 1000); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testDateFormatMinutesTimeZone() { String test = "%s"; String escapedString = BucketPath.escapeString( test, headersWithTimeZone, CUSTOM_TIMEZONE, true, Calendar.MINUTE, 5, false); System.out.println("Escaped String: " + escapedString); Calendar cal2 = createCalendar(2012, 5, 23, 13, 45, 0, 0, CUSTOM_TIMEZONE); String expectedString = String.valueOf(cal2.getTimeInMillis() / 1000); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testDateFormatSeconds() { String test = "%s"; String escapedString = BucketPath.escapeString( test, headers, true, Calendar.SECOND, 5); System.out.println("Escaped String: " + escapedString); Calendar cal2 = createCalendar(2012, 5, 23, 13, 46, 30, 0, null); String expectedString = String.valueOf(cal2.getTimeInMillis() / 1000); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testDateFormatSecondsTimeZone() { String test = "%s"; String escapedString = BucketPath.escapeString( test, headersWithTimeZone, CUSTOM_TIMEZONE, true, Calendar.SECOND, 5, false); System.out.println("Escaped String: " + escapedString); Calendar cal2 = createCalendar(2012, 5, 23, 13, 46, 30, 0, CUSTOM_TIMEZONE); String expectedString = String.valueOf(cal2.getTimeInMillis() / 1000); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testNoRounding() { String test = "%c"; String escapedString = BucketPath.escapeString( test, headers, false, Calendar.HOUR_OF_DAY, 12); System.out.println("Escaped String: " + escapedString); SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy"); Date d = new Date(cal.getTimeInMillis()); String expectedString = format.format(d); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testNoPadding() { Calendar calender; Map<String, String> calender_timestamp; calender = Calendar.getInstance(); calender.set(2014, (5 - 1), 3, 13, 46, 33); calender_timestamp = new HashMap<String, String>(); calender_timestamp.put("timestamp", String.valueOf(calender.getTimeInMillis())); SimpleDateFormat format = new SimpleDateFormat("M-d"); String test = "%n-%e"; String escapedString = BucketPath.escapeString( test, calender_timestamp, false, Calendar.HOUR_OF_DAY, 12); Date d = new Date(calender.getTimeInMillis()); String expectedString = format.format(d); calender.set(2014, (11 - 1), 13, 13, 46, 33); calender_timestamp.put("timestamp", String.valueOf(calender.getTimeInMillis())); escapedString += " " + BucketPath.escapeString( test, calender_timestamp, false, Calendar.HOUR_OF_DAY, 12); System.out.println("Escaped String: " + escapedString); d = new Date(calender.getTimeInMillis()); expectedString += " " + format.format(d); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testDateFormatTimeZone() { TimeZone utcTimeZone = TimeZone.getTimeZone("UTC"); String test = "%c"; String escapedString = BucketPath.escapeString( test, headers, utcTimeZone, false, Calendar.HOUR_OF_DAY, 12, false); System.out.println("Escaped String: " + escapedString); SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy"); format.setTimeZone(utcTimeZone); Date d = new Date(cal.getTimeInMillis()); String expectedString = format.format(d); System.out.println("Expected String: " + expectedString); Assert.assertEquals(expectedString, escapedString); }
@Test public void testStaticEscapeStrings() { Map<String, String> staticStrings; staticStrings = new HashMap<>(); try { InetAddress addr = InetAddress.getLocalHost(); staticStrings.put("localhost", addr.getHostName()); staticStrings.put("IP", addr.getHostAddress()); staticStrings.put("FQDN", addr.getCanonicalHostName()); } catch (UnknownHostException e) { Assert.fail("Test failed due to UnkownHostException"); } TimeZone utcTimeZone = TimeZone.getTimeZone("UTC"); String filePath = "%[localhost]/%[IP]/%[FQDN]"; String realPath = BucketPath.escapeString(filePath, headers, utcTimeZone, false, Calendar.HOUR_OF_DAY, 12, false); String[] args = realPath.split("\\/"); Assert.assertEquals(args[0],staticStrings.get("localhost")); Assert.assertEquals(args[1],staticStrings.get("IP")); Assert.assertEquals(args[2],staticStrings.get("FQDN")); StringBuilder s = new StringBuilder(); s.append("Expected String: ").append(staticStrings.get("localhost")); s.append("/").append(staticStrings.get("IP")).append("/"); s.append(staticStrings.get("FQDN")); System.out.println(s); System.out.println("Escaped String: " + realPath ); }
@Test (expected = RuntimeException.class) public void testStaticEscapeStringsNoKey() { Map<String, String> staticStrings; staticStrings = new HashMap<>(); TimeZone utcTimeZone = TimeZone.getTimeZone("UTC"); String filePath = "%[abcdefg]/%[IP]/%[FQDN]"; String realPath = BucketPath.escapeString(filePath, headers, utcTimeZone, false, Calendar.HOUR_OF_DAY, 12, false); }