Back to Repositories

Testing Composite Message Format Parsing in dianping/cat

This test suite evaluates the CompositeFormat class functionality in the CAT monitoring system, specifically focusing on message aggregation formatting. The tests verify proper parsing and formatting of composite messages with placeholders and dynamic content.

Test Coverage Overview

The test coverage focuses on the message format parsing capabilities of the CompositeFormat class.

  • Tests placeholder replacement in composite message formats
  • Verifies handling of static text with dynamic placeholders
  • Covers pattern matching for {world}, {*}, and {md5:8} format specifiers

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework to validate message parsing outcomes.

The implementation employs pattern-based message format parsing with:
  • AggregationMessageFormat for defining message templates
  • CompositeFormat for handling the actual parsing logic
  • Direct string comparison for validation

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • ParseException handling for format errors
  • Custom AggregationMessageFormat class
  • CompositeFormat parser implementation

Best Practices Demonstrated

The test exhibits several testing best practices:

  • Clear test method naming (TestParse)
  • Proper exception handling for parse errors
  • Isolated test cases with specific assertions
  • Well-structured test setup with clear input/output expectations

dianping/cat

cat-consumer/src/test/java/com/dianping/cat/consumer/core/aggregation/CompositeFormatTest.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.consumer.core.aggregation;

import static org.junit.Assert.assertEquals;

import java.text.ParseException;

import org.junit.Test;

import com.dianping.cat.config.AggregationMessageFormat;
import com.dianping.cat.config.CompositeFormat;

public class CompositeFormatTest {
	@Test
	public void TestParse() throws ParseException {
		AggregationMessageFormat amf = new AggregationMessageFormat("Hello {world}.I am{*}.{md5:8}.");
		CompositeFormat format = new CompositeFormat(amf);

		assertEquals("Hello {world}.I am Jack.{md5:8}.", format.parse("Hello world.I am Jack.balabala."));
	}
}