Back to Repositories

Testing Alert Message Delivery System in dianping/cat

This test suite validates the SenderManager component in the Cat monitoring system, focusing on alert message delivery functionality. The tests ensure proper handling of alert channels and message sending capabilities.

Test Coverage Overview

The test suite covers core alert sending functionality through the SenderManager component.

  • Validates email alert channel functionality
  • Tests message entity construction and delivery
  • Verifies receiver list handling
  • Checks development mode configuration

Implementation Analysis

The testing approach uses JUnit with ComponentTestCase for dependency injection and component lookup.

Key patterns include:
  • System property configuration for dev mode
  • Component lookup pattern for SenderManager instantiation
  • Message entity construction with multiple receivers

Technical Details

Testing tools and configuration:
  • JUnit testing framework
  • ComponentTestCase for DI container
  • SendMessageEntity for message construction
  • AlertChannel enumeration for delivery method
  • Development mode system property setting

Best Practices Demonstrated

The test demonstrates strong testing practices through proper test isolation and component setup.

  • Clear test setup using @Before annotation
  • Proper dependency injection usage
  • Explicit test environment configuration
  • Clear message construction pattern

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/alert/sender/SenderManagerTest.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.sender;

import java.util.ArrayList;
import java.util.List;

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

import com.dianping.cat.alarm.spi.AlertChannel;
import com.dianping.cat.alarm.spi.sender.SendMessageEntity;
import com.dianping.cat.alarm.spi.sender.SenderManager;

public class SenderManagerTest extends ComponentTestCase {

	@Before
	public void before() throws Exception {
		System.setProperty("devMode", "true");
	}

	@Test
	public void test() throws Exception {
		SenderManager manager = lookup(SenderManager.class);
		List<String> receivers = new ArrayList<String>();

		receivers.add("[email protected]");
		SendMessageEntity message = new SendMessageEntity("Test", "test", "title", "content", receivers);
		boolean result = manager.sendAlert(AlertChannel.MAIL, message);

		System.out.println(result);
	}

}