Package de.xima.fc.testhelper
Class WebDavServer
- java.lang.Object
-
- de.xima.fc.testhelper.WebDavServer
-
- All Implemented Interfaces:
AutoCloseable
public final class WebDavServer extends Object implements AutoCloseable
A simple WebDav server against which integration tests can be run.- Author:
- XIMA MEDIA GmbH
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
String
davUri(String... path)
boolean
exists(String... path)
byte[]
fileContentAsByteArray(String... path)
InputStream
fileContentAsStream(String... path)
String
fileContentAsUtf8String(String... path)
String
httpUri(String... paths)
List<com.github.sardine.DavResource>
lsDir(int depth, String... path)
List<com.github.sardine.DavResource>
lsDir(String... path)
List<com.github.sardine.DavResource>
lsDirAll(String... path)
void
mkdir(String... path)
static WebDavServer
newServer()
Starts a new WebDAV server that does not required credentials.static WebDavServer
newServer(String username, String password, Path tmpDir)
Starts a new WebDAV server secured with the given credentials.void
put(String path, byte[] data)
void
put(String path, File data)
void
put(String path, InputStream data)
void
put(String path, String data, Charset charset)
void
put(String path, Path data)
void
putUtf8String(String path, String data)
-
-
-
Method Detail
-
close
public void close() throws Exception
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
davUri
public String davUri(String... path)
- Parameters:
path
- Path on the WebDAV server.- Returns:
- URL for the given path, with the
dav://
protocol.
-
exists
public boolean exists(String... path) throws IOException
- Parameters:
path
- Path to check.- Returns:
true
if the given path exists on the server, orfalse
otherwise.- Throws:
IOException
- When the path could not be checked, usually due to a network error.
-
fileContentAsByteArray
public byte[] fileContentAsByteArray(String... path) throws IOException
- Parameters:
path
- Path on the WebDAV server.- Returns:
- The contents of the file at the given path, as a byte array.
- Throws:
IOException
- When the file could not be read.
-
fileContentAsStream
public InputStream fileContentAsStream(String... path) throws IOException
- Parameters:
path
- Path on the WebDAV server.- Returns:
- The contents of the file at the given path, as an input stream.
- Throws:
IOException
- When the file could not be read.
-
fileContentAsUtf8String
public String fileContentAsUtf8String(String... path) throws IOException
- Parameters:
path
- Path on the WebDAV server.- Returns:
- The contents of the file at the given path, as a string with the UTF-8 encoding.
- Throws:
IOException
- When the file could not be read.
-
httpUri
public String httpUri(String... paths)
- Parameters:
paths
- Path on the WebDAV server.- Returns:
- URL for the given path, with the
http://
protocol.
-
lsDir
public List<com.github.sardine.DavResource> lsDir(int depth, String... path) throws IOException
- Parameters:
depth
- The depth to look at (use 0 for single resource, 1 for directory listing, -1 for infinite recursion)path
- Path to process.- Returns:
- The contents of the given directory.
- Throws:
IOException
- When the path could not be checked, usually due to a network error.
-
lsDir
public List<com.github.sardine.DavResource> lsDir(String... path) throws IOException
- Parameters:
path
- Path to process.- Returns:
- The contents of the given directory, not including subdirectories.
- Throws:
IOException
- When the path could not be checked, usually due to a network error.
-
lsDirAll
public List<com.github.sardine.DavResource> lsDirAll(String... path) throws IOException
- Parameters:
path
- Path to process.- Returns:
- The contents of the given directory, including subdirectories.
- Throws:
IOException
- When the path could not be checked, usually due to a network error.
-
mkdir
public void mkdir(String... path) throws IOException
- Parameters:
path
- Path on the WebDAV server.- Throws:
IOException
- Creates the given directory. The directory itself must not exist, all parent directories must exist.
-
put
public void put(String path, byte[] data) throws IOException
- Parameters:
path
- Path on the WebDAV server.data
- The data of the file.- Throws:
IOException
- Creates a file at the given path and the given data. All parent directories must exist.
-
put
public void put(String path, File data) throws IOException
- Parameters:
path
- Path on the WebDAV server.data
- A file with the data.- Throws:
IOException
- Creates a file at the given path and the given data. All parent directories must exist.
-
put
public void put(String path, InputStream data) throws IOException
- Parameters:
path
- Path on the WebDAV server.data
- The data of the file.- Throws:
IOException
- Creates a file at the given path and the given data. All parent directories must exist.
-
put
public void put(String path, Path data) throws IOException
- Parameters:
path
- Path on the WebDAV server.data
- The data of the file.- Throws:
IOException
- Creates a file at the given path and the given data. All parent directories must exist.
-
put
public void put(String path, String data, Charset charset) throws IOException
- Parameters:
path
- Path on the WebDAV server.data
- A string with the data.charset
- Encoding to use for converting the string to bytes.- Throws:
IOException
- Creates a file at the given path and the given data. All parent directories must exist.
-
putUtf8String
public void putUtf8String(String path, String data) throws IOException
- Parameters:
path
- Path on the WebDAV server.data
- The data of the file.- Throws:
IOException
- Creates a file at the given path and the given data. All parent directories must exist.
-
newServer
public static WebDavServer newServer() throws Exception
Starts a new WebDAV server that does not required credentials.- Returns:
- A new server that does not require credentials.
- Throws:
Exception
- When the server could not be created or when it failed to start.
-
newServer
public static WebDavServer newServer(String username, String password, Path tmpDir) throws Exception
Starts a new WebDAV server secured with the given credentials.- Parameters:
username
- User name that must be specified when connecting to the server.password
- Password that must be specified when connecting to the server.tmpDir
- Temporary directory for caching files.- Returns:
- A new server that requires the given user name and password for a connection.
- Throws:
Exception
- When the server could not be created or when it failed to start.
-
-