This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit a5642320414ad766f039560dceca2be88746d8dc
Author: yaohaishi <yaohai...@huawei.com>
AuthorDate: Sun Feb 11 19:28:19 2018 +0800

    SCB-338 extract AccessLogGenerator
---
 .../vertx/accesslog/AccessLogConfiguration.java    |  5 ++-
 .../rest/vertx/accesslog/AccessLogGenerator.java   | 45 ++++++++++++++++++++
 .../vertx/accesslog/impl/AccessLogHandler.java     | 32 ++-------------
 ...andlerTest.java => AccessLogGeneratorTest.java} | 48 ++++------------------
 .../vertx/accesslog/impl/AccessLogHandlerTest.java | 40 ------------------
 5 files changed, 61 insertions(+), 109 deletions(-)

diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogConfiguration.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogConfiguration.java
index 9172095..6f10955 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogConfiguration.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogConfiguration.java
@@ -20,6 +20,7 @@ package org.apache.servicecomb.transport.rest.vertx.accesslog;
 import com.netflix.config.DynamicPropertyFactory;
 
 public final class AccessLogConfiguration {
+
   private static final String BASE = "servicecomb.accesslog.";
 
   private static final String ACCESSLOG_ENABLED = BASE + "enabled";
@@ -28,6 +29,8 @@ public final class AccessLogConfiguration {
 
   public static final AccessLogConfiguration INSTANCE = new 
AccessLogConfiguration();
 
+  public static final String DEFAULT_PATTERN = "%h - - %t %r %s %B";
+
   private AccessLogConfiguration() {
 
   }
@@ -37,7 +40,7 @@ public final class AccessLogConfiguration {
   }
 
   public String getAccesslogPattern() {
-    return getProperty("%h - - %t %r %s %B", ACCESSLOG_PATTERN);
+    return getProperty(DEFAULT_PATTERN, ACCESSLOG_PATTERN);
   }
 
   private String getProperty(String defaultValue, String key) {
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGenerator.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGenerator.java
new file mode 100644
index 0000000..0541392
--- /dev/null
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGenerator.java
@@ -0,0 +1,45 @@
+package org.apache.servicecomb.transport.rest.vertx.accesslog;
+
+import java.util.List;
+
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.AccessLogElement;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogPatternParser;
+
+/**
+ * Accept {@link AccessLogParam} and generate access log.
+ * <br/>
+ * Each AccessLogParam for a line of access log.
+ */
+public class AccessLogGenerator {
+  /**
+   * traversal this array to generate access log segment.
+   */
+  private AccessLogElement[] accessLogElements;
+
+  public AccessLogGenerator(String rawPattern, AccessLogPatternParser 
accessLogPatternParser) {
+    List<AccessLogElementExtraction> extractionList = 
accessLogPatternParser.parsePattern(rawPattern);
+
+    accessLogElements = new AccessLogElement[extractionList.size()];
+    for (int i = 0; i < extractionList.size(); ++i) {
+      accessLogElements[i] = extractionList.get(i).getAccessLogElement();
+    }
+  }
+
+  public String generateLog(AccessLogParam accessLogParam) {
+    StringBuilder log = new StringBuilder(128);
+    accessLogParam.setEndMillisecond(System.currentTimeMillis());
+
+    AccessLogElement[] accessLogElements = getAccessLogElements();
+    for (int i = 0; i < accessLogElements.length; ++i) {
+      log.append(accessLogElements[i].getFormattedElement(accessLogParam));
+    }
+
+    return log.toString();
+  }
+
+
+  private AccessLogElement[] getAccessLogElements() {
+    return accessLogElements;
+  }
+}
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandler.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandler.java
index 3424c4d..bc77a30 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandler.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandler.java
@@ -17,11 +17,8 @@
 
 package org.apache.servicecomb.transport.rest.vertx.accesslog.impl;
 
-import java.util.List;
-
+import 
org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogGenerator;
 import org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogParam;
-import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.AccessLogElement;
-import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogPatternParser;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,15 +29,10 @@ import io.vertx.ext.web.RoutingContext;
 public class AccessLogHandler implements Handler<RoutingContext> {
   private static Logger LOGGER = LoggerFactory.getLogger("accesslog");
 
-  private static AccessLogElement[] accessLogElements;
+  private AccessLogGenerator accessLogGenerator;
 
   public AccessLogHandler(String rawPattern, AccessLogPatternParser 
accessLogPatternParser) {
-    List<AccessLogElementExtraction> extractionList = 
accessLogPatternParser.parsePattern(rawPattern);
-
-    accessLogElements = new AccessLogElement[extractionList.size()];
-    for (int i = 0; i < extractionList.size(); ++i) {
-      accessLogElements[i] = extractionList.get(i).getAccessLogElement();
-    }
+    accessLogGenerator = new AccessLogGenerator(rawPattern, 
accessLogPatternParser);
   }
 
   @Override
@@ -48,24 +40,8 @@ public class AccessLogHandler implements 
Handler<RoutingContext> {
     AccessLogParam accessLogParam = new 
AccessLogParam().setStartMillisecond(System.currentTimeMillis())
         .setRoutingContext(context);
 
-    context.addBodyEndHandler(v -> log(accessLogParam));
+    context.addBodyEndHandler(v -> 
LOGGER.info(accessLogGenerator.generateLog(accessLogParam)));
 
     context.next();
   }
-
-  private void log(AccessLogParam accessLogParam) {
-    StringBuilder log = new StringBuilder(128);
-    accessLogParam.setEndMillisecond(System.currentTimeMillis());
-
-    AccessLogElement[] accessLogElements = getAccessLogElements();
-    for (int i = 0; i < accessLogElements.length; ++i) {
-      log.append(accessLogElements[i].getFormattedElement(accessLogParam));
-    }
-
-    LOGGER.info(log.toString());
-  }
-
-  private AccessLogElement[] getAccessLogElements() {
-    return accessLogElements;
-  }
 }
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGeneratorTest.java
similarity index 58%
copy from 
transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
copy to 
transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGeneratorTest.java
index a4e3337..f7747fe 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/AccessLogGeneratorTest.java
@@ -1,21 +1,4 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.servicecomb.transport.rest.vertx.accesslog.impl;
+package org.apache.servicecomb.transport.rest.vertx.accesslog;
 
 import static org.junit.Assert.assertEquals;
 
@@ -23,23 +6,21 @@ import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.TimeZone;
 
-import org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogParam;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.AccessLogElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.DatetimeConfigurableElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.MethodElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.PlainTextElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
-import org.junit.BeforeClass;
+import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
-import org.slf4j.Logger;
 
 import io.vertx.core.http.HttpMethod;
 import io.vertx.core.http.HttpServerRequest;
 import io.vertx.ext.web.RoutingContext;
 import mockit.Deencapsulation;
 
-public class AccessLogHandlerTest {
+public class AccessLogGeneratorTest {
 
   private static final AccessLogElement methodElement = new MethodElement();
 
@@ -47,23 +28,16 @@ public class AccessLogHandlerTest {
 
   private static final AccessLogElement plainTextElement = new 
PlainTextElement(" - ");
 
-  private static final Logger logger = Mockito.mock(Logger.class);
-
-  private static final AccessLogHandler ACCESS_LOG_HANDLER = new 
AccessLogHandler("rawPattern", s -> {
+  private static final AccessLogGenerator ACCESS_LOG_GENERATOR = new 
AccessLogGenerator("rawPattern", s -> {
     assertEquals("rawPattern", s);
     return Arrays.asList(new 
AccessLogElementExtraction().setAccessLogElement(methodElement),
         new AccessLogElementExtraction().setAccessLogElement(plainTextElement),
         new AccessLogElementExtraction().setAccessLogElement(datetimeElement));
   });
 
-  @BeforeClass
-  public static void init() {
-    Deencapsulation.setField(AccessLogHandler.class, "LOGGER", logger);
-  }
-
   @Test
   public void testConstructor() {
-    AccessLogElement[] elements = Deencapsulation.getField(ACCESS_LOG_HANDLER, 
"accessLogElements");
+    AccessLogElement[] elements = 
Deencapsulation.getField(ACCESS_LOG_GENERATOR, "accessLogElements");
     assertEquals(3, elements.length);
     assertEquals(methodElement, elements[0]);
     assertEquals(plainTextElement, elements[1]);
@@ -71,12 +45,6 @@ public class AccessLogHandlerTest {
   }
 
   @Test
-  public void handle() {
-    RoutingContext testContext = Mockito.mock(RoutingContext.class);
-    ACCESS_LOG_HANDLER.handle(testContext);
-  }
-
-  @Test
   public void testLog() {
     RoutingContext context = Mockito.mock(RoutingContext.class);
     HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
@@ -90,8 +58,8 @@ public class AccessLogHandlerTest {
     Mockito.when(context.request()).thenReturn(request);
     Mockito.when(request.method()).thenReturn(HttpMethod.DELETE);
 
-    Deencapsulation.invoke(ACCESS_LOG_HANDLER, "log", accessLogParam);
+    String log = ACCESS_LOG_GENERATOR.generateLog(accessLogParam);
 
-    Mockito.verify(logger).info("DELETE" + " - " + 
simpleDateFormat.format(startMillisecond));
+    Assert.assertEquals("DELETE" + " - " + 
simpleDateFormat.format(startMillisecond), log);
   }
-}
+}
\ No newline at end of file
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
index a4e3337..d907a15 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/accesslog/impl/AccessLogHandlerTest.java
@@ -19,25 +19,18 @@ package 
org.apache.servicecomb.transport.rest.vertx.accesslog.impl;
 
 import static org.junit.Assert.assertEquals;
 
-import java.text.SimpleDateFormat;
 import java.util.Arrays;
-import java.util.TimeZone;
 
-import org.apache.servicecomb.transport.rest.vertx.accesslog.AccessLogParam;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.AccessLogElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.DatetimeConfigurableElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.MethodElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.element.impl.PlainTextElement;
 import 
org.apache.servicecomb.transport.rest.vertx.accesslog.parser.AccessLogElementExtraction;
-import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.slf4j.Logger;
 
-import io.vertx.core.http.HttpMethod;
-import io.vertx.core.http.HttpServerRequest;
 import io.vertx.ext.web.RoutingContext;
-import mockit.Deencapsulation;
 
 public class AccessLogHandlerTest {
 
@@ -56,42 +49,9 @@ public class AccessLogHandlerTest {
         new AccessLogElementExtraction().setAccessLogElement(datetimeElement));
   });
 
-  @BeforeClass
-  public static void init() {
-    Deencapsulation.setField(AccessLogHandler.class, "LOGGER", logger);
-  }
-
-  @Test
-  public void testConstructor() {
-    AccessLogElement[] elements = Deencapsulation.getField(ACCESS_LOG_HANDLER, 
"accessLogElements");
-    assertEquals(3, elements.length);
-    assertEquals(methodElement, elements[0]);
-    assertEquals(plainTextElement, elements[1]);
-    assertEquals(datetimeElement, elements[2]);
-  }
-
   @Test
   public void handle() {
     RoutingContext testContext = Mockito.mock(RoutingContext.class);
     ACCESS_LOG_HANDLER.handle(testContext);
   }
-
-  @Test
-  public void testLog() {
-    RoutingContext context = Mockito.mock(RoutingContext.class);
-    HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
-    long startMillisecond = 1416863450581L;
-    AccessLogParam accessLogParam = new 
AccessLogParam().setStartMillisecond(startMillisecond)
-        .setRoutingContext(context);
-    SimpleDateFormat simpleDateFormat = new 
SimpleDateFormat(DatetimeConfigurableElement.DEFAULT_DATETIME_PATTERN,
-        DatetimeConfigurableElement.DEFAULT_LOCALE);
-    simpleDateFormat.setTimeZone(TimeZone.getDefault());
-
-    Mockito.when(context.request()).thenReturn(request);
-    Mockito.when(request.method()).thenReturn(HttpMethod.DELETE);
-
-    Deencapsulation.invoke(ACCESS_LOG_HANDLER, "log", accessLogParam);
-
-    Mockito.verify(logger).info("DELETE" + " - " + 
simpleDateFormat.format(startMillisecond));
-  }
 }

-- 
To stop receiving notification emails like this one, please contact
ningji...@apache.org.

Reply via email to