001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.commons.domain;
017
018import static org.fcrepo.kernel.api.RdfLexicon.LDP_NAMESPACE;
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.text.ParseException;
025
026import org.junit.Test;
027
028/**
029 * @author cabeer
030 */
031public class PreferTagTest {
032
033    @Test
034    public void testEmpty() {
035        final PreferTag preferTag = PreferTag.emptyTag();
036        assertEquals("", preferTag.getTag());
037        assertEquals("", preferTag.getValue());
038        assertTrue(preferTag.getParams().isEmpty());
039    }
040
041    @Test
042    public void testTrailingSemicolon() throws ParseException {
043        final PreferTag preferTag = new PreferTag("foo=bar;");
044        assertNotNull(preferTag.getParams());
045    }
046
047    @Test
048    public void testEquals() throws ParseException {
049        final PreferTag preferTag1 = new PreferTag("handling=lenient; received=\"minimal\"");
050        final PreferTag preferTag2 = new PreferTag("handling=lenient; received=\"minimal\"");
051        final PreferTag preferTag3 = PreferTag.emptyTag();
052        assertTrue(preferTag1.equals(preferTag2));
053        assertTrue(preferTag1.equals(preferTag1));  // ensure consistency
054        assertTrue(preferTag2.equals(preferTag1));
055        assertFalse(preferTag1.equals(preferTag3));
056        assertFalse(preferTag1.equals(null));
057        assertFalse(preferTag1.equals("some string"));
058    }
059
060    @Test
061    public void testHashCode() throws ParseException {
062        doTestHashCode(new PreferTag("handling=lenient; received=\"minimal\""),
063                new PreferTag("handling=lenient; received=\"minimal\""),
064                true);
065
066        doTestHashCode(new PreferTag("handling=lenient; received=\"minimal\""),
067                new PreferTag("return=representation; include=\"" + LDP_NAMESPACE + "PreferMinimalContainer\""),
068                false);
069
070        doTestHashCode(new PreferTag("handling=lenient; received=\"minimal\""),
071                PreferTag.emptyTag(),
072                false);
073    }
074
075    private static void doTestHashCode(final PreferTag tag0, final PreferTag tag1, final boolean expectEqual)    {
076        assertEquals(expectEqual, tag0.equals(tag1));
077        assertEquals(expectEqual, tag0.hashCode() == tag1.hashCode());
078    }
079
080}