Back to Repositories

Testing IP Geolocation Utilities Implementation in ruoyi-vue-pro

This test suite validates IP address handling and geolocation functionality in the IPUtils class, focusing on area ID retrieval and geographic area name resolution for both string and long IP formats.

Test Coverage Overview

The test suite provides comprehensive coverage for IP address processing and location identification.

Key areas tested include:
  • Area ID extraction from string IP addresses
  • Area ID extraction from long-format IP addresses
  • Geographic area name resolution for string IPs
  • Geographic area name resolution for long-format IPs

Implementation Analysis

The testing approach employs JUnit Jupiter for systematic validation of the IPUtils class functionality. Tests utilize both string-based and long-format IP representations, implementing parallel test methods for each format to ensure consistent behavior across input types.

The implementation leverages the ip2region library’s Searcher component for IP validation and geographic mapping.

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter test framework
  • ip2region XDB Searcher for IP validation
  • Custom Area class for geographic data representation
  • Static assertion methods for result verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Discrete test methods for each functionality
  • Clear test method naming conventions
  • Consistent assertion patterns
  • Comprehensive coverage of both input formats
  • Effective use of test data representing real-world scenarios

yunaiv/ruoyi-vue-pro

yudao-framework/yudao-spring-boot-starter-biz-ip/src/test/java/cn/iocoder/yudao/framework/ip/core/utils/IPUtilsTest.java

            
package cn.iocoder.yudao.framework.ip.core.utils;

import cn.iocoder.yudao.framework.ip.core.Area;
import org.junit.jupiter.api.Test;
import org.lionsoul.ip2region.xdb.Searcher;


import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * {@link IPUtils} 的单元测试
 *
 * @author wanglhup
 */
public class IPUtilsTest {

    @Test
    public void testGetAreaId_string() {
        // 120.202.4.0|120.202.4.255|420600
        Integer areaId = IPUtils.getAreaId("120.202.4.50");
        assertEquals(420600, areaId);
    }

    @Test
    public void testGetAreaId_long() throws Exception {
        // 120.203.123.0|120.203.133.255|360900
        long ip = Searcher.checkIP("120.203.123.250");
        Integer areaId = IPUtils.getAreaId(ip);
        assertEquals(360900, areaId);
    }

    @Test
    public void testGetArea_string() {
        // 120.202.4.0|120.202.4.255|420600
        Area area = IPUtils.getArea("120.202.4.50");
        assertEquals("襄阳市", area.getName());
    }

    @Test
    public void testGetArea_long() throws Exception {
        // 120.203.123.0|120.203.133.255|360900
        long ip = Searcher.checkIP("120.203.123.252");
        Area area = IPUtils.getArea(ip);
        assertEquals("宜春市", area.getName());
    }

}