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

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

There is a newer version: 4.30.0
Show newest version
package liquibase.sdk.resource;

import liquibase.GlobalConfiguration;
import liquibase.resource.AbstractResourceAccessor;
import liquibase.resource.InputStreamList;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.*;

public class MockResourceAccessor extends AbstractResourceAccessor {

    private Map contentByFileName;

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

    public MockResourceAccessor(Map contentByFileName) {
        this.contentByFileName = contentByFileName;
    }


    @Override
    public InputStreamList openStreams(String relativeTo, String streamPath) throws IOException {
        InputStream stream = null;
        if (contentByFileName.containsKey(streamPath)) {
            stream = new ByteArrayInputStream(contentByFileName.get(streamPath).getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()));
        }
        if (stream == null) {
            return null;
        } else {
            InputStreamList list = new InputStreamList();
            list.add(URI.create(streamPath), stream);
            return list;
        }
    }

    @Override
    public SortedSet list(String relativeTo, String path, boolean recursive, boolean includeFiles, boolean includeDirectories) throws IOException {
        SortedSet returnSet = new TreeSet<>();
        for (String file : contentByFileName.keySet()) {
            if (file.startsWith(path)) {
                returnSet.add(file);
            }
        }
        return returnSet;
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy