Back to Repositories

Testing Cached Report Builder Implementation in dianping/cat

A comprehensive test suite for validating the cached report builder functionality in the CAT monitoring system. This test suite focuses on verifying the daily, weekly, and monthly report generation processes with component-based testing.

Test Coverage Overview

The test suite provides extensive coverage of the CachedReportBuilder component, focusing on report generation timing and task execution.

  • Tests daily task building functionality
  • Verifies weekly and monthly report task registration
  • Validates server configuration initialization
  • Ensures proper task execution timing

Implementation Analysis

The implementation utilizes JUnit framework with ComponentTestCase as the base class for dependency injection and component lookup. The testing approach employs asynchronous verification with thread sleep to ensure task completion.

  • Uses ComponentTestCase for component management
  • Implements task registration verification
  • Employs async testing patterns

Technical Details

  • JUnit test framework implementation
  • Server configuration initialization from XML
  • Component lookup mechanism for dependency injection
  • TimeHelper utility for date management
  • Assert statements for validation

Best Practices Demonstrated

The test demonstrates several testing best practices including proper setup of test dependencies and validation of asynchronous operations.

  • Clean component initialization
  • Proper resource management
  • Async operation verification
  • Clear test case organization

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/task/cached/CachedReportBuilerTest.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.task.cached;

import java.io.File;
import java.util.Date;

import org.junit.Assert;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;

import com.dianping.cat.Cat;
import com.dianping.cat.config.server.ServerConfigManager;
import com.dianping.cat.helper.TimeHelper;
import com.dianping.cat.report.task.TaskBuilder;
import com.dianping.cat.report.task.current.CurrentReportBuilder;
import com.dianping.cat.report.task.current.CurrentWeeklyMonthlyReportTask;
import com.dianping.cat.report.task.current.CurrentWeeklyMonthlyReportTask.CurrentWeeklyMonthlyTask;

public class CachedReportBuilerTest extends ComponentTestCase {

	private int m_index = 0;

	@Test
	public void test() throws Exception {
		ServerConfigManager manager = (ServerConfigManager) lookup(ServerConfigManager.class);

		manager.initialize(new File(Cat.getCatHome(),"server.xml"));

		TaskBuilder builder = lookup(TaskBuilder.class, CurrentReportBuilder.ID);
		CurrentWeeklyMonthlyReportTask.getInstance().register(new CurrentWeeklyMonthlyTask() {

			@Override
			public String getReportName() {
				return "Test";
			}

			@Override
			public void buildCurrentWeeklyTask(String name, String domain, Date start) {
				m_index++;
			}

			@Override
			public void buildCurrentMonthlyTask(String name, String domain, Date start) {
				m_index++;
			}
		});

		builder.buildDailyTask("test", "test", TimeHelper.getCurrentDay());

		Thread.sleep(1000 * 5);

		Assert.assertEquals(true, m_index > 0);
	}

}