1015N/A/*
1015N/A * CDDL HEADER START
1015N/A *
1015N/A * The contents of this file are subject to the terms of the
1015N/A * Common Development and Distribution License (the "License").
1015N/A * You may not use this file except in compliance with the License.
1015N/A *
1015N/A * See LICENSE.txt included in this distribution for the specific
1015N/A * language governing permissions and limitations under the License.
1015N/A *
1015N/A * When distributing Covered Code, include this CDDL HEADER in each
1015N/A * file and include the License file at LICENSE.txt.
1015N/A * If applicable, add the following below this CDDL HEADER, with the
1015N/A * fields enclosed by brackets "[]" replaced with your own identifying
1015N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1015N/A *
1015N/A * CDDL HEADER END
1015N/A */
1015N/A
1015N/A/*
1015N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
1015N/A * Use is subject to license terms.
1015N/A */
1015N/A
1015N/Apackage org.opensolaris.opengrok.util;
1015N/A
1015N/Aimport java.util.ArrayList;
1015N/Aimport java.util.Arrays;
1015N/Aimport java.util.LinkedList;
1015N/Aimport java.util.List;
1015N/Aimport org.junit.Test;
1015N/Aimport static org.junit.Assert.*;
1015N/A
1015N/A/**
1015N/A * Unit tests for the {@code Interner} class.
1015N/A */
1015N/Apublic class InternerTest {
1015N/A
1015N/A @Test
1015N/A public void testInternString() {
1015N/A String s1_1 = new String("s1");
1015N/A String s1_2 = new String("s1");
1015N/A String s1_3 = new String("s1");
1015N/A assertNotSame(s1_1, s1_2);
1015N/A assertNotSame(s1_1, s1_3);
1015N/A assertNotSame(s1_2, s1_3);
1015N/A
1015N/A String s2_1 = new String("s2");
1015N/A String s2_2 = new String("s2");
1015N/A assertNotSame(s2_1, s2_2);
1015N/A
1015N/A Interner<String> interner = new Interner<String>();
1015N/A
1015N/A String s1_1_i = interner.intern(s1_1);
1015N/A assertEquals(s1_1, s1_1_i);
1015N/A String s1_2_i = interner.intern(s1_2);
1015N/A assertEquals(s1_2, s1_2_i);
1015N/A String s1_3_i = interner.intern(s1_3);
1015N/A assertEquals(s1_3, s1_3_i);
1015N/A
1015N/A String s2_1_i = interner.intern(s2_1);
1015N/A assertEquals(s2_1, s2_1_i);
1015N/A String s2_2_i = interner.intern(s2_2);
1015N/A assertEquals(s2_2, s2_2_i);
1015N/A
1015N/A assertSame(s1_1_i, s1_2_i);
1015N/A assertSame(s1_1_i, s1_3_i);
1015N/A assertSame(s1_2_i, s1_3_i);
1015N/A
1015N/A assertSame(s2_1_i, s2_2_i);
1015N/A
1015N/A assertNotSame(s1_1_i, s2_1_i);
1015N/A assertFalse(s1_1_i.equals(s2_1_i));
1015N/A }
1015N/A
1015N/A @Test
1015N/A public void testInternList() {
1015N/A String[] array1 = {"a", "b", "c"};
1015N/A String[] array2 = {"d", "e", "f"};
1015N/A
1015N/A ArrayList<String> l1_1 = new ArrayList<String>(Arrays.asList(array1));
1015N/A LinkedList<String> l1_2 = new LinkedList<String>(Arrays.asList(array1));
1015N/A ArrayList<String> l2_1 = new ArrayList<String>(Arrays.asList(array2));
1015N/A LinkedList<String> l2_2 = new LinkedList<String>(Arrays.asList(array2));
1015N/A
1015N/A assertNotSame(l1_1, l1_2);
1015N/A assertNotSame(l2_1, l2_2);
1015N/A
1015N/A assertEquals(l1_1, l1_2);
1015N/A assertEquals(l2_1, l2_2);
1015N/A
1015N/A assertFalse(l1_1.equals(l2_1));
1015N/A
1015N/A Interner<List<String>> interner = new Interner<List<String>>();
1015N/A
1015N/A List<String> l1_1_i = interner.intern(l1_1);
1015N/A List<String> l1_2_i = interner.intern(l1_2);
1015N/A List<String> l2_1_i = interner.intern(l2_1);
1015N/A List<String> l2_2_i = interner.intern(l2_2);
1015N/A
1015N/A assertEquals(l1_1, l1_1_i);
1015N/A assertEquals(l1_2, l1_2_i);
1015N/A assertEquals(l2_1, l2_1_i);
1015N/A assertEquals(l2_2, l2_2_i);
1015N/A
1015N/A assertSame(l1_1_i, l1_2_i);
1015N/A assertSame(l2_1_i, l2_2_i);
1015N/A
1015N/A assertFalse(l1_1_i.equals(l2_1_i));
1015N/A }
1015N/A
1015N/A @Test
1019N/A @SuppressWarnings("unchecked")
1015N/A public void testInternNull() {
1015N/A assertNull(new Interner().intern(null));
1015N/A }
1015N/A
1015N/A @Test
1015N/A public void testInvariant() {
1015N/A Object[] s1 = {
1015N/A null, new String("1"), new String("2"), new String("3")
1015N/A };
1015N/A
1015N/A Object[] s2 = {
1015N/A null, new String("1"), new String("2"), new String("3")
1015N/A };
1015N/A
1015N/A assertEquals(s1.length, s2.length);
1015N/A
1015N/A Interner<Object> interner = new Interner<Object>();
1015N/A
1015N/A for (int i = 0; i < s1.length; i++) {
1015N/A Object o1 = s1[i];
1015N/A Object o2 = s2[i];
1015N/A
1015N/A // The javadoc for Interner.intern() mentions this invariant:
1015N/A assertTrue((o1 == null) ?
1015N/A (interner.intern(o1) == null) :
1015N/A o1.equals(o2) == (interner.intern(o1) == interner.intern(o2)));
1015N/A }
1015N/A }
1015N/A
1015N/A}