2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.logging;
23 import static java.lang.Thread.sleep;
24 import static org.junit.Assert.*;
26 import org.junit.After;
27 import org.junit.Test;
30 public class StopWatchTest {
33 public void cleanup() {
38 public void elapsedTimeTest() throws InterruptedException {
42 assertTrue(Long.parseLong(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString())) >= 1000L);
46 public void elapsedTimeConditionalTest() throws InterruptedException {
47 StopWatch.conditionalStart();
49 StopWatch.stopIfStarted();
50 String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
51 long elapsedTime = Long.parseLong(elapsedTimeStr);
52 assertTrue(elapsedTime >= 1000L);
56 public void clearTest() throws InterruptedException {
60 assertNotNull(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()));
63 assertNull(MDC.get(LoggingContext.LoggingField.STOP_WATCH_START.toString()));
64 assertNull(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()));
69 public void stopTest() throws InterruptedException {
74 String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
75 long elapsedTime1 = Long.parseLong(elapsedTimeStr);
77 StopWatch.stopIfStarted();
78 elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
79 long elapsedTime2 = Long.parseLong(elapsedTimeStr);
80 assertTrue(elapsedTime1 == elapsedTime2);
84 public void startTest() throws InterruptedException {
87 StopWatch.conditionalStart();
90 String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
91 long elapsedTime = Long.parseLong(elapsedTimeStr);
93 assertTrue(elapsedTime >= 1000L);