Back to Repositories

Testing Time Comparison Logic Implementation in dianping/cat

This test suite validates time comparison functionality in the Cat monitoring system, focusing on start and end time validation logic. The tests verify the handling of time strings and calendar comparisons for alert scheduling.

Test Coverage Overview

The test suite provides coverage for time comparison logic with specific focus on boundary conditions.

  • Tests start time validation with edge case ’25:00′
  • Validates end time comparison functionality
  • Covers hour and minute level comparisons
  • Includes boundary condition testing for time transitions

Implementation Analysis

The implementation uses JUnit for unit testing, with a focused approach on time comparison logic.

Key patterns include:
  • Calendar instance manipulation for current time reference
  • String parsing for time format validation
  • Boolean logic for start/end time comparisons
  • Separate test cases for start and end time scenarios

Technical Details

Testing infrastructure includes:

  • JUnit 4.x test framework
  • Java Calendar API for time manipulation
  • Assert statements for validation
  • Helper method ‘compareTime’ for reusable time comparison logic
  • String parsing for time format handling

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Clear test method naming convention
  • Isolated test cases for distinct scenarios
  • Helper method abstraction for common functionality
  • Proper assertion usage for validations
  • Edge case consideration with invalid time format

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/alert/JudgeTimeTest.java

            
/*
 * Copyright (c) 2011-2018, Meituan Dianping. All Rights Reserved.
 *
 * 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 com.dianping.cat.report.alert;

import java.util.Calendar;

import org.junit.Assert;
import org.junit.Test;

public class JudgeTimeTest {

	@Test
	public void testStartTime() {
		Calendar cal = Calendar.getInstance();

		Assert.assertFalse(compareTime("25:00", cal, true));
	}

	@Test
	public void testEndTime() {
		Calendar cal = Calendar.getInstance();

		Assert.assertTrue(compareTime("25:00", cal, false));
	}

	private boolean compareTime(String timeStr, Calendar currentCal, boolean isStartTime) {
		String[] times = timeStr.split(":");
		int hour = Integer.parseInt(times[0]);
		int minute = Integer.parseInt(times[1]);
		int currentHour = currentCal.get(Calendar.HOUR_OF_DAY);
		int currentMinute = currentCal.get(Calendar.MINUTE);

		if (currentHour == hour) {
			if (currentMinute == minute) {
				return true;
			} else {
				return (currentMinute > minute) == isStartTime;
			}
		} else {
			return (currentHour > hour) == isStartTime;
		}
	}

}