Back to Repositories

Validating Configuration File Utilities in Apollo Config

This test suite validates the ConfigFileUtils class functionality in Apollo, focusing on configuration file name parsing and format validation. The tests ensure proper handling of various file formats and naming patterns used in the Apollo configuration system.

Test Coverage Overview

The test suite provides comprehensive coverage of configuration file utilities, including:

  • Format validation for properties, yml, and json files
  • Exception handling for invalid file formats
  • Parsing of application IDs, cluster names, and namespaces
  • File name generation for different config formats
Edge cases include empty extensions, invalid format strings, and various cluster naming patterns.

Implementation Analysis

The testing approach employs JUnit’s annotation-based test methods with dedicated test cases for both valid and invalid scenarios. The implementation leverages expected exception testing patterns for validation failures and uses assertEquals for verification of parsed components.

The test structure follows the arrange-act-assert pattern with clear separation of positive and negative test cases.

Technical Details

Testing tools and configuration:

  • JUnit 4 testing framework
  • SLF4J logging implementation
  • BadRequestException for error handling
  • ConfigFileFormat enum for file type definitions
  • Static utility method testing patterns

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive positive and negative test cases
  • Clear method naming conventions
  • Proper exception testing
  • Logging integration for debugging
  • Isolated test methods for specific functionality
  • Consistent assertion patterns

apolloconfig/apollo

apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/util/ConfigFileUtilsTest.java

            
/*
 * Copyright 2024 Apollo Authors
 *
 * Licensed 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.ctrip.framework.apollo.portal.util;

import static org.junit.Assert.*;

import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigFileUtilsTest {

  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  @Test
  public void checkFormat() {
    ConfigFileUtils.checkFormat("1234+default+app.properties");
    ConfigFileUtils.checkFormat("1234+default+app.yml");
    ConfigFileUtils.checkFormat("1234+default+app.json");
  }

  @Test(expected = BadRequestException.class)
  public void checkFormatWithException0() {
    ConfigFileUtils.checkFormat("1234+defaultes");
  }

  @Test(expected = BadRequestException.class)
  public void checkFormatWithException1() {
    ConfigFileUtils.checkFormat(".json");
  }

  @Test(expected = BadRequestException.class)
  public void checkFormatWithException2() {
    ConfigFileUtils.checkFormat("application.");
  }

  @Test
  public void getFormat() {
    final String properties = ConfigFileUtils.getFormat("application+default+application.properties");
    assertEquals("properties", properties);

    final String yml = ConfigFileUtils.getFormat("application+default+application.yml");
    assertEquals("yml", yml);
  }

  @Test
  public void getAppId() {
    final String application = ConfigFileUtils.getAppId("application+default+application.properties");
    assertEquals("application", application);

    final String abc = ConfigFileUtils.getAppId("abc+default+application.yml");
    assertEquals("abc", abc);
  }

  @Test
  public void getClusterName() {
    final String cluster = ConfigFileUtils.getClusterName("application+default+application.properties");
    assertEquals("default", cluster);

    final String Beijing = ConfigFileUtils.getClusterName("abc+Beijing+application.yml");
    assertEquals("Beijing", Beijing);
  }

  @Test
  public void getNamespace() {
    final String application = ConfigFileUtils.getNamespace("234+default+application.properties");
    assertEquals("application", application);

    final String applicationYml = ConfigFileUtils.getNamespace("abc+default+application.yml");
    assertEquals("application.yml", applicationYml);
  }

  @Test
  public void toFilename() {
    final String propertiesFilename0 = ConfigFileUtils.toFilename("123", "default", "application", ConfigFileFormat.Properties);
    logger.info("propertiesFilename0 {}", propertiesFilename0);
    assertEquals("123+default+application.properties", propertiesFilename0);

    final String ymlFilename0 = ConfigFileUtils.toFilename("666", "none", "cc.yml", ConfigFileFormat.YML);
    logger.info("ymlFilename0 {}", ymlFilename0);
    assertEquals("666+none+cc.yml", ymlFilename0);
  }

}