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.

FormatterTest.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * FormatterTest.java
  3. * JUnit 4.x based test
  4. *
  5. * Created on 16 June 2007, 22:54
  6. */
  7. package com.dmdirc.ui.messages;
  8. import com.dmdirc.Config;
  9. import com.dmdirc.config.IdentityManager;
  10. import java.io.File;
  11. import java.util.Set;
  12. import org.junit.AfterClass;
  13. import org.junit.Before;
  14. import org.junit.BeforeClass;
  15. import org.junit.Test;
  16. import static org.junit.Assert.*;
  17. /**
  18. *
  19. * @author chris
  20. */
  21. public class FormatterTest {
  22. public FormatterTest() {
  23. }
  24. @BeforeClass
  25. public static void setUpClass() throws Exception {
  26. IdentityManager.load();
  27. Config.init();
  28. }
  29. @AfterClass
  30. public static void tearDownClass() throws Exception {
  31. }
  32. @Before
  33. public void setUp() throws Exception {
  34. }
  35. @Test
  36. public void formatMessage() {
  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 getFormats() {
  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 hasFormat() {
  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 registerDefault() {
  67. }
  68. @Test
  69. public void saveAndLoad() {
  70. Formatter.registerDefault("unitTest_saveLoad", "");
  71. final String fileName = "unittest_formatter";
  72. final File file = new File(Config.getConfigDir() + fileName);
  73. if (file.exists()) {
  74. file.delete();
  75. }
  76. Formatter.saveAs(fileName);
  77. assertTrue(file.exists());
  78. Formatter.reload();
  79. Formatter.loadFile(fileName);
  80. assertTrue(Formatter.hasFormat("unitTest_saveLoad"));
  81. file.delete();
  82. }
  83. @Test
  84. public void reload() {
  85. Formatter.reload();
  86. final Set<String> s1 = Formatter.getFormats();
  87. Formatter.registerDefault("UnitTestABCDEF", "");
  88. Formatter.reload();
  89. final Set<String> s2 = Formatter.getFormats();
  90. assertEquals(s1.size(), s2.size());
  91. }
  92. }