Back to Repositories

Testing Aliyun Cloud Storage Integration in litemall

This test suite validates Aliyun cloud storage integration in the litemall application, focusing on file upload, retrieval, and URL generation functionality. The tests ensure proper handling of image files and storage operations using Aliyun’s cloud infrastructure.

Test Coverage Overview

The test suite covers core Aliyun storage operations including:
  • File upload functionality using FileInputStream
  • Resource loading and retrieval capabilities
  • URL generation for stored files
  • Integration with Spring’s Resource interface
Test coverage focuses on the complete lifecycle of file storage operations, from initial upload to access URL generation.

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit4 integration.

Key implementation patterns include:
  • Spring Runner configuration for dependency injection
  • WebAppConfiguration for web context simulation
  • Resource path handling using ClassLoader
  • Logging integration for operation verification

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Spring Boot Test context
  • Apache Commons Logging
  • Aliyun SDK integration
  • Spring Resource abstraction
Configuration leverages Spring Boot’s auto-configuration capabilities with custom Aliyun storage settings.

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Proper dependency injection using Spring’s @Autowired
  • Resource cleanup through Spring’s managed resources
  • Effective logging for operation tracking
  • Isolation of storage operations for unit testing
  • Use of test resources for predictable test execution

linlinjava/litemall

litemall-core/src/test/java/org/linlinjava/litemall/core/AliyunStorageTest.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.AliyunStorage;
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.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 AliyunStorageTest {

    private final Log logger = LogFactory.getLog(AliyunStorageTest.class);
    @Autowired
    private AliyunStorage aliyunStorage;

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

}