Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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