FileOutputStreamTest.cpp 3.1 KB

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