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

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