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 org.junit.After;
24 import org.junit.Test;
27 import static java.lang.Thread.sleep;
28 import static org.junit.Assert.*;
30 public class StopWatchTest {
33 public void cleanup() {
37 public void elapsedTimeTest() throws InterruptedException {
41 assertTrue(Long.parseLong(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString())) >= 1000L);
44 public void elapsedTimeConditionalTest() throws InterruptedException {
45 StopWatch.conditionalStart();
47 StopWatch.stopIfStarted();
48 String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
49 long elapsedTime = Long.parseLong(elapsedTimeStr);
50 assertTrue(elapsedTime >= 1000L);
53 public void clearTest() throws InterruptedException {
57 assertNotNull( MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()));
60 assertNull(MDC.get(LoggingContext.LoggingField.STOP_WATCH_START.toString()));
61 assertNull(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()));
66 public void stopTest() throws InterruptedException {
71 String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
72 long elapsedTime1 = Long.parseLong(elapsedTimeStr);
74 StopWatch.stopIfStarted();
75 elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
76 long elapsedTime2 = Long.parseLong(elapsedTimeStr);
77 assertTrue(elapsedTime1 == elapsedTime2);
81 public void startTest() throws InterruptedException {
84 StopWatch.conditionalStart();
87 String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString());
88 long elapsedTime = Long.parseLong(elapsedTimeStr);
90 assertTrue(elapsedTime >= 1000L);