public class ApacheCombinedFormatLoggerTest {
  LogRecordingHandler logRecords = new LogRecordingHandler();
  Instant currentTime = LocalDateTime.of(2012, 6, 27, 12, 4, 0).toInstant(ZoneOffset.of("-05:00"));
  ApacheCombinedFormatLogger apacheCommonLogger =
      new ApacheCombinedFormatLogger(
          anonymousLogger(logRecords), Clock.fixed(currentTime, ZoneId.of("GMT+01:00")), Locale.US);

  Request request = new Request().protocol("HTTP/1.1").remoteIp("192.168.0.1");
  Response response = new Response();

  @Test
  public void logsRequestsServedInApacheCombinedLogFormat() throws Exception {
    request
        .method(GET)
        .uri("/products?keyword=dogs")
        .addHeader(HeaderNames.REFERER, "http://lama/wool")
        .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)");

    apacheCommonLogger.handle(request, response);
    response.status(OK).body("a response with a size of 28").done();

    response.await();
    logRecords.assertEntries(
        contains(
            "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\""));
  }

  @Test
  public void logsEmptyStringWhenNoUserAgentInRequest() throws Exception {
    request
        .method(GET)
        .uri("/products?keyword=dogs")
        .addHeader(HeaderNames.REFERER, "http://lama/wool");

    apacheCommonLogger.handle(request, response);
    response.status(OK).body("a response with a size of 28").done();

    response.await();
    logRecords.assertEntries(
        contains(
            "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"\""));
  }

  @Test
  public void logsEmptyStringWhenNoRefererInRequest() throws Exception {
    request
        .method(GET)
        .uri("/products?keyword=dogs")
        .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)");

    apacheCommonLogger.handle(request, response);
    response.status(OK).body("a response with a size of 28").done();

    response.await();
    logRecords.assertEntries(
        contains(
            "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\""));
  }
}
Exemplo n.º 2
0
  public static void main(String[] args) {
    LocalDateTime localDateTime = LocalDateTime.of(2016, 3, 27, 1, 20);
    localDateTime = localDateTime.plusHours(1);
    System.out.println(localDateTime);

    ZoneId zoneId = ZoneId.of("Europe/Warsaw");
    ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
    System.out.println(zonedDateTime);
  }
Exemplo n.º 3
0
 public static void main(String[] args) {
   LocalDate date = LocalDate.of(2015, Month.JULY, 6);
   LocalTime time = LocalTime.of(15, 25);
   LocalDateTime dateTime = LocalDateTime.of(date, time);
   printSectionName("LocalDateTime");
   System.out.println(dateTime);
   System.out.println(dateTime.minusDays(1));
   System.out.println(dateTime.minusHours(5));
   System.out.println(dateTime.minusSeconds(7));
   printDelimiterString();
 }
Exemplo n.º 4
0
  @Test
  public void testLocalDateTime() {
    System.out.println(LocalDateTime.now());
    System.out.println(LocalDateTime.of(1994, Month.MAY, 15, 11, 30));
    System.out.println(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));
    System.out.println(LocalDateTime.ofEpochSecond(1450749600, 0, ZoneOffset.ofHours(8)));

    System.out.printf("6 months from now: %s%n", LocalDateTime.now().plusMonths(6));
    System.out.printf("6 days ago: %s%n", LocalDateTime.now().minusDays(6));

    System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now()));
    System.out.println(DateTimeFormatter.ofPattern("MM-dd HH:mm").format(LocalDateTime.now()));
  }
Exemplo n.º 5
0
 public void setDateAndTime(int day, int month, int year, int hour, int minute, int second) {
   LocalDate dateToSet = LocalDate.of(day, month, year);
   LocalTime timeToSet = LocalTime.of(hour, minute, second);
   dateAndTime = LocalDateTime.of(dateToSet, timeToSet);
 }
Exemplo n.º 6
0
 public void setDate(int day, int month, int year) {
   LocalDate dateToSet = LocalDate.of(day, month, year);
   LocalTime currentTime = LocalTime.from(dateAndTime);
   dateAndTime = LocalDateTime.of(dateToSet, currentTime);
 }
Exemplo n.º 7
0
 public void setTime(int hour, int minute, int second) {
   LocalDate currentDate = LocalDate.from(dateAndTime);
   LocalTime timeToSet = LocalTime.of(hour, minute, second);
   dateAndTime = LocalDateTime.of(currentDate, timeToSet);
 }
Exemplo n.º 8
0
 public void test_getValue_Calendrical_dateTime() {
   Calendrical cal = LocalDateTime.of(2007, 6, 20, 12, 30);
   assertEquals(rule().getValue(cal), rule().field(2007));
 }