Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ConfigBinderImplTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.config;
  23. import com.dmdirc.config.binding.ConfigBinder;
  24. import com.dmdirc.config.binding.ConfigBinding;
  25. import com.dmdirc.config.provider.AggregateConfigProvider;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.mockito.Mock;
  30. import org.mockito.runners.MockitoJUnitRunner;
  31. import static org.junit.Assert.assertEquals;
  32. import static org.mockito.ArgumentMatchers.any;
  33. import static org.mockito.ArgumentMatchers.anyBoolean;
  34. import static org.mockito.ArgumentMatchers.eq;
  35. import static org.mockito.Mockito.when;
  36. @RunWith(MockitoJUnitRunner.class)
  37. public class ConfigBinderImplTest {
  38. @Mock private AggregateConfigProvider configProvider;
  39. private ConfigBinder binder;
  40. @Before
  41. public void setup() {
  42. binder = new ConfigBinderImpl(configProvider);
  43. }
  44. @Test
  45. public void testAppliesStringSetting() {
  46. class StringTest {
  47. @ConfigBinding(domain = "test", key="foo")
  48. public String field;
  49. public String bar;
  50. @ConfigBinding(domain = "test", key="bar")
  51. public void method(final String boo) {
  52. this.bar = boo;
  53. }
  54. }
  55. final StringTest test = new StringTest();
  56. when(configProvider.getOptionString(eq("test"), eq("foo"), anyBoolean(), any()))
  57. .thenReturn("test123");
  58. when(configProvider.getOptionString(eq("test"), eq("bar"), anyBoolean(), any()))
  59. .thenReturn("test456");
  60. binder.bind(test, StringTest.class);
  61. assertEquals("test123", test.field);
  62. assertEquals("test456", test.bar);
  63. }
  64. @Test
  65. public void testAppliesStringSettingWithDefaultDomnain() {
  66. class StringTest {
  67. @ConfigBinding(key="foo")
  68. public String field;
  69. public String bar;
  70. @ConfigBinding(key="bar")
  71. public void method(final String boo) {
  72. this.bar = boo;
  73. }
  74. }
  75. final StringTest test = new StringTest();
  76. when(configProvider.getOptionString(eq("test"), eq("foo"), anyBoolean(), any()))
  77. .thenReturn("test123");
  78. when(configProvider.getOptionString(eq("test"), eq("bar"), anyBoolean(), any()))
  79. .thenReturn("test456");
  80. binder.withDefaultDomain("test").bind(test, StringTest.class);
  81. assertEquals("test123", test.field);
  82. assertEquals("test456", test.bar);
  83. }
  84. }