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.

SimpleInjectorTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2006-2013 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
  20. * THE SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import java.io.Serializable;
  24. import org.junit.Test;
  25. import static org.junit.Assert.*;
  26. public class SimpleInjectorTest {
  27. @Test
  28. public void testNoParams() {
  29. SimpleInjector instance = new SimpleInjector();
  30. assertTrue(instance.getParameters().isEmpty());
  31. }
  32. @Test
  33. public void testAddParams() {
  34. final Object object = new Object();
  35. SimpleInjector instance = new SimpleInjector();
  36. instance.addParameter(Object.class, object);
  37. assertEquals(1, instance.getParameters().size());
  38. }
  39. @Test
  40. public void testAddObjectsParams() {
  41. final TestObject object = new TestObject("");
  42. SimpleInjector instance = new SimpleInjector();
  43. instance.addParameter(object);
  44. assertEquals(3, instance.getParameters().size());
  45. }
  46. @Test
  47. public void testParentsParams() {
  48. final Object object = new Object();
  49. SimpleInjector parent = new SimpleInjector();
  50. parent.addParameter(Object.class, object);
  51. SimpleInjector child = new SimpleInjector(parent);
  52. assertEquals(1, child.getParameters().size());
  53. }
  54. @Test
  55. public void testCreateInstanceNoParams() {
  56. SimpleInjector instance = new SimpleInjector();
  57. final Object result = instance.createInstance(Object.class);
  58. assertNotNull(result);
  59. }
  60. @Test
  61. public void testCreateInstanceParams() {
  62. SimpleInjector instance = new SimpleInjector();
  63. instance.addParameter(String.class, "test");
  64. instance.addParameter(SimpleInjectorTest.class, this);
  65. final TestObject result = instance.createInstance(TestObject.class);
  66. assertNotNull(result);
  67. }
  68. @Test(expected=IllegalArgumentException.class)
  69. public void testCreateUnsatisfiedConstructor() {
  70. SimpleInjector instance = new SimpleInjector();
  71. final Object result = instance.createInstance(TestObject.class);
  72. assertNull(result);
  73. }
  74. @Test(expected=IllegalArgumentException.class)
  75. public void testCreatePrivateConstructor() {
  76. SimpleInjector instance = new SimpleInjector();
  77. instance.addParameter(String.class, "");
  78. final Object result = instance.createInstance(PrivateObject.class);
  79. assertNull(result);
  80. }
  81. @Test(expected=IllegalArgumentException.class)
  82. public void testCreateExceptionConstructor() {
  83. SimpleInjector instance = new SimpleInjector();
  84. instance.addParameter(String.class, "");
  85. instance.addParameter(SimpleInjectorTest.class, this);
  86. final Object result = instance.createInstance(ExceptionObject.class);
  87. assertNull(result);
  88. }
  89. class TestObject implements Serializable {
  90. public TestObject(final String test) {
  91. }
  92. }
  93. class PrivateObject implements Serializable {
  94. private PrivateObject() {
  95. }
  96. }
  97. class ExceptionObject implements Serializable {
  98. public ExceptionObject() {
  99. throw new IndexOutOfBoundsException();
  100. }
  101. }
  102. }