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.

YamlAutoCommandStoreTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.commandparser.auto;
  23. import com.google.common.base.Optional;
  24. import com.google.common.collect.Sets;
  25. import com.google.common.jimfs.Configuration;
  26. import com.google.common.jimfs.Jimfs;
  27. import java.io.IOException;
  28. import java.nio.file.FileSystem;
  29. import java.nio.file.Files;
  30. import java.util.Set;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import static junit.framework.TestCase.assertFalse;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertTrue;
  36. public class YamlAutoCommandStoreTest {
  37. private FileSystem fs;
  38. private YamlAutoCommandStore ycs;
  39. private AutoCommand command;
  40. private AutoCommand command1;
  41. private AutoCommand command2;
  42. private AutoCommand command3;
  43. private AutoCommand command4;
  44. @Before
  45. public void setup() throws IOException {
  46. fs = Jimfs.newFileSystem(Configuration.unix());
  47. Files.copy(getClass().getResource("readtest.yml").openStream(), fs.getPath("readtest.yml"));
  48. command = new AutoCommand(Optional.fromNullable("server"),
  49. Optional.fromNullable("network"),
  50. Optional.fromNullable("profile"),
  51. "command");
  52. command1 = new AutoCommand(Optional.fromNullable("server1"),
  53. Optional.fromNullable("network1"),
  54. Optional.fromNullable("profile1"),
  55. "command1");
  56. command2 = new AutoCommand(Optional.fromNullable("server2"),
  57. Optional.fromNullable("network2"),
  58. Optional.<String>absent(),
  59. "command2");
  60. command3 = new AutoCommand(Optional.<String>absent(),
  61. Optional.fromNullable("network3"),
  62. Optional.<String>absent(),
  63. "command3");
  64. command4 = new AutoCommand(Optional.<String>absent(),
  65. Optional.<String>absent(),
  66. Optional.<String>absent(),
  67. "command4");
  68. }
  69. @Test
  70. public void testReadAutoCommands() {
  71. ycs = new YamlAutoCommandStore(fs.getPath("readtest.yml"));
  72. final Set<AutoCommand> commands = ycs.readAutoCommands();
  73. assertTrue("Command 1 not present", commands.contains(command1));
  74. assertTrue("Command 2 not present", commands.contains(command2));
  75. assertTrue("Command 3 not present", commands.contains(command3));
  76. assertTrue("Command 4 not present", commands.contains(command4));
  77. }
  78. @Test
  79. public void testWriteAutoCommands() throws IOException {
  80. ycs = new YamlAutoCommandStore(fs.getPath("store.yml"));
  81. assertEquals(0, ycs.readAutoCommands().size());
  82. assertFalse(Files.exists(fs.getPath("store.yml")));
  83. ycs.writeAutoCommands(Sets.newHashSet(command));
  84. final Set<AutoCommand> commands = ycs.readAutoCommands();
  85. assertTrue("Command not present", commands.contains(command));
  86. assertTrue(Files.exists(fs.getPath("store.yml")));
  87. }
  88. }