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.4KB

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