[dna-commits] DNA SVN: r190 - in branches/federation/dna-spi/src: test/java/org/jboss/dna/spi/graph/impl and 1 other directory.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Fri May 23 16:44:56 EDT 2008


Author: rhauch
Date: 2008-05-23 16:44:56 -0400 (Fri, 23 May 2008)
New Revision: 190

Added:
   branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicNamespaceRegistryTest.java
   branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BooleanValueFactoryTest.java
   branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DecimalValueFactoryTest.java
   branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DoubleValueFactoryTest.java
Modified:
   branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/AbstractValueFactory.java
   branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/BooleanValueFactory.java
   branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/DecimalValueFactory.java
Log:
DNA-67: Create graph API for federation engine
http://jira.jboss.org/jira/browse/DNA-67

Created unit tests for several of the basic implementation classes, and in the process changes some of the interfaces to handle functionality that was missing or incorrect.

Added more unit tests for the basic implementations.

Modified: branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/AbstractValueFactory.java
===================================================================
--- branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/AbstractValueFactory.java	2008-05-23 19:01:39 UTC (rev 189)
+++ branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/AbstractValueFactory.java	2008-05-23 20:44:56 UTC (rev 190)
@@ -21,6 +21,9 @@
  */
 package org.jboss.dna.spi.graph.impl;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
 import java.math.BigDecimal;
 import java.net.URI;
 import java.util.Calendar;
@@ -103,6 +106,21 @@
         if (value instanceof Path) return create((Path)value);
         if (value instanceof Reference) return create((Reference)value);
         if (value instanceof URI) return create((URI)value);
+        if (value instanceof byte[]) return create((byte[])value);
+        if (value instanceof InputStream) {
+            try {
+                return create((InputStream)value, 0);
+            } catch (IOException e) {
+                throw new ValueFormatException(e);
+            }
+        }
+        if (value instanceof Reader) {
+            try {
+                return create((Reader)value, 0);
+            } catch (IOException e) {
+                throw new ValueFormatException(e);
+            }
+        }
         return create(value.toString());
     }
 

