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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2006-2014 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 java.io.File;
  24. import java.io.IOException;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.assertFalse;
  30. import static org.junit.Assert.assertTrue;
  31. public class TextFileTest {
  32. @Test
  33. public void testGetLines() throws IOException {
  34. final TextFile file
  35. = new TextFile(getClass().getResourceAsStream("test1.txt"));
  36. final List<String> lines = file.getLines();
  37. assertEquals(7, lines.size());
  38. assertEquals("Line 1", lines.get(0));
  39. }
  40. @Test
  41. public void testGetLines2() throws IOException {
  42. final TextFile file
  43. = new TextFile(getClass().getResourceAsStream("test1.txt"));
  44. final List<String> lines = file.getLines();
  45. assertEquals(7, lines.size());
  46. assertEquals("Line 1", lines.get(0));
  47. }
  48. @Test
  49. public void testWrite() throws IOException {
  50. File tempFile = File.createTempFile("dmdirc_unit_test", null);
  51. TextFile file = new TextFile(tempFile);
  52. final List<String> lines = Arrays.asList(new String[]{
  53. "hello", "this is a test", "meep"
  54. });
  55. file.writeLines(lines);
  56. file = new TextFile(tempFile);
  57. final List<String> newLines = file.getLines();
  58. assertEquals(lines, newLines);
  59. tempFile.deleteOnExit();
  60. }
  61. @Test(expected = UnsupportedOperationException.class)
  62. public void testIllegalWrite() throws IOException {
  63. final TextFile file
  64. = new TextFile(getClass().getResourceAsStream("test1.txt"));
  65. file.writeLines(Arrays.asList(new String[]{
  66. "hello", "this is a test", "meep"
  67. }));
  68. }
  69. @Test(expected = UnsupportedOperationException.class)
  70. public void testIllegalDelete() throws IOException {
  71. final TextFile file
  72. = new TextFile(getClass().getResourceAsStream("test1.txt"));
  73. file.delete();
  74. }
  75. @Test
  76. public void testDelete() throws IOException {
  77. File tempFile = File.createTempFile("dmdirc_unit_test", "de;ete");
  78. TextFile file = new TextFile(tempFile);
  79. final List<String> lines = Arrays.asList(new String[]{
  80. "hello", "this is a test", "meep"
  81. });
  82. file.writeLines(lines);
  83. assertTrue(tempFile.exists());
  84. file = new TextFile(tempFile);
  85. file.delete();
  86. assertFalse(tempFile.exists());
  87. }
  88. }