Move query file from assets to resources

This commit is contained in:
Yusuke Matsubara 2018-01-19 22:22:15 +09:00
parent 9acf22b5b2
commit 4aa4461d2c
4 changed files with 14 additions and 5 deletions

View file

@ -94,7 +94,12 @@ android {
} }
sourceSets { sourceSets {
// use kotlin only in tests (for now)
test.java.srcDirs += 'src/test/kotlin' test.java.srcDirs += 'src/test/kotlin'
// use main assets and resources in test
test.assets.srcDirs += 'src/main/assets'
test.resources.srcDirs += 'src/main/resoures'
} }
buildTypes { buildTypes {

View file

@ -34,7 +34,7 @@ public class NearbyPlaces {
public NearbyPlaces() { public NearbyPlaces() {
try { try {
wikidataQuery = FileUtils.readFromResource("/assets/queries/nearby_query.rq"); wikidataQuery = FileUtils.readFromResource("/queries/nearby_query.rq");
Timber.v(wikidataQuery); Timber.v(wikidataQuery);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View file

@ -4,8 +4,10 @@ import android.os.Environment;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -16,16 +18,18 @@ public class FileUtils {
/** /**
* Read and return the content of a resource file as string. * Read and return the content of a resource file as string.
* *
* @param fileName asset file's path (e.g. "/assets/queries/nearby_query.rq") * @param fileName asset file's path (e.g. "/queries/nearby_query.rq")
* @return the content of the file * @return the content of the file
*/ */
public static String readFromResource(String fileName) throws IOException { public static String readFromResource(String fileName) throws IOException {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = new BufferedReader( InputStream inputStream = FileUtils.class.getResourceAsStream(fileName);
new InputStreamReader( if (inputStream == null) {
CommonsApplication.class.getResourceAsStream(fileName), "UTF-8")); throw new FileNotFoundException(fileName);
}
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
buffer.append(line + "\n"); buffer.append(line + "\n");