Back to Repositories

Testing Event Report Filter Implementation in dianping/cat

This test suite validates the EventReportFilter functionality in the CAT monitoring system, focusing on XML parsing and filtering capabilities for event reports. The tests ensure proper handling of event data filtering with different parameter combinations.

Test Coverage Overview

The test suite provides comprehensive coverage of the EventReportFilter component, validating both null parameter handling and specific type filtering.

Key areas tested include:
  • Null filter parameter validation
  • Type-specific filtering (URL type)
  • XML parsing and comparison
  • Report transformation accuracy

Implementation Analysis

The testing approach employs JUnit framework with XML-based test data validation. The implementation uses a combination of file-based test resources and SAX parsing to verify filter behavior.

Key patterns include:
  • Resource file loading for test data
  • XML parsing and comparison
  • Filter chain validation

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • DefaultSaxParser for XML processing
  • TestHelper for equality comparison
  • UTF-8 encoded XML test files
  • Custom EventReportFilter implementation

Best Practices Demonstrated

The test suite demonstrates several testing best practices in Java unit testing.

Notable practices include:
  • Isolated test resources
  • Clear test data separation
  • Proper exception handling
  • Readable assertion statements
  • Efficient resource loading

dianping/cat

cat-home/src/test/java/com/dianping/cat/report/page/event/EventReportFilterTest.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.page.event;

import org.junit.Assert;
import org.junit.Test;
import org.unidal.helper.Files;

import com.dianping.cat.TestHelper;
import com.dianping.cat.consumer.event.model.entity.EventReport;
import com.dianping.cat.consumer.event.model.transform.DefaultSaxParser;
import com.dianping.cat.report.page.event.service.LocalEventService.EventReportFilter;

public class EventReportFilterTest {
	@Test
	public void test() throws Exception {
		String source = Files.forIO().readFrom(getClass().getResourceAsStream("event_filter.xml"), "utf-8");
		EventReport report = DefaultSaxParser.parse(source);

		EventReportFilter f1 = new EventReportFilter(null, null, null);
		String expected1 = Files.forIO().readFrom(getClass().getResourceAsStream("event_filter_type.xml"), "utf-8");

		Assert.assertTrue(TestHelper.isEquals(DefaultSaxParser.parse(expected1), DefaultSaxParser.parse(f1.buildXml(report))));

		EventReportFilter f2 = new EventReportFilter("URL", null, null);
		String expected2 = Files.forIO().readFrom(getClass().getResourceAsStream("event_filter_name.xml"), "utf-8");

		Assert.assertTrue(TestHelper.isEquals(DefaultSaxParser.parse(expected2), DefaultSaxParser.parse(f2.buildXml(report))));
	}
}