diff options
| author | Brian S. O'Neill <bronee@gmail.com> | 2008-07-22 20:33:21 +0000 | 
|---|---|---|
| committer | Brian S. O'Neill <bronee@gmail.com> | 2008-07-22 20:33:21 +0000 | 
| commit | 928d131f8a1481a32fbd2657f57d28761087b30f (patch) | |
| tree | b58c57be882c08eeaaac3113c0a3bde535f3690f /src | |
| parent | 4311256bbfdcc051cc1bfc5874ea260ffe482b8e (diff) | |
Support BigDecimal and BigInteger tests with H2.
Diffstat (limited to 'src')
5 files changed, 105 insertions, 11 deletions
diff --git a/src/test/java/com/amazon/carbonado/TestStorables.java b/src/test/java/com/amazon/carbonado/TestStorables.java index a170409..d2069a3 100644 --- a/src/test/java/com/amazon/carbonado/TestStorables.java +++ b/src/test/java/com/amazon/carbonado/TestStorables.java @@ -3022,13 +3022,14 @@ public class TestStorables extends TestCase {          WithBigInteger s = storage.prepare();          s.setId(1);          BigInteger bi = new BigInteger("123456789012345678901234567890"); +        BigInteger expected = expected(bi);          s.setNumber(bi);          s.insert();          s = storage.prepare();          s.setId(1);          s.load(); -        assertEquals(bi, s.getNumber()); +        assertEquals(expected, s.getNumber());          s = storage.prepare();          s.setId(2); @@ -3046,7 +3047,7 @@ public class TestStorables extends TestCase {          s = query.with(bi).loadOne();          assertEquals(1, s.getId()); -        assertEquals(bi, s.getNumber()); +        assertEquals(expected, s.getNumber());          s = query.with(BigInteger.ZERO).tryLoadOne();          assertEquals(null, s); @@ -3057,15 +3058,19 @@ public class TestStorables extends TestCase {          s.insert();          s = query.with(BigInteger.ONE).loadOne(); -        assertEquals(BigInteger.ONE, s.getNumber()); +        assertEquals(expected(BigInteger.ONE), s.getNumber());          s = query.with(1).loadOne(); -        assertEquals(BigInteger.ONE, s.getNumber()); +        assertEquals(expected(BigInteger.ONE), s.getNumber()); +    } + +    protected BigInteger expected(BigInteger bi) { +        return bi;      }      public void test_BigDecimal() throws Exception {          BigDecimal bd = new BigDecimal("12345678901234567890.1234567890"); -        BigDecimal normalized = expectedNormalization(bd); +        BigDecimal expected = expected(bd);          Storage<WithBigDecimal> storage = getRepository().storageFor(WithBigDecimal.class); @@ -3075,12 +3080,12 @@ public class TestStorables extends TestCase {          s.insert();          // Ensure insert behaves as if Storable was reloaded. -        assertEquals(normalized, s.getNumber()); +        assertEquals(expected, s.getNumber());          s = storage.prepare();          s.setId(1);          s.load(); -        assertEquals(normalized, s.getNumber()); +        assertEquals(expected, s.getNumber());          {              s = storage.prepare(); @@ -3108,10 +3113,10 @@ public class TestStorables extends TestCase {          s = query.with(bd).loadOne();          assertEquals(1, s.getId()); -        assertEquals(normalized, s.getNumber()); +        assertEquals(expected, s.getNumber());          BigDecimal bd2 = new BigDecimal("123.0"); -        BigDecimal nm2 = expectedNormalization(bd2); +        BigDecimal nm2 = expected(bd2);          s.setNumber(bd2);          s.update(); @@ -3147,7 +3152,7 @@ public class TestStorables extends TestCase {          assertEquals(BigDecimal.ONE, s.getNumber());      } -    protected BigDecimal expectedNormalization(BigDecimal bd) { +    protected BigDecimal expected(BigDecimal bd) {          return bd.stripTrailingZeros();      } diff --git a/src/test/java/com/amazon/carbonado/repo/jdbc/H2SchemaResolver.java b/src/test/java/com/amazon/carbonado/repo/jdbc/H2SchemaResolver.java index 8eddb5b..a5ca1e7 100644 --- a/src/test/java/com/amazon/carbonado/repo/jdbc/H2SchemaResolver.java +++ b/src/test/java/com/amazon/carbonado/repo/jdbc/H2SchemaResolver.java @@ -18,6 +18,9 @@  package com.amazon.carbonado.repo.jdbc;
 +import java.math.BigDecimal;
 +import java.math.BigInteger;
 +
  import java.util.ArrayList;
  import java.util.List;
 @@ -115,6 +118,8 @@ public class H2SchemaResolver implements SchemaResolver {                  typeName = "BLOB";
              } else if (type == Clob.class) {
                  typeName = "CLOB";
 +            } else if (type == BigDecimal.class || type == BigInteger.class) {
 +                typeName = "NUMBER";
              } else {
                  return false;
              }
 diff --git a/src/test/java/com/amazon/carbonado/repo/jdbc/TestH2.java b/src/test/java/com/amazon/carbonado/repo/jdbc/TestH2.java index 2682f53..f7b5580 100644 --- a/src/test/java/com/amazon/carbonado/repo/jdbc/TestH2.java +++ b/src/test/java/com/amazon/carbonado/repo/jdbc/TestH2.java @@ -18,6 +18,9 @@  package com.amazon.carbonado.repo.jdbc;
 +import java.math.BigDecimal;
 +import java.math.BigInteger;
 +
  import java.io.*;
  import java.sql.DriverManager;
 @@ -341,7 +344,18 @@ public class TestH2 extends com.amazon.carbonado.TestStorables {      @Override
      public void test_insertLobBig() throws Exception {
 -	// Not a useful test.
 +        // Not a useful test.
 +    }
 +
 +    @Override
 +    protected BigInteger expected(BigInteger bi) {
 +        // Used to detect that BigIntegerAdapter was selected.
 +        return bi.add(BigInteger.ONE);
 +    }
 +
 +    @Override
 +    protected BigDecimal expected(BigDecimal bd) {
 +        return bd;
      }
      private RepositoryBuilder jdbcBuilder(boolean isMaster) throws RepositoryException {
 diff --git a/src/test/java/com/amazon/carbonado/stored/BigIntegerAdapter.java b/src/test/java/com/amazon/carbonado/stored/BigIntegerAdapter.java new file mode 100644 index 0000000..9ed742c --- /dev/null +++ b/src/test/java/com/amazon/carbonado/stored/BigIntegerAdapter.java @@ -0,0 +1,69 @@ +/*
 + * Copyright 2008 Amazon Technologies, Inc. or its affiliates.
 + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 + * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 + *
 + * 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 com.amazon.carbonado.stored;
 +
 +import java.math.BigDecimal;
 +import java.math.BigInteger;
 +
 +import java.lang.annotation.ElementType;
 +import java.lang.annotation.Retention;
 +import java.lang.annotation.RetentionPolicy;
 +import java.lang.annotation.Target;
 +
 +import com.amazon.carbonado.adapter.AdapterDefinition;
 +
 +/**
 + * 
 + *
 + * @author Brian S O'Neill
 + */
 +@Retention(RetentionPolicy.RUNTIME)
 +@Target(ElementType.METHOD)
 +@AdapterDefinition(storageTypePreferences=BigDecimal.class)
 +public @interface BigIntegerAdapter {
 +    public static class Adapter {
 +        /**
 +         * @param type type of object that contains the adapted property
 +         * @param propertyName name of property with adapter
 +         * @param ann specific annotation that binds to this adapter class
 +         */
 +        public Adapter(Class<?> type, String propertyName, BigIntegerAdapter ann) {
 +        }
 +
 +        public BigDecimal adaptToBigDecimal(BigInteger value) {
 +            if (value == null) {
 +                return null;
 +            }
 +            // Purposely destroy value in order for test case to detect if
 +            // adapter was selected or not.
 +            return new BigDecimal(value.add(BigInteger.ONE), 0);
 +        }
 +
 +        public BigInteger adaptToBigInteger(BigDecimal value) {
 +            if (value == null) {
 +                return null;
 +            }
 +            value = value.stripTrailingZeros();
 +            if (value.scale() != 0) {
 +                throw new IllegalArgumentException("Cannot convert to BigInteger: " + value);
 +            }
 +            return value.unscaledValue();
 +        }
 +    }
 +}
 diff --git a/src/test/java/com/amazon/carbonado/stored/WithBigInteger.java b/src/test/java/com/amazon/carbonado/stored/WithBigInteger.java index f789782..7515c5f 100644 --- a/src/test/java/com/amazon/carbonado/stored/WithBigInteger.java +++ b/src/test/java/com/amazon/carbonado/stored/WithBigInteger.java @@ -34,6 +34,7 @@ public interface WithBigInteger extends Storable {      void setId(int id);
      @Nullable
 +    @BigIntegerAdapter
      BigInteger getNumber();
      void setNumber(BigInteger number);
  }
  | 
