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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 java.io.File;
  10. import java.util.Set;
  11. import org.junit.AfterClass;
  12. import org.junit.Before;
  13. import org.junit.BeforeClass;
  14. import org.junit.Test;
  15. import static org.junit.Assert.*;
  16. /**
  17. *
  18. * @author chris
  19. */
  20. public class FormatterTest {
  21. public FormatterTest() {
  22. }
  23. @BeforeClass
  24. public static void setUpClass() throws Exception {
  25. }
  26. @AfterClass
  27. public static void tearDownClass() throws Exception {
  28. }
  29. @Before
  30. public void setUp() throws Exception {
  31. }
  32. @Test
  33. public void formatMessage() {
  34. Formatter.registerDefault("unitTest", "abc %2$s %1$s def");
  35. // Standard format test
  36. assertEquals(Formatter.formatMessage("unitTest", "123", "456"), "abc 456 123 def");
  37. // Check unknown formats
  38. assertTrue(Formatter.formatMessage("unitTest123", "m").indexOf("No format string") > -1);
  39. Formatter.registerDefault("unitTest2", "abc %2$$$ZAS %1$s def");
  40. // And invalid formats
  41. assertTrue(Formatter.formatMessage("unitTest2", "m").indexOf("Invalid format string") > -1);
  42. }
  43. @Test
  44. public void getFormats() {
  45. final Set<String> s1 = Formatter.getFormats();
  46. Formatter.registerDefault("unitTest3", "abc");
  47. final Set<String> s2 = Formatter.getFormats();
  48. final Set<String> s3 = Formatter.getFormats();
  49. assertEquals(s2, s3);
  50. assertTrue(s1.size() + 1 == s2.size());
  51. }
  52. @Test
  53. public void hasFormat() {
  54. final String[] targets = new String[]{"unknown", "abc", "def", "unittestfail"};
  55. for (String target : targets) {
  56. assertFalse(Formatter.hasFormat(target));
  57. }
  58. for (String target : Formatter.getFormats()) {
  59. assertTrue(Formatter.hasFormat(target));
  60. }
  61. }
  62. @Test
  63. public void registerDefault() {
  64. }
  65. @Test
  66. public void saveAndLoad() {
  67. Formatter.registerDefault("unitTest_saveLoad", "");
  68. final String fileName = "unittest_formatter";
  69. final File file = new File(Config.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 reload() {
  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. }