Back to Repositories

Testing URL Extraction Functionality in termux-app

This test suite validates URL extraction functionality in the Termux Android application. The tests ensure accurate parsing and handling of various URL formats within text content, focusing on the TermuxUrlUtils class implementation.

Test Coverage Overview

The test coverage focuses on URL extraction capabilities across different text scenarios.

  • Tests single URL extraction from plain text
  • Validates multiple URL handling with line breaks
  • Verifies mixed protocol handling (http/https)
  • Tests URL fragment identifier handling

Implementation Analysis

The testing approach employs JUnit’s assertion framework with a custom helper method (assertUrlsAre) to streamline URL validation. The implementation uses LinkedHashSet for maintaining unique URLs while preserving insertion order.

The pattern follows arrange-act-assert with various text inputs and expected URL outputs.

Technical Details

  • Testing Framework: JUnit
  • Core Classes: TermuxUrlUtils
  • Data Structures: LinkedHashSet
  • Helper Methods: assertUrlsAre for verification
  • Test Scope: URL extraction functionality

Best Practices Demonstrated

The test suite exhibits several testing best practices for maintainable and reliable code.

  • Single Responsibility: Each test case focuses on specific URL extraction scenarios
  • DRY Principle: Utilizes helper method to reduce code duplication
  • Clear Test Names: Descriptive method names indicating test purpose
  • Comprehensive Assertions: Validates exact expected outputs

termux/termux-app

app/src/test/java/com/termux/app/TermuxActivityTest.java

            
package com.termux.app;

import com.termux.shared.termux.data.TermuxUrlUtils;

import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.LinkedHashSet;

public class TermuxActivityTest {

    private void assertUrlsAre(String text, String... urls) {
        LinkedHashSet<String> expected = new LinkedHashSet<>();
        Collections.addAll(expected, urls);
        Assert.assertEquals(expected, TermuxUrlUtils.extractUrls(text));
    }

    @Test
    public void testExtractUrls() {
        assertUrlsAre("hello http://example.com world", "http://example.com");

        assertUrlsAre("http://example.com\nhttp://another.com", "http://example.com", "http://another.com");

        assertUrlsAre("hello http://example.com world and http://more.example.com with secure https://more.example.com",
            "http://example.com", "http://more.example.com", "https://more.example.com");

        assertUrlsAre("hello https://example.com/#bar https://example.com/foo#bar",
            "https://example.com/#bar", "https://example.com/foo#bar");
    }

}