/** * Returns the object managed by this initializer. This implementation just * returns the object passed to the constructor. * * @return the object managed by this initializer * @throws ConcurrentException if an error occurs */ @Override public T get() throws ConcurrentException { return getObject(); }
/** * Tests the string representation if the managed object is null. */ @Test public void testToStringNull() { final String s = new ConstantInitializer<>(null).toString(); assertTrue("Object not found: " + s, s.indexOf("object = null") > 0); } }
/** * Helper method for testing equals() and hashCode(). * * @param obj the object to compare with the test instance * @param expected the expected result */ private void checkEquals(final Object obj, final boolean expected) { assertTrue("Wrong result of equals", expected == init.equals(obj)); if (obj != null) { assertTrue("Not symmetric", expected == obj.equals(init)); if (expected) { assertEquals("Different hash codes", init.hashCode(), obj.hashCode()); } } }
@Before public void setUp() throws Exception { init = new ConstantInitializer<>(VALUE); }
/** * Tests the string representation. */ @Test public void testToString() { final String s = init.toString(); final Pattern pattern = Pattern .compile("ConstantInitializer@-?\\d+ \\[ object = " + VALUE + " \\]"); assertTrue("Wrong string: " + s, pattern.matcher(s).matches()); }
/** * Tests whether get() returns the correct object. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testGet() throws ConcurrentException { assertEquals("Wrong object", VALUE, init.get()); }
/** * Tests equals() if the expected result is true. */ @Test public void testEqualsTrue() { checkEquals(init, true); ConstantInitializer<Integer> init2 = new ConstantInitializer<>( Integer.valueOf(VALUE.intValue())); checkEquals(init2, true); init = new ConstantInitializer<>(null); init2 = new ConstantInitializer<>(null); checkEquals(init2, true); }
/** * Tests equals() if the expected result is false. */ @Test public void testEqualsFalse() { ConstantInitializer<Integer> init2 = new ConstantInitializer<>( null); checkEquals(init2, false); init2 = new ConstantInitializer<>(VALUE + 1); checkEquals(init2, false); }
/** * Compares this object with another one. This implementation returns * <b>true</b> if and only if the passed in object is an instance of * {@code ConstantInitializer} which refers to an object equals to the * object managed by this instance. * * @param obj the object to compare to * @return a flag whether the objects are equal */ @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof ConstantInitializer<?>)) { return false; } final ConstantInitializer<?> c = (ConstantInitializer<?>) obj; return Objects.equals(getObject(), c.getObject()); }
/** * Tests createIfAbsentUnchecked() if no exception is thrown. */ @Test public void testCreateIfAbsentUncheckedSuccess() { final String key = "testKey"; final Integer value = 42; final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); assertEquals("Wrong result", value, ConcurrentUtils.createIfAbsentUnchecked(map, key, new ConstantInitializer<>(value))); assertEquals("Wrong value in map", value, map.get(key)); }
/** * Returns a string representation for this object. This string also * contains a string representation of the object managed by this * initializer. * * @return a string for this object */ @Override public String toString() { return String.format(FMT_TO_STRING, Integer.valueOf(System.identityHashCode(this)), String.valueOf(getObject())); } }
/** * Tests equals() with objects of other classes. */ @Test public void testEqualsWithOtherObjects() { checkEquals(null, false); checkEquals(this, false); checkEquals(new ConstantInitializer<>("Test"), false); }
/** * Tests whether the correct object is returned. */ @Test public void testGetObject() { assertEquals("Wrong object", VALUE, init.getObject()); }
/** * Returns the object managed by this initializer. This implementation just * returns the object passed to the constructor. * * @return the object managed by this initializer * @throws ConcurrentException if an error occurs */ @Override public T get() throws ConcurrentException { return getObject(); }
/** * Returns the object managed by this initializer. This implementation just * returns the object passed to the constructor. * * @return the object managed by this initializer * @throws ConcurrentException if an error occurs */ @Override public T get() throws ConcurrentException { return getObject(); }
/** * Returns the object managed by this initializer. This implementation just * returns the object passed to the constructor. * * @return the object managed by this initializer * @throws ConcurrentException if an error occurs */ @Override public T get() throws ConcurrentException { return getObject(); }