FileOutputStreamTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2023-03-25
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. using ls::std::core::FileNotFoundException;
  14. using ls::std::core::FileOperationException;
  15. using ls::std::io::File;
  16. using ls::std::io::FileOutputStream;
  17. using ls::std::io::FileReader;
  18. using ls::std::test::TestHelper;
  19. using std::string;
  20. using testing::Test;
  21. namespace
  22. {
  23. class FileOutputStreamTest : public Test
  24. {
  25. public:
  26. FileOutputStreamTest() = default;
  27. ~FileOutputStreamTest() override = default;
  28. };
  29. TEST_F(FileOutputStreamTest, constructor_file_does_not_exist)
  30. {
  31. string path = TestHelper::getResourcesFolderLocation() + "not-existing.txt";
  32. File file{path};
  33. EXPECT_THROW(
  34. {
  35. try
  36. {
  37. FileOutputStream outputStream{file};
  38. }
  39. catch (const FileNotFoundException &_exception)
  40. {
  41. throw;
  42. }
  43. },
  44. FileNotFoundException);
  45. }
  46. TEST_F(FileOutputStreamTest, write)
  47. {
  48. string path = TestHelper::getResourcesFolderLocation() + "tmp-output-stream.txt";
  49. File file{path};
  50. file.createNewFile();
  51. ASSERT_TRUE(file.exists());
  52. FileOutputStream outputStream{file};
  53. ASSERT_TRUE(outputStream.write("Hello! "));
  54. ASSERT_TRUE(outputStream.write("How are you?"));
  55. outputStream.close();
  56. file.remove();
  57. ASSERT_FALSE(file.exists());
  58. }
  59. TEST_F(FileOutputStreamTest, write_with_another_appending_stream)
  60. {
  61. string path = TestHelper::getResourcesFolderLocation() + "tmp-output-stream.txt";
  62. File file{path};
  63. file.createNewFile();
  64. ASSERT_TRUE(file.exists());
  65. FileOutputStream outputStream{file};
  66. ASSERT_TRUE(outputStream.write("Hello! "));
  67. ASSERT_TRUE(outputStream.write("How are you?"));
  68. outputStream.close();
  69. FileOutputStream newOutputStream{file, true};
  70. ASSERT_TRUE(newOutputStream.write(" I'm fine! "));
  71. ASSERT_TRUE(newOutputStream.write("Thank you!"));
  72. newOutputStream.close();
  73. // validation
  74. FileReader reader{file};
  75. string content{reader.read()};
  76. ASSERT_TRUE(content.find("Hello! How are you? I'm fine! Thank you!") != string::npos);
  77. file.remove();
  78. ASSERT_FALSE(file.exists());
  79. }
  80. TEST_F(FileOutputStreamTest, write_no_permission_to_write)
  81. {
  82. #if defined(unix) || defined(__APPLE__)
  83. File file{TestHelper::getResourcesFolderLocation() + "no-writable.txt"};
  84. #endif
  85. #ifdef _WIN32
  86. File file{TestHelper::getResourcesFolderLocation() + "no-writable-windows.txt"};
  87. #endif
  88. FileOutputStream outputStream{file};
  89. EXPECT_THROW(
  90. {
  91. try
  92. {
  93. outputStream.write("something");
  94. }
  95. catch (const FileOperationException &_exception)
  96. {
  97. throw;
  98. }
  99. },
  100. FileOperationException);
  101. }
  102. }