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.2KB

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