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.junit.Assert.assertEquals;
019import static org.junit.Assert.assertFalse;
020import static org.junit.Assert.assertTrue;
021
022import org.junit.Test;
023
024/**
025 * <p>RangeTest class.</p>
026 *
027 * @author awoods
028 */
029public class RangeTest {
030    @Test
031    public void testUnboundedRange() {
032        final Range range = new Range(5);
033
034        assertEquals(5L, range.start());
035        assertEquals(-1L, range.end());
036
037    }
038    @Test
039    public void testRangeParsing() {
040        final Range range = Range.convert("bytes=50-100");
041
042        assertEquals(50L, range.start());
043        assertEquals(100L, range.end());
044        assertEquals(51L, range.size());
045        assertTrue(range.hasRange());
046
047    }
048
049    @Test
050    public void testUnboundedUpperRangeParsing() {
051        final Range range = Range.convert("bytes=50-");
052
053        assertEquals(50L, range.start());
054        assertEquals(-1L, range.end());
055        assertEquals(-1L, range.size());
056        assertTrue(range.hasRange());
057
058    }
059
060    @Test
061    public void testUnboundedLowerRangeParsing() {
062        final Range range = Range.convert("bytes=-50");
063
064        assertEquals(0L, range.start());
065        assertEquals(50L, range.end());
066        assertEquals(51L, range.size());
067        assertTrue(range.hasRange());
068
069    }
070
071    @Test
072    public void testGarbageRangeParsing() {
073        final Range range = Range.convert("something-thats-not-a-range");
074
075        assertFalse(range.hasRange());
076        assertEquals(0L, range.start());
077        assertEquals(-1L, range.end());
078        assertEquals(-1L, range.size());
079
080    }
081}