Back to Repositories

Testing Storage Report Merger Implementation in CAT

This test suite validates the functionality of the HistoryStorageReportMerger class in the CAT monitoring system. It specifically tests the merging capabilities of storage reports, ensuring accurate combination of historical storage data from multiple sources.

Test Coverage Overview

The test suite focuses on verifying the storage report merging functionality with comprehensive coverage.

Key areas tested include:
  • XML parsing and processing of storage reports
  • Merging multiple storage reports into a single consolidated report
  • Validation of merged report content against expected results
  • UTF-8 encoding handling during file operations

Implementation Analysis

The testing approach utilizes JUnit framework with a focus on XML-based data processing.

Key implementation patterns include:
  • File-based test data loading using resource streams
  • SAX parsing for XML processing
  • Visitor pattern implementation for report processing
  • String comparison with whitespace normalization

Technical Details

Testing tools and components:
  • JUnit 4 testing framework
  • DefaultSaxParser for XML parsing
  • DefaultXmlBuilder for XML generation
  • Files.forIO utility for file operations
  • StorageReportMerger for report consolidation

Best Practices Demonstrated

The test implementation showcases several testing best practices and quality approaches.

Notable practices include:
  • Clear test method naming and organization
  • Proper resource handling and file operations
  • Explicit assertion messages
  • Careful handling of platform-specific line endings
  • Separation of test data from test logic

dianping/cat

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

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

import com.dianping.cat.consumer.storage.StorageReportMerger;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import com.dianping.cat.consumer.storage.model.transform.DefaultSaxParser;
import com.dianping.cat.consumer.storage.model.transform.DefaultXmlBuilder;
import com.dianping.cat.report.page.storage.task.HistoryStorageReportMerger;

public class HistoryStorageReportMergerTest {
	@Test
	public void testMerge() throws Exception {
		String oldXml = Files.forIO().readFrom(getClass().getResourceAsStream("storage.xml"), "utf-8");
		StorageReport report1 = DefaultSaxParser.parse(oldXml);
		StorageReport report2 = DefaultSaxParser.parse(oldXml);
		String expected = Files.forIO().readFrom(getClass().getResourceAsStream("result.xml"), "utf-8");
		StorageReportMerger merger = new HistoryStorageReportMerger(new StorageReport(report1.getId()));

		report1.accept(merger);
		report2.accept(merger);

		String actual = new DefaultXmlBuilder().buildXml(merger.getStorageReport());

		Assert.assertEquals("Check the merge result!", expected.replace("\r", ""), actual.replace("\r", ""));
	}
}