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

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