Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FormatterTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2007 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.ui.messages;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.config.IdentityManager;
  25. import java.io.File;
  26. import java.util.Set;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.junit.Assert.*;
  30. public class FormatterTest {
  31. @Before
  32. public void setUp() throws Exception {
  33. IdentityManager.load();
  34. }
  35. @Test
  36. public void testFormatMessage() {
  37. Formatter.registerDefault("unitTest", "abc %2$s %1$s def");
  38. // Standard format test
  39. assertEquals(Formatter.formatMessage("unitTest", "123", "456"), "abc 456 123 def");
  40. // Check unknown formats
  41. assertTrue(Formatter.formatMessage("unitTest123", "m").indexOf("No format string") > -1);
  42. Formatter.registerDefault("unitTest2", "abc %2$$$ZAS %1$s def");
  43. // And invalid formats
  44. assertTrue(Formatter.formatMessage("unitTest2", "m").indexOf("Invalid format string") > -1);
  45. }
  46. @Test
  47. public void testGetFormats() {
  48. final Set<String> s1 = Formatter.getFormats();
  49. Formatter.registerDefault("unitTest3", "abc");
  50. final Set<String> s2 = Formatter.getFormats();
  51. final Set<String> s3 = Formatter.getFormats();
  52. assertEquals(s2, s3);
  53. assertTrue(s1.size() + 1 == s2.size());
  54. }
  55. @Test
  56. public void testHasFormat() {
  57. final String[] targets = new String[]{"unknown", "abc", "def", "unittestfail"};
  58. for (String target : targets) {
  59. assertFalse(Formatter.hasFormat(target));
  60. }
  61. for (String target : Formatter.getFormats()) {
  62. assertTrue(Formatter.hasFormat(target));
  63. }
  64. }
  65. @Test
  66. public void testSaveAndLoad() {
  67. Formatter.registerDefault("unitTest_saveLoad", "");
  68. final String fileName = "unittest_formatter";
  69. final File file = new File(Main.getConfigDir() + fileName);
  70. if (file.exists()) {
  71. file.delete();
  72. }
  73. Formatter.saveAs(fileName);
  74. assertTrue(file.exists());
  75. Formatter.reload();
  76. Formatter.loadFile(fileName);
  77. assertTrue(Formatter.hasFormat("unitTest_saveLoad"));
  78. file.delete();
  79. }
  80. @Test
  81. public void testReload() {
  82. Formatter.reload();
  83. final Set<String> s1 = Formatter.getFormats();
  84. Formatter.registerDefault("UnitTestABCDEF", "");
  85. Formatter.reload();
  86. final Set<String> s2 = Formatter.getFormats();
  87. assertEquals(s1.size(), s2.size());
  88. }
  89. }