Back to Repositories

Testing Tencent Cloud Storage Integration in Litemall

This test suite evaluates the Tencent cloud storage integration functionality in the Litemall application. It validates file upload, resource loading, and URL generation capabilities for the TencentStorage service implementation.

Test Coverage Overview

The test coverage focuses on core cloud storage operations with Tencent’s service.

Key functionality tested includes:
  • File upload to Tencent cloud storage
  • Resource loading from storage
  • URL generation for stored files
  • Image file handling with proper MIME types

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit 4 integration. The implementation leverages Spring’s dependency injection to test the TencentStorage service, using a test image file as the data source.

The test demonstrates Spring’s Resource abstraction and file handling patterns with actual cloud storage interaction.

Technical Details

Testing tools and configuration include:
  • SpringRunner for test execution
  • WebAppConfiguration for web context setup
  • Apache Commons Logging for test output
  • Spring Boot Test framework for dependency injection
  • Test resources including a sample PNG image

Best Practices Demonstrated

The test exhibits several quality testing practices including proper resource cleanup, logging of operations, and integration with Spring’s testing framework.

Notable practices include:
  • Proper exception handling for IO operations
  • Use of Spring’s Resource abstraction
  • Automated test resource management
  • Clear logging of test steps and results

linlinjava/litemall

litemall-core/src/test/java/org/linlinjava/litemall/core/TencentStorageTest.java

            
package org.linlinjava.litemall.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.linlinjava.litemall.core.storage.TencentStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebAppConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest
public class TencentStorageTest {

    private Log logger = LogFactory.getLog(TencentStorageTest.class);
    @Autowired
    private TencentStorage tencentStorage;

    @Test
    public void test() throws IOException {
        String test = getClass().getClassLoader().getResource("litemall.png").getFile();
        File testFile = new File(test);
        tencentStorage.store(new FileInputStream(test), testFile.length(), "image/png", "litemall.png");
        Resource resource = tencentStorage.loadAsResource("litemall.png");
        String url = tencentStorage.generateUrl("litemall.png");
        logger.info("test file " + test);
        logger.info("store file " + resource.getURI());
        logger.info("generate url " + url);
    }

}