/mongodb/src/test/java/site/ycsb/db/OptionsSupportTest.java

https://github.com/sears/YCSB · Java · 184 lines · 118 code · 16 blank · 50 comment · 0 complexity · 704e49d114e0d1da7c0f01c7132e54c9 MD5 · raw file

  1. /*
  2. * Copyright (c) 2014, Yahoo!, Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you
  5. * may not use this file except in compliance with the License. You
  6. * may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. * implied. See the License for the specific language governing
  14. * permissions and limitations under the License. See accompanying
  15. * LICENSE file.
  16. */
  17. package site.ycsb.db;
  18. import static site.ycsb.db.OptionsSupport.updateUrl;
  19. import static org.hamcrest.CoreMatchers.is;
  20. import static org.junit.Assert.assertThat;
  21. import java.util.Properties;
  22. import org.junit.Test;
  23. /**
  24. * OptionsSupportTest provides tests for the OptionsSupport class.
  25. *
  26. * @author rjm
  27. */
  28. public class OptionsSupportTest {
  29. /**
  30. * Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
  31. * {@code mongodb.maxconnections}.
  32. */
  33. @Test
  34. public void testUpdateUrlMaxConnections() {
  35. assertThat(
  36. updateUrl("mongodb://locahost:27017/",
  37. props("mongodb.maxconnections", "1234")),
  38. is("mongodb://locahost:27017/?maxPoolSize=1234"));
  39. assertThat(
  40. updateUrl("mongodb://locahost:27017/?foo=bar",
  41. props("mongodb.maxconnections", "1234")),
  42. is("mongodb://locahost:27017/?foo=bar&maxPoolSize=1234"));
  43. assertThat(
  44. updateUrl("mongodb://locahost:27017/?maxPoolSize=1",
  45. props("mongodb.maxconnections", "1234")),
  46. is("mongodb://locahost:27017/?maxPoolSize=1"));
  47. assertThat(
  48. updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
  49. is("mongodb://locahost:27017/?foo=bar"));
  50. }
  51. /**
  52. * Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
  53. * {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
  54. */
  55. @Test
  56. public void testUpdateUrlWaitQueueMultiple() {
  57. assertThat(
  58. updateUrl(
  59. "mongodb://locahost:27017/",
  60. props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
  61. "1234")),
  62. is("mongodb://locahost:27017/?waitQueueMultiple=1234"));
  63. assertThat(
  64. updateUrl(
  65. "mongodb://locahost:27017/?foo=bar",
  66. props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
  67. "1234")),
  68. is("mongodb://locahost:27017/?foo=bar&waitQueueMultiple=1234"));
  69. assertThat(
  70. updateUrl(
  71. "mongodb://locahost:27017/?waitQueueMultiple=1",
  72. props("mongodb.threadsAllowedToBlockForConnectionMultiplier",
  73. "1234")), is("mongodb://locahost:27017/?waitQueueMultiple=1"));
  74. assertThat(
  75. updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
  76. is("mongodb://locahost:27017/?foo=bar"));
  77. }
  78. /**
  79. * Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
  80. * {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
  81. */
  82. @Test
  83. public void testUpdateUrlWriteConcern() {
  84. assertThat(
  85. updateUrl("mongodb://locahost:27017/",
  86. props("mongodb.writeConcern", "errors_ignored")),
  87. is("mongodb://locahost:27017/?w=0"));
  88. assertThat(
  89. updateUrl("mongodb://locahost:27017/?foo=bar",
  90. props("mongodb.writeConcern", "unacknowledged")),
  91. is("mongodb://locahost:27017/?foo=bar&w=0"));
  92. assertThat(
  93. updateUrl("mongodb://locahost:27017/?foo=bar",
  94. props("mongodb.writeConcern", "acknowledged")),
  95. is("mongodb://locahost:27017/?foo=bar&w=1"));
  96. assertThat(
  97. updateUrl("mongodb://locahost:27017/?foo=bar",
  98. props("mongodb.writeConcern", "journaled")),
  99. is("mongodb://locahost:27017/?foo=bar&journal=true&j=true"));
  100. assertThat(
  101. updateUrl("mongodb://locahost:27017/?foo=bar",
  102. props("mongodb.writeConcern", "replica_acknowledged")),
  103. is("mongodb://locahost:27017/?foo=bar&w=2"));
  104. assertThat(
  105. updateUrl("mongodb://locahost:27017/?foo=bar",
  106. props("mongodb.writeConcern", "majority")),
  107. is("mongodb://locahost:27017/?foo=bar&w=majority"));
  108. // w already exists.
  109. assertThat(
  110. updateUrl("mongodb://locahost:27017/?w=1",
  111. props("mongodb.writeConcern", "acknowledged")),
  112. is("mongodb://locahost:27017/?w=1"));
  113. // Unknown options
  114. assertThat(
  115. updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
  116. is("mongodb://locahost:27017/?foo=bar"));
  117. }
  118. /**
  119. * Test method for {@link OptionsSupport#updateUrl(String, Properties)} for
  120. * {@code mongodb.threadsAllowedToBlockForConnectionMultiplier}.
  121. */
  122. @Test
  123. public void testUpdateUrlReadPreference() {
  124. assertThat(
  125. updateUrl("mongodb://locahost:27017/",
  126. props("mongodb.readPreference", "primary")),
  127. is("mongodb://locahost:27017/?readPreference=primary"));
  128. assertThat(
  129. updateUrl("mongodb://locahost:27017/?foo=bar",
  130. props("mongodb.readPreference", "primary_preferred")),
  131. is("mongodb://locahost:27017/?foo=bar&readPreference=primaryPreferred"));
  132. assertThat(
  133. updateUrl("mongodb://locahost:27017/?foo=bar",
  134. props("mongodb.readPreference", "secondary")),
  135. is("mongodb://locahost:27017/?foo=bar&readPreference=secondary"));
  136. assertThat(
  137. updateUrl("mongodb://locahost:27017/?foo=bar",
  138. props("mongodb.readPreference", "secondary_preferred")),
  139. is("mongodb://locahost:27017/?foo=bar&readPreference=secondaryPreferred"));
  140. assertThat(
  141. updateUrl("mongodb://locahost:27017/?foo=bar",
  142. props("mongodb.readPreference", "nearest")),
  143. is("mongodb://locahost:27017/?foo=bar&readPreference=nearest"));
  144. // readPreference already exists.
  145. assertThat(
  146. updateUrl("mongodb://locahost:27017/?readPreference=primary",
  147. props("mongodb.readPreference", "secondary")),
  148. is("mongodb://locahost:27017/?readPreference=primary"));
  149. // Unknown options
  150. assertThat(
  151. updateUrl("mongodb://locahost:27017/?foo=bar", props("foo", "1234")),
  152. is("mongodb://locahost:27017/?foo=bar"));
  153. }
  154. /**
  155. * Factory method for a {@link Properties} object.
  156. *
  157. * @param key
  158. * The key for the property to set.
  159. * @param value
  160. * The value for the property to set.
  161. * @return The {@link Properties} with the property added.
  162. */
  163. private Properties props(String key, String value) {
  164. Properties props = new Properties();
  165. props.setProperty(key, value);
  166. return props;
  167. }
  168. }