001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons.domain;
019
020import static org.fcrepo.kernel.api.RdfLexicon.LDP_NAMESPACE;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertTrue;
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() {
043        final PreferTag preferTag = new PreferTag("foo=bar;");
044        assertNotNull(preferTag.getParams());
045    }
046
047    @Test
048    public void testEquals() {
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() {
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}