/bson/src/test/unit/util/JsonPoweredTestHelper.java

https://github.com/foursquare/mongo-java-driver · Java · 80 lines · 54 code · 9 blank · 17 comment · 8 complexity · dfa8e291299479a1ae1016e55690cb08 MD5 · raw file

  1. /*
  2. * Copyright 2015 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. *
  17. */
  18. package util;
  19. import org.bson.BsonDocument;
  20. import org.bson.codecs.BsonDocumentCodec;
  21. import org.bson.codecs.DecoderContext;
  22. import org.bson.json.JsonReader;
  23. import java.io.BufferedReader;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import java.net.URISyntaxException;
  29. import java.nio.charset.Charset;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. public final class JsonPoweredTestHelper {
  33. public static BsonDocument getTestDocument(final File file) throws IOException {
  34. return new BsonDocumentCodec().decode(new JsonReader(getFileAsString(file)), DecoderContext.builder().build());
  35. }
  36. public static List<File> getTestFiles(final String resourcePath) throws URISyntaxException {
  37. List<File> files = new ArrayList<File>();
  38. addFilesFromDirectory(new File(JsonPoweredTestHelper.class.getResource(resourcePath).toURI()), files);
  39. return files;
  40. }
  41. private static String getFileAsString(final File file) throws IOException {
  42. StringBuilder stringBuilder = new StringBuilder();
  43. String line;
  44. String ls = System.getProperty("line.separator");
  45. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")));
  46. try {
  47. while ((line = reader.readLine()) != null) {
  48. stringBuilder.append(line);
  49. stringBuilder.append(ls);
  50. }
  51. } finally {
  52. reader.close();
  53. }
  54. return stringBuilder.toString();
  55. }
  56. private static void addFilesFromDirectory(final File directory, final List<File> files) {
  57. String[] fileNames = directory.list();
  58. if (fileNames != null) {
  59. for (String fileName : fileNames) {
  60. File file = new File(directory, fileName);
  61. if (file.isDirectory()) {
  62. addFilesFromDirectory(file, files);
  63. } else if (file.getName().endsWith(".json")) {
  64. files.add(file);
  65. }
  66. }
  67. }
  68. }
  69. private JsonPoweredTestHelper() {
  70. }
  71. }