You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextFileTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.io;
  18. import com.google.common.jimfs.Configuration;
  19. import com.google.common.jimfs.Jimfs;
  20. import java.io.IOException;
  21. import java.net.URISyntaxException;
  22. import java.nio.charset.Charset;
  23. import java.nio.file.AccessDeniedException;
  24. import java.nio.file.AccessMode;
  25. import java.nio.file.FileSystem;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.nio.file.spi.FileSystemProvider;
  29. import java.util.Arrays;
  30. import java.util.List;
  31. import org.junit.After;
  32. import org.junit.Before;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import org.mockito.Mock;
  36. import org.mockito.runners.MockitoJUnitRunner;
  37. import static org.junit.Assert.assertEquals;
  38. import static org.junit.Assert.assertFalse;
  39. import static org.junit.Assert.assertTrue;
  40. import static org.mockito.Mockito.doThrow;
  41. import static org.mockito.Mockito.when;
  42. @RunWith(MockitoJUnitRunner.class)
  43. public class TextFileTest {
  44. @Mock private Path ro;
  45. @Mock private FileSystem mockedFileSystem;
  46. @Mock private FileSystemProvider fileSystemProvider;
  47. private Path test1;
  48. private Path temp;
  49. private FileSystem fileSystem;
  50. @Before
  51. public void setup() throws IOException {
  52. fileSystem = Jimfs.newFileSystem(Configuration.unix());
  53. Files.copy(getClass().getResourceAsStream("test1.txt"), fileSystem.getPath("/test1.txt"));
  54. test1 = fileSystem.getPath("/test1.txt");
  55. temp = fileSystem.getPath("/temp.txt");
  56. when(mockedFileSystem.provider()).thenReturn(fileSystemProvider);
  57. when(ro.getFileSystem()).thenReturn(mockedFileSystem);
  58. doThrow(new AccessDeniedException("Nup.")).when(fileSystemProvider).checkAccess(ro, AccessMode.WRITE);
  59. }
  60. @After
  61. public void tearDown() throws IOException {
  62. fileSystem.close();
  63. }
  64. @Test
  65. public void testGetLines() throws IOException, URISyntaxException {
  66. final TextFile file = new TextFile(test1, Charset.forName("UTF-8"));
  67. final List<String> lines = file.getLines();
  68. assertEquals(7, lines.size());
  69. assertEquals("Line 1", lines.get(0));
  70. }
  71. @Test
  72. public void testGetLines2() throws IOException, URISyntaxException {
  73. final TextFile file = new TextFile(test1, Charset.forName("UTF-8"));
  74. final List<String> lines = file.getLines();
  75. assertEquals(7, lines.size());
  76. assertEquals("Line 1", lines.get(0));
  77. }
  78. @Test
  79. public void testWrite() throws IOException {
  80. final TextFile file = new TextFile(temp, Charset.forName("UTF-8"));
  81. final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
  82. file.writeLines(lines);
  83. final List<String> newLines = file.getLines();
  84. assertEquals(lines, newLines);
  85. }
  86. @Test(expected = UnsupportedOperationException.class)
  87. public void testIllegalWrite() throws IOException, URISyntaxException {
  88. final TextFile file = new TextFile(ro, Charset.forName("UTF-8"));
  89. file.writeLines(Arrays.asList("hello", "this is a test", "meep"));
  90. }
  91. @Test(expected = UnsupportedOperationException.class)
  92. public void testIllegalDelete() throws IOException, URISyntaxException {
  93. final TextFile file = new TextFile(ro, Charset.forName("UTF-8"));
  94. file.delete();
  95. }
  96. @Test
  97. public void testDelete() throws IOException {
  98. final TextFile file = new TextFile(temp, Charset.forName("UTF-8"));
  99. final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
  100. file.writeLines(lines);
  101. assertTrue(Files.exists(temp));
  102. file.delete();
  103. assertFalse(Files.exists(temp));
  104. }
  105. @Test
  106. public void testIsWriteable_Stream() throws IOException {
  107. final TextFile file = new TextFile(Files.newInputStream(test1), Charset.forName("UTF-8"));
  108. assertFalse(file.isWritable());
  109. }
  110. @Test
  111. public void testReadLine_Stream() throws IOException {
  112. final TextFile file = new TextFile(Files.newInputStream(test1), Charset.forName("UTF-8"));
  113. final List<String> lines = file.getLines();
  114. assertEquals(7, lines.size());
  115. assertEquals("Line 1", lines.get(0));
  116. }
  117. }