Modified: branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/BooleanValueFactory.java
===================================================================
--- branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/BooleanValueFactory.java	2008-05-23 19:01:39 UTC (rev 189)
+++ branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/BooleanValueFactory.java	2008-05-23 20:44:56 UTC (rev 190)
@@ -54,7 +54,7 @@
      */
     public Boolean create( String value ) {
         if (value == null) return null;
-        return Boolean.valueOf(value);
+        return Boolean.valueOf(value.trim());
     }
 
     /**

Modified: branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/DecimalValueFactory.java
===================================================================
--- branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/DecimalValueFactory.java	2008-05-23 19:01:39 UTC (rev 189)
+++ branches/federation/dna-spi/src/main/java/org/jboss/dna/spi/graph/impl/DecimalValueFactory.java	2008-05-23 20:44:56 UTC (rev 190)
@@ -55,7 +55,7 @@
     public BigDecimal create( String value ) throws ValueFormatException {
         if (value == null) return null;
         try {
-            return new BigDecimal(value);
+            return new BigDecimal(value.trim());
         } catch (NumberFormatException e) {
             throw new ValueFormatException(SpiI18n.errorCreatingValue.text(getPropertyType().getName(), String.class.getSimpleName(), value), e);
         }
@@ -66,7 +66,7 @@
      */
     public BigDecimal create( String value, TextEncoder decoder ) {
         // this probably doesn't really need to call the decoder, but by doing so then we don't care at all what the decoder does
-        return create(getEncoder(decoder).decode(value));
+        return create(getEncoder(decoder).decode(value.trim()));
     }
 
     /**

Added: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicNamespaceRegistryTest.java
===================================================================
--- branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicNamespaceRegistryTest.java	                        (rev 0)
+++ branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicNamespaceRegistryTest.java	2008-05-23 20:44:56 UTC (rev 190)
@@ -0,0 +1,135 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.spi.graph.impl;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Randall Hauch
+ */
+public class BasicNamespaceRegistryTest {
+
+    private String validNamespaceUri1;
+    private String validNamespaceUri2;
+    private String validNamespaceUri3;
+    private String validPrefix1;
+    private String validPrefix2;
+    private BasicNamespaceRegistry namespaceRegistry;
+
+    @Before
+    public void setUp() throws Exception {
+        namespaceRegistry = new BasicNamespaceRegistry();
+        validNamespaceUri1 = "http://www.jboss.org/dna";
+        validNamespaceUri2 = "http://acme.com/something";
+        validNamespaceUri3 = "http://www.redhat.com";
+        validPrefix1 = "dna";
+        validPrefix2 = "acme";
+    }
+
+    @Test
+    public void shouldReturnNullForNamespaceUriIfPrefixIsNotRegistered() {
+        assertThat(namespaceRegistry.getNamespaceForPrefix("notfound"), is(nullValue()));
+        // now register some namespaces ...
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register(validPrefix2, validNamespaceUri2);
+        assertThat(namespaceRegistry.getNamespaceForPrefix("notfound"), is(nullValue()));
+    }
+
+    @Test
+    public void shouldReturnNamespaceUriForPrefixThatIsRegistered() {
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register(validPrefix2, validNamespaceUri2);
+        assertThat(namespaceRegistry.getNamespaceForPrefix(validPrefix1), is(validNamespaceUri1));
+        assertThat(namespaceRegistry.getNamespaceForPrefix(validPrefix2), is(validNamespaceUri2));
+    }
+
+    @Test( expected = IllegalArgumentException.class )
+    public void shouldNotAllowNullPrefixParameterWhenGettingNamespaceUri() {
+        namespaceRegistry.getNamespaceForPrefix(null);
+    }
+
+    @Test
+    public void shouldReturnNullForPrefixIfNamespaceUriIsNotRegistered() {
+        assertThat(namespaceRegistry.getPrefixForNamespaceUri(validNamespaceUri3), is(nullValue()));
+        // now register some namespaces ...
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register(validPrefix2, validNamespaceUri2);
+        assertThat(namespaceRegistry.getPrefixForNamespaceUri(validNamespaceUri3), is(nullValue()));
+    }
+
+    @Test
+    public void shouldReturnPrefixForNamespaceUriThatIsRegistered() {
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register(validPrefix2, validNamespaceUri2);
+        assertThat(namespaceRegistry.getPrefixForNamespaceUri(validNamespaceUri1), is(validPrefix1));
+        assertThat(namespaceRegistry.getPrefixForNamespaceUri(validNamespaceUri2), is(validPrefix2));
+    }
+
+    @Test( expected = IllegalArgumentException.class )
+    public void shouldNotAllowNullNamespaceUriParameterWhenGettingPrefix() {
+        namespaceRegistry.getPrefixForNamespaceUri(null);
+    }
+
+    @Test
+    public void shouldReturnNullDefaultNamespaceUriWhenNoNamespacesAreRegistered() {
+        assertThat(namespaceRegistry.getDefaultNamespaceUri(), is(nullValue()));
+    }
+
+    @Test
+    public void shouldReturnNullDefaultNamespaceUriWhenThereAreNamespacesRegisteredButNoneRegisteredWithZeroLengthPrefix() {
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register(validPrefix2, validNamespaceUri2);
+        assertThat(namespaceRegistry.getDefaultNamespaceUri(), is(nullValue()));
+    }
+
+    @Test
+    public void shouldReturnNonNullDefaultNamespaceUriWhenThereAreNamespacesRegisteredIncludineOneWithZeroLengthPrefix() {
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register("", validNamespaceUri2);
+        assertThat(namespaceRegistry.getDefaultNamespaceUri(), is(validNamespaceUri2));
+    }
+
+    @Test
+    public void shouldNotFindRegisteredNamespaceIfNamespaceNotRegistered() {
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri1), is(false));
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri2), is(false));
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri3), is(false));
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register(validPrefix2, validNamespaceUri2);
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri1), is(true));
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri2), is(true));
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri3), is(false));
+    }
+
+    @Test
+    public void shouldFindRegisteredNamespace() {
+        namespaceRegistry.register(validPrefix1, validNamespaceUri1);
+        namespaceRegistry.register(validPrefix2, validNamespaceUri2);
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri1), is(true));
+        assertThat(namespaceRegistry.isRegisteredNamespaceUri(validNamespaceUri2), is(true));
+    }
+
+}


Property changes on: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BasicNamespaceRegistryTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BooleanValueFactoryTest.java
===================================================================
--- branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BooleanValueFactoryTest.java	                        (rev 0)
+++ branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BooleanValueFactoryTest.java	2008-05-23 20:44:56 UTC (rev 190)
@@ -0,0 +1,171 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.spi.graph.impl;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.net.URI;
+import java.util.Calendar;
+import java.util.Date;
+import org.jboss.dna.common.text.TextEncoder;
+import org.jboss.dna.spi.graph.Name;
+import org.jboss.dna.spi.graph.Path;
+import org.jboss.dna.spi.graph.Reference;
+import org.jboss.dna.spi.graph.ValueFormatException;
+import org.jmock.Mockery;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Randall Hauch
+ */
+public class BooleanValueFactoryTest {
+
+    private BooleanValueFactory factory;
+    private StringValueFactory stringFactory;
+    private Mockery context;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        TextEncoder encoder = Path.URL_ENCODER;
+        stringFactory = new StringValueFactory(encoder);
+        factory = new BooleanValueFactory(encoder, stringFactory);
+        context = new Mockery();
+    }
+
+    @Test
+    public void shouldCreateBooleanFromBoolean() {
+        assertThat(factory.create(true), is(true));
+        assertThat(factory.create(false), is(false));
+    }
+
+    @Test
+    public void shouldCreateBooleanFromTrueAndFalseStringRegardlessOfCase() {
+        assertThat(factory.create("true"), is(true));
+        assertThat(factory.create("false"), is(false));
+        assertThat(factory.create("TRUE"), is(true));
+        assertThat(factory.create("FALSE"), is(false));
+    }
+
+    @Test
+    public void shouldCreateBooleanFromTrueAndFalseStringRegardlessOfLeadingAndTrailingWhitespace() {
+        assertThat(factory.create("  true  "), is(true));
+        assertThat(factory.create("  false  "), is(false));
+        assertThat(factory.create("  TRUE  "), is(true));
+        assertThat(factory.create("  FALSE  "), is(false));
+    }
+
+    @Test
+    public void shouldCreateFalseFromStringContainingOneOrZero() {
+        assertThat(factory.create("1"), is(false));
+        assertThat(factory.create("0"), is(false));
+        assertThat(factory.create("  0  "), is(false));
+        assertThat(factory.create("  1  "), is(false));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromIntegerValue() {
+        factory.create(1);
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromLongValue() {
+        factory.create(1l);
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromFloatValue() {
+        factory.create(1.0f);
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromDoubleValue() {
+        factory.create(1.0d);
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromBigDecimal() {
+        factory.create(1.0d);
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromDate() {
+        factory.create(new Date());
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromCalendar() {
+        factory.create(Calendar.getInstance());
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromName() {
+        factory.create(context.mock(Name.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromPath() {
+        factory.create(context.mock(Path.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromReference() {
+        factory.create(context.mock(Reference.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateBooleanFromUri() throws Exception {
+        factory.create(new URI("http://www.jboss.org"));
+    }
+
+    @Test
+    public void shouldCreateBooleanFromByteArrayContainingUtf8EncodingOfTrueOrFalseStringRegardlessOfCase() throws Exception {
+        assertThat(factory.create("true".getBytes("UTF-8")), is(true));
+        assertThat(factory.create("false".getBytes("UTF-8")), is(false));
+        assertThat(factory.create("TRUE".getBytes("UTF-8")), is(true));
+        assertThat(factory.create("FALSE".getBytes("UTF-8")), is(false));
+        assertThat(factory.create("something else".getBytes("UTF-8")), is(false));
+    }
+
+    @Test
+    public void shouldCreateBooleanFromInputStreamContainingUtf8EncodingOfTrueOrFalseStringRegardlessOfCase() throws Exception {
+        assertThat(factory.create(new ByteArrayInputStream("true".getBytes("UTF-8"))), is(true));
+        assertThat(factory.create(new ByteArrayInputStream("false".getBytes("UTF-8"))), is(false));
+        assertThat(factory.create(new ByteArrayInputStream("TRUE".getBytes("UTF-8"))), is(true));
+        assertThat(factory.create(new ByteArrayInputStream("FALSE".getBytes("UTF-8"))), is(false));
+        assertThat(factory.create(new ByteArrayInputStream("something else".getBytes("UTF-8"))), is(false));
+    }
+
+    @Test
+    public void shouldCreateBooleanFromReaderContainingTrueOrFalseStringRegardlessOfCase() throws Exception {
+        assertThat(factory.create(new StringReader("true")), is(true));
+        assertThat(factory.create(new StringReader("false")), is(false));
+        assertThat(factory.create(new StringReader("TRUE")), is(true));
+        assertThat(factory.create(new StringReader("FALSE")), is(false));
+        assertThat(factory.create(new StringReader("something else")), is(false));
+    }
+}


Property changes on: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/BooleanValueFactoryTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DecimalValueFactoryTest.java
===================================================================
--- branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DecimalValueFactoryTest.java	                        (rev 0)
+++ branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DecimalValueFactoryTest.java	2008-05-23 20:44:56 UTC (rev 190)
@@ -0,0 +1,173 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.spi.graph.impl;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.math.BigDecimal;
+import java.net.URI;
+import java.util.Calendar;
+import java.util.Date;
+import org.jboss.dna.common.text.TextEncoder;
+import org.jboss.dna.spi.graph.Name;
+import org.jboss.dna.spi.graph.Path;
+import org.jboss.dna.spi.graph.Reference;
+import org.jboss.dna.spi.graph.ValueFormatException;
+import org.jmock.Mockery;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Randall Hauch
+ */
+public class DecimalValueFactoryTest {
+
+    private DecimalValueFactory factory;
+    private StringValueFactory stringFactory;
+    private Mockery context;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        TextEncoder encoder = Path.URL_ENCODER;
+        stringFactory = new StringValueFactory(encoder);
+        factory = new DecimalValueFactory(encoder, stringFactory);
+        context = new Mockery();
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromBooleanValue() {
+        factory.create(true);
+    }
+
+    @Test
+    public void shouldCreateDecimalFromString() {
+        assertThat(factory.create("1"), is(BigDecimal.valueOf(1)));
+        assertThat(factory.create("-1.0"), is(BigDecimal.valueOf(-1.0d)));
+        assertThat(factory.create("100.000101"), is(BigDecimal.valueOf(100.000101d)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromStringRegardlessOfLeadingAndTrailingWhitespace() {
+        assertThat(factory.create("  1  "), is(BigDecimal.valueOf(1)));
+        assertThat(factory.create("  -1.0  "), is(BigDecimal.valueOf(-1.0d)));
+        assertThat(factory.create("  100.000101  "), is(BigDecimal.valueOf(100.000101d)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromIntegerValue() {
+        assertThat(factory.create(1), is(BigDecimal.valueOf(1)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromLongValue() {
+        assertThat(factory.create(1l), is(BigDecimal.valueOf(1l)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromFloatValue() {
+        assertThat(factory.create(1.0f), is(BigDecimal.valueOf(1.0f)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromDoubleValue() {
+        assertThat(factory.create(1.0d), is(BigDecimal.valueOf(1.0d)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromBigDecimal() {
+        BigDecimal value = new BigDecimal(100);
+        assertThat(factory.create(value), is(value));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromDate() {
+        Date value = new Date();
+        assertThat(factory.create(value), is(BigDecimal.valueOf(value.getTime())));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromCalendar() {
+        Calendar value = Calendar.getInstance();
+        assertThat(factory.create(value), is(BigDecimal.valueOf(value.getTimeInMillis())));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromName() {
+        factory.create(context.mock(Name.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromPath() {
+        factory.create(context.mock(Path.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromReference() {
+        factory.create(context.mock(Reference.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromUri() throws Exception {
+        factory.create(new URI("http://www.jboss.org"));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromByteArrayContainingUtf8EncodingOfStringWithDecimal() throws Exception {
+        assertThat(factory.create("1".getBytes("UTF-8")), is(BigDecimal.valueOf(1l)));
+        assertThat(factory.create("-1.0".getBytes("UTF-8")), is(BigDecimal.valueOf(-1.d)));
+        assertThat(factory.create("100.000101".getBytes("UTF-8")), is(BigDecimal.valueOf(100.000101d)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromInputStreamContainingUtf8EncodingOfStringWithDecimal() throws Exception {
+        assertThat(factory.create(new ByteArrayInputStream("1".getBytes("UTF-8"))), is(BigDecimal.valueOf(1l)));
+        assertThat(factory.create(new ByteArrayInputStream("-1.0".getBytes("UTF-8"))), is(BigDecimal.valueOf(-1.d)));
+        assertThat(factory.create(new ByteArrayInputStream("100.000101".getBytes("UTF-8"))), is(BigDecimal.valueOf(100.000101d)));
+    }
+
+    @Test
+    public void shouldCreateDecimalFromReaderContainingStringWithDecimal() throws Exception {
+        assertThat(factory.create(new StringReader("1")), is(BigDecimal.valueOf(1l)));
+        assertThat(factory.create(new StringReader("-1.0")), is(BigDecimal.valueOf(-1.d)));
+        assertThat(factory.create(new StringReader("100.000101")), is(BigDecimal.valueOf(100.000101d)));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromByteArrayContainingUtf8EncodingOfStringWithContentsOtherThanDecimal() throws Exception {
+        factory.create("something".getBytes("UTF-8"));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromInputStreamContainingUtf8EncodingOfStringWithContentsOtherThanDecimal() throws Exception {
+        factory.create(new ByteArrayInputStream("something".getBytes("UTF-8")));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDecimalFromReaderContainingStringWithContentsOtherThanDecimal() throws Exception {
+        factory.create(new StringReader("something"));
+    }
+}


Property changes on: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DecimalValueFactoryTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DoubleValueFactoryTest.java
===================================================================
--- branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DoubleValueFactoryTest.java	                        (rev 0)
+++ branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DoubleValueFactoryTest.java	2008-05-23 20:44:56 UTC (rev 190)
@@ -0,0 +1,175 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.spi.graph.impl;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import java.io.ByteArrayInputStream;
+import java.math.BigDecimal;
+import java.net.URI;
+import java.util.Calendar;
+import java.util.Date;
+import org.jboss.dna.common.text.TextEncoder;
+import org.jboss.dna.spi.graph.Name;
+import org.jboss.dna.spi.graph.Path;
+import org.jboss.dna.spi.graph.Reference;
+import org.jboss.dna.spi.graph.ValueFormatException;
+import org.jmock.Mockery;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Randall Hauch
+ */
+public class DoubleValueFactoryTest {
+
+    private DoubleValueFactory factory;
+    private StringValueFactory stringFactory;
+    private Mockery context;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        TextEncoder encoder = Path.URL_ENCODER;
+        stringFactory = new StringValueFactory(encoder);
+        factory = new DoubleValueFactory(encoder, stringFactory);
+        context = new Mockery();
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromBooleanValue() {
+        factory.create(true);
+    }
+
+    @Test
+    public void shouldCreateDoubleFromString() {
+        assertThat(factory.create("1"), is(Double.valueOf(1)));
+        assertThat(factory.create("-1.0"), is(Double.valueOf(-1.0d)));
+        assertThat(factory.create("100.000101"), is(Double.valueOf(100.000101d)));
+    }
+
+    @Test
+    public void shouldCreateDoubleFromStringRegardlessOfLeadingAndTrailingWhitespace() {
+        assertThat(factory.create("  1  "), is(Double.valueOf(1)));
+        assertThat(factory.create("  -1.0  "), is(Double.valueOf(-1.0d)));
+        assertThat(factory.create("  100.000101  "), is(Double.valueOf(100.000101d)));
+    }
+
+    @Test
+    public void shouldNotCreateDoubleFromIntegerValue() {
+        assertThat(factory.create(1), is(1.0d));
+    }
+
+    @Test
+    public void shouldNotCreateDoubleFromLongValue() {
+        assertThat(factory.create(1l), is(1.0d));
+    }
+
+    @Test
+    public void shouldNotCreateDoubleFromFloatValue() {
+        assertThat(factory.create(1.0f), is(1.0d));
+    }
+
+    @Test
+    public void shouldNotCreateDoubleFromDoubleValue() {
+        assertThat(factory.create(1.0d), is(1.0d));
+    }
+
+    @Test
+    public void shouldCreateDoubleFromBigDecimal() {
+        BigDecimal value = new BigDecimal(100);
+        assertThat(factory.create(value), is(value.doubleValue()));
+    }
+
+    @Test
+    public void shouldCreateDoubleFromDate() {
+        Date value = new Date();
+        assertThat(factory.create(value), is(Double.valueOf(value.getTime())));
+    }
+
+    @Test
+    public void shouldCreateDoubleFromCalendar() {
+        Calendar value = Calendar.getInstance();
+        assertThat(factory.create(value), is(Double.valueOf(value.getTimeInMillis())));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromName() {
+        factory.create(context.mock(Name.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromPath() {
+        factory.create(context.mock(Path.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromReference() {
+        factory.create(context.mock(Reference.class));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromUri() throws Exception {
+        factory.create(new URI("http://www.jboss.org"));
+    }
+
+    @Test
+    public void shouldCreateDoubleFromByteArrayContainingUtf8EncodingOfStringWithDouble() throws Exception {
+        assertThat(factory.create("0.1".getBytes("UTF-8")), is(0.1d));
+        assertThat(factory.create("1".getBytes("UTF-8")), is(1.0d));
+        assertThat(factory.create("-1.03".getBytes("UTF-8")), is(-1.03));
+        assertThat(factory.create("1003044".getBytes("UTF-8")), is(1003044.d));
+    }
+
+    @Test
+    public void shouldCreateDoubleFromInputStreamContainingUtf8EncodingOfStringWithDouble() throws Exception {
+        assertThat(factory.create(new ByteArrayInputStream("0.1".getBytes("UTF-8"))), is(0.1d));
+        assertThat(factory.create(new ByteArrayInputStream("1".getBytes("UTF-8"))), is(1.0d));
+        assertThat(factory.create(new ByteArrayInputStream("-1.03".getBytes("UTF-8"))), is(-1.03));
+        assertThat(factory.create(new ByteArrayInputStream("1003044".getBytes("UTF-8"))), is(1003044.d));
+    }
+
+    @Test
+    public void shouldCreateDoubleFromReaderContainingStringWithDouble() throws Exception {
+        assertThat(factory.create(new ByteArrayInputStream("0.1".getBytes("UTF-8"))), is(0.1d));
+        assertThat(factory.create(new ByteArrayInputStream("1".getBytes("UTF-8"))), is(1.0d));
+        assertThat(factory.create(new ByteArrayInputStream("-1.03".getBytes("UTF-8"))), is(-1.03));
+        assertThat(factory.create(new ByteArrayInputStream("1003044".getBytes("UTF-8"))), is(1003044.d));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromByteArrayContainingUtf8EncodingOfStringWithContentsOtherThanDouble() throws Exception {
+        factory.create("something".getBytes("UTF-8"));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromInputStreamContainingUtf8EncodingOfStringWithContentsOtherThanDouble() throws Exception {
+        factory.create(new ByteArrayInputStream("something".getBytes("UTF-8")));
+    }
+
+    @Test( expected = ValueFormatException.class )
+    public void shouldNotCreateDoubleFromReaderContainingStringWithContentsOtherThanDouble() throws Exception {
+        factory.create(new ByteArrayInputStream("something".getBytes("UTF-8")));
+    }
+}


Property changes on: branches/federation/dna-spi/src/test/java/org/jboss/dna/spi/graph/impl/DoubleValueFactoryTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain




More information about the dna-commits mailing list