/* * ModeShape (http://www.modeshape.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.modeshape.jcr.value.basic; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import java.io.ByteArrayInputStream; import java.math.BigDecimal; import java.net.URI; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import java.util.List; import org.junit.Before; import org.junit.Test; import org.modeshape.jcr.value.Name; import org.modeshape.jcr.value.Path; import org.modeshape.jcr.value.Reference; import org.modeshape.jcr.value.ValueFormatException; /** * @author Randall Hauch * @author John Verhaeg */ public class LongValueFactoryTest extends BaseValueFactoryTest { private LongValueFactory factory; @Before @Override public void beforeEach() { super.beforeEach(); factory = new LongValueFactory(Path.URL_DECODER, valueFactories); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateDoubleFromBooleanValue() { factory.create(true); } @Test public void shouldCreateLongFromString() { assertThat(factory.create("1"), is(Long.valueOf(1))); assertThat(factory.create("-10"), is(Long.valueOf(-10))); assertThat(factory.create("100000101"), is(Long.valueOf(100000101))); } @Test public void shouldCreateLongFromStringRegardlessOfLeadingAndTrailingWhitespace() { assertThat(factory.create(" 1 "), is(Long.valueOf(1))); assertThat(factory.create(" -10 "), is(Long.valueOf(-10))); assertThat(factory.create(" 100000101 "), is(Long.valueOf(100000101))); } @Test public void shouldNotCreateLongFromIntegerValue() { assertThat(factory.create(1), is(1l)); } @Test public void shouldNotCreateLongFromLongValue() { assertThat(factory.create(1l), is(1l)); } @Test public void shouldNotCreateLongFromFloatValue() { assertThat(factory.create(1.0f), is(1l)); assertThat(factory.create(1.023f), is(1l)); assertThat(factory.create(1.923f), is(1l)); } @Test public void shouldNotCreateLongFromDoubleValue() { assertThat(factory.create(1.0122d), is(1l)); } @Test public void shouldCreateLongFromBigDecimal() { BigDecimal value = new BigDecimal(100); assertThat(factory.create(value), is(value.longValue())); } @Test public void shouldCreateLongFromDate() { Date value = new Date(); assertThat(factory.create(value), is(Long.valueOf(value.getTime()))); } @Test public void shouldCreateLongFromCalendar() { Calendar value = Calendar.getInstance(); assertThat(factory.create(value), is(Long.valueOf(value.getTimeInMillis()))); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromName() { factory.create(mock(Name.class)); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromPath() { factory.create(mock(Path.class)); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromReference() { factory.create(mock(Reference.class)); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromUri() throws Exception { factory.create(new URI("http://www.jboss.org")); } @Test public void shouldCreateLongFromByteArrayContainingUtf8EncodingOfStringWithLong() throws Exception { assertThat(factory.create("0".getBytes("UTF-8")), is(0l)); assertThat(factory.create("10".getBytes("UTF-8")), is(10l)); assertThat(factory.create("-103".getBytes("UTF-8")), is(-103l)); assertThat(factory.create("1003044".getBytes("UTF-8")), is(1003044l)); } @Test public void shouldCreateLongFromInputStreamContainingUtf8EncodingOfStringWithLong() throws Exception { assertThat(factory.create(new ByteArrayInputStream("0".getBytes("UTF-8"))), is(0l)); assertThat(factory.create(new ByteArrayInputStream("10".getBytes("UTF-8"))), is(10l)); assertThat(factory.create(new ByteArrayInputStream("-103".getBytes("UTF-8"))), is(-103l)); assertThat(factory.create(new ByteArrayInputStream("1003044".getBytes("UTF-8"))), is(1003044l)); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromByteArrayContainingUtf8EncodingOfStringWithContentsOtherThanLong() throws Exception { factory.create("something".getBytes("UTF-8")); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromInputStreamContainingUtf8EncodingOfStringWithContentsOtherThanLong() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); } @Test( expected = ValueFormatException.class ) public void shouldNotCreateLongFromReaderContainingStringWithContentsOtherThanLong() throws Exception { factory.create(new ByteArrayInputStream("something".getBytes("UTF-8"))); } @Test public void shouldCreateIteratorOverValuesWhenSuppliedIteratorOfUnknownObjects() { List<String> values = new ArrayList<String>(); for (int i = 0; i != 10; ++i) values.add(" " + i); Iterator<Long> iter = factory.create(values.iterator()); Iterator<String> valueIter = values.iterator(); while (iter.hasNext()) { assertThat(iter.next(), is(factory.create(valueIter.next()))); } } }