All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.sdk.resource.MockResourceAccessor Maven / Gradle / Ivy

The newest version!
package liquibase.sdk.resource;

import liquibase.resource.AbstractResourceAccessor;
import liquibase.resource.Resource;

import java.io.IOException;
import java.util.*;

public class MockResourceAccessor extends AbstractResourceAccessor {

    private final SortedMap contentByFileName;

    public MockResourceAccessor() {
        this(new HashMap<>());
    }

    public MockResourceAccessor(Map contentByFileName) {
        this.contentByFileName = new TreeMap<>(contentByFileName);
    }

    @Override
    public void close() throws Exception {

    }

    @Override
    public List getAll(String path) throws IOException {
        path = path.replace("\\", "/");
        String content = contentByFileName.get(path);
        List returnSet = new ArrayList<>();
        if (content != null) {
            returnSet.add(new MockResource(path, content));
        }

        if (returnSet.isEmpty()) {
            return null;
        }
        return returnSet;
    }

    @Override
    public List search(String path, boolean recursive) throws IOException {
        path = path.replace("\\", "/");
        List returnList = new ArrayList<>();
        for (String file : contentByFileName.keySet()) {
            if (file.startsWith(path)) {
                if (!recursive && (file.split("/").length) - (path.split("/").length) > 2) {
                    continue;
                }
                returnList.add(new MockResource(file, contentByFileName.get(file)));
            }
        }
        return returnList;
    }

    @Override
    public List describeLocations() {
        return Collections.singletonList("MockResouceAccessor.java");
    }

    public void setContent(String fileName, String content) {
        this.contentByFileName.put(fileName, content);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy