ConstantPool { public T valueOf(Class<?> firstNameComponent, String secondNameComponent) { if (firstNameComponent == null) { throw new NullPointerException("firstNameComponent"); } if (secondNameComponent == null) { throw new NullPointerException("secondNameComponent"); } return valueOf(firstNameComponent.getName() + '#' + secondNameComponent); }  T valueOf(Class<?> firstNameComponent, String secondNameComponent); T valueOf(String name); boolean exists(String name); T newInstance(String name); @Deprecated final int nextId();  }
@Test(expected = NullPointerException.class) public void testCannotProvideNullName() { pool.valueOf(null); }
@Test @SuppressWarnings("RedundantStringConstructorCall") public void testUniqueness() { TestConstant a = pool.valueOf(new String("Leroy")); TestConstant b = pool.valueOf(new String("Leroy")); assertThat(a, is(sameInstance(b))); }
@Test public void testIdUniqueness() { TestConstant one = pool.valueOf("one"); TestConstant two = pool.valueOf("two"); assertThat(one.id(), is(not(two.id()))); }
@Test public void testCompare() { TestConstant a = pool.valueOf("a_alpha"); TestConstant b = pool.valueOf("b_beta"); TestConstant c = pool.valueOf("c_gamma"); TestConstant d = pool.valueOf("d_delta"); TestConstant e = pool.valueOf("e_epsilon"); Set<TestConstant> set = new TreeSet<TestConstant>(); set.add(b); set.add(c); set.add(e); set.add(d); set.add(a); TestConstant[] array = set.toArray(new TestConstant[5]); assertThat(array.length, is(5)); Arrays.sort(array, new Comparator<TestConstant>() { @Override public int compare(TestConstant o1, TestConstant o2) { return o1.name().compareTo(o2.name()); } }); assertThat(array[0], is(sameInstance(a))); assertThat(array[1], is(sameInstance(b))); assertThat(array[2], is(sameInstance(c))); assertThat(array[3], is(sameInstance(d))); assertThat(array[4], is(sameInstance(e))); }
@Test public void testComposedName() { TestConstant a = pool.valueOf(Object.class, "A"); assertThat(a.name(), is("java.lang.Object#A")); }