Back to Repositories

Testing Local Storage Implementation in Litemall Core Module

This test suite validates the local storage functionality in the Litemall core module, focusing on file storage operations and resource handling. The tests ensure proper file upload, retrieval, and URL generation for stored resources.

Test Coverage Overview

The test coverage focuses on core local storage operations including file storage, resource loading, and URL generation.

  • Tests file upload functionality with image files
  • Validates resource loading capabilities
  • Verifies URL generation for stored resources
  • Covers basic file I/O operations

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit integration. It implements a straightforward test case that exercises the LocalStorage component’s primary functions.

The test leverages Spring’s Resource abstraction and file I/O operations, demonstrating integration with Spring’s core functionality and file handling capabilities.

Technical Details

Testing tools and configuration:

  • JUnit 4 testing framework
  • Spring Test context framework
  • Spring Boot test annotations (@SpringBootTest)
  • Apache Commons Logging for test output
  • WebAppConfiguration for web context testing

Best Practices Demonstrated

The test implementation showcases several testing best practices for Spring Boot applications.

  • Proper dependency injection using @Autowired
  • Resource cleanup through Spring’s resource management
  • Comprehensive logging of test operations
  • Clear test method organization
  • Appropriate use of Spring Boot test annotations

linlinjava/litemall

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

    private final Log logger = LogFactory.getLog(LocalStorageTest.class);
    @Autowired
    private LocalStorage localStorage;

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

}