Last active
March 16, 2018 09:08
-
-
Save handstandsam/c6a329a381e58b160575 to your computer and use it in GitHub Desktop.
Wiremock Android Assets FileSource (In Progress)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wiremock_tests; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URI; | |
import java.util.List; | |
import android.content.res.AssetManager; | |
import com.capitalone.mobile.wallet.util.IOUtils; | |
import com.github.tomakehurst.wiremock.common.BinaryFile; | |
import com.github.tomakehurst.wiremock.common.FileSource; | |
import com.github.tomakehurst.wiremock.common.TextFile; | |
import com.google.common.io.ByteStreams; | |
import wiremock_tests.utils.WireMockTestUtils; | |
public class AndroidAssetsBinaryFile extends BinaryFile { | |
private final AssetManager assetManager; | |
private String assetPath; | |
public AndroidAssetsBinaryFile(AssetManager assetManager, String assetPath) { | |
super(null); // We're completely overriding the super() implementation | |
this.assetManager = assetManager; | |
this.assetPath = assetPath; | |
} | |
public byte[] readContents() { | |
// InputStream stream = null; | |
// | |
// byte[] ioe; | |
// try { | |
// stream = this.uri.toURL().openStream(); | |
// ioe = ByteStreams.toByteArray(stream); | |
// } catch (IOException var6) { | |
// throw new RuntimeException(var6); | |
// } finally { | |
// this.closeStream(stream); | |
// } | |
// | |
// return ioe; | |
try { | |
InputStream inputStream = assetManager.open(assetPath); | |
return IOUtils.toByteArray(inputStream); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void closeStream(InputStream stream) { | |
if (stream != null) { | |
try { | |
stream.close(); | |
} catch (IOException var3) { | |
throw new RuntimeException(var3); | |
} | |
} | |
} | |
public String name() { | |
return assetPath; | |
} | |
public String toString() { | |
return this.name(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wiremock_tests; | |
import android.content.res.AssetManager; | |
import com.github.tomakehurst.wiremock.common.BinaryFile; | |
import com.github.tomakehurst.wiremock.common.FileSource; | |
import com.github.tomakehurst.wiremock.common.TextFile; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.List; | |
public class AndroidAssetsReadOnlyFileSource implements FileSource { | |
private final AssetManager assetManager; | |
private String assetPath; | |
public AndroidAssetsReadOnlyFileSource(AssetManager assetManager) { | |
this(assetManager, ""); | |
} | |
public AndroidAssetsReadOnlyFileSource(AssetManager assetManager, String assetPath) { | |
this.assetManager = assetManager; | |
this.assetPath = assetPath; | |
} | |
@Override | |
public BinaryFile getBinaryFileNamed(String name) { | |
return new AndroidAssetsBinaryFile(assetManager, assetPath + "/" + name); | |
} | |
@Override | |
public void createIfNecessary() { | |
return; | |
} | |
@Override | |
public FileSource child(String name) { | |
return new AndroidAssetsReadOnlyFileSource(assetManager, name); | |
} | |
@Override | |
public String getPath() { | |
return assetPath; | |
} | |
@Override | |
public List<TextFile> listFilesRecursively() { | |
return null; | |
} | |
@Override | |
public void writeTextFile(String name, String contents) { | |
return; | |
} | |
@Override | |
public void writeBinaryFile(String name, byte[] contents) { | |
return; | |
} | |
@Override | |
public boolean exists() { | |
boolean exists = false; | |
InputStream inputStream = null; | |
try { | |
assetManager.open(assetPath); | |
exists = true; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (inputStream != null) { | |
try { | |
inputStream.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return exists; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wireMockServer = new WireMockServer(WIREMOCK_LOCAL_PORT, new AndroidAssetsReadOnlyFileSource(getApplication().getAssets()), false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment