/collection-plugins/mongodb/src/test/java/com/springsource/insight/plugin/mongodb/MongoArgumentUtilsTest.java

https://github.com/spring-projects/spring-insight-plugins · Java · 123 lines · 88 code · 20 blank · 15 comment · 0 complexity · 342babdfd2e061ced4ffecd7ff601a4f MD5 · raw file

  1. /**
  2. * Copyright (c) 2009-2011 VMware, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You 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 implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.springsource.insight.plugin.mongodb;
  17. import java.math.BigDecimal;
  18. import java.math.BigInteger;
  19. import java.util.Random;
  20. import org.bson.types.ObjectId;
  21. import org.junit.Test;
  22. import com.springsource.insight.collection.test.AbstractCollectionTestSupport;
  23. import com.springsource.insight.util.StringFormatterUtils;
  24. import com.springsource.insight.util.StringUtil;
  25. public class MongoArgumentUtilsTest extends AbstractCollectionTestSupport {
  26. public MongoArgumentUtilsTest() {
  27. super();
  28. }
  29. @Test
  30. public void testLongStringTruncatedToLimit() {
  31. String bigRandom = new BigInteger(2048, new Random()).toString(32);
  32. String argVal = MongoArgumentUtils.toString(bigRandom);
  33. assertEquals("Mismatched result length", MongoArgumentUtils.MAX_STRING_LENGTH, StringUtil.getSafeLength(argVal));
  34. assertTrue("Not ellipsified", argVal.endsWith(StringUtil.ELLIPSIS));
  35. }
  36. @Test
  37. public void testNullToString() {
  38. assertEquals(StringFormatterUtils.NULL_VALUE_STRING, MongoArgumentUtils.toString((Object) null));
  39. }
  40. @Test
  41. public void testStringValue() {
  42. final String value = "Slartibartfast";
  43. assertEquals(value, MongoArgumentUtils.toString(value));
  44. }
  45. @Test
  46. public void testBooleanValue() {
  47. assertEquals(Boolean.TRUE.toString(), MongoArgumentUtils.toString(Boolean.TRUE));
  48. }
  49. @Test
  50. public void testByteValue() {
  51. Byte value = Byte.valueOf((byte) 42);
  52. assertEquals(value.toString(), MongoArgumentUtils.toString(value));
  53. }
  54. @Test
  55. public void testCharacterValue() {
  56. Character value = Character.valueOf('A');
  57. assertEquals(value.toString(), MongoArgumentUtils.toString(value));
  58. }
  59. @Test
  60. public void testShortValue() {
  61. Short value = Short.valueOf((short) 42);
  62. assertEquals(value.toString(), MongoArgumentUtils.toString(value));
  63. }
  64. @Test
  65. public void testIntegerValue() {
  66. Integer value = Integer.valueOf(42);
  67. assertEquals(value.toString(), MongoArgumentUtils.toString(value));
  68. }
  69. @Test
  70. public void testLongValue() {
  71. Long value = Long.valueOf(42L);
  72. assertEquals(value.toString(), MongoArgumentUtils.toString(value));
  73. }
  74. @Test
  75. public void testFloatValue() {
  76. Float value = Float.valueOf(42.0f);
  77. assertEquals(value.toString(), MongoArgumentUtils.toString(value));
  78. }
  79. @Test
  80. public void testDoubleValue() {
  81. Double value = Double.valueOf(42.0d);
  82. assertEquals(value.toString(), MongoArgumentUtils.toString(value));
  83. }
  84. @Test
  85. public void testBigIntegerValue() {
  86. String value = "42424242";
  87. assertEquals(value, MongoArgumentUtils.toString(new BigInteger(value)));
  88. }
  89. @Test
  90. public void testBigDecimalValue() {
  91. String value = "42.424242";
  92. assertEquals(value, MongoArgumentUtils.toString(new BigDecimal(value)));
  93. }
  94. @Test
  95. public void testObjectIdValue() {
  96. String value = "0123456789abcd0123456789";
  97. assertEquals(value, MongoArgumentUtils.toString(new ObjectId(value)));
  98. }
  99. @Test
  100. public void testUnknownClass() {
  101. assertEquals(Random.class.getSimpleName(), MongoArgumentUtils.toString(new Random()));
  102. }
  103. }