From fc5d50bf1854d3b23cf3ad4c3c33e285525db789 Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Wed, 22 Jul 2009 19:36:59 +0000 Subject: [PATCH] OPWNJPA-1191 Adding messages exceptions thrown by TupleImpl git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@796854 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/persistence/TupleImpl.java | 44 ++++++++++++------- .../openjpa/persistence/localizer.properties | 7 ++- .../openjpa/persistence/TestTupleImpl.java | 3 +- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/TupleImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/TupleImpl.java index 86e86d2a0..8f42ccdb7 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/TupleImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/TupleImpl.java @@ -25,7 +25,11 @@ import java.util.List; import javax.persistence.Tuple; import javax.persistence.TupleElement; +import org.apache.openjpa.kernel.ExpressionStoreQuery; +import org.apache.openjpa.lib.util.Localizer; + public class TupleImpl implements Tuple { + private static final Localizer _loc = Localizer.forPackage(TupleImpl.class); List> elements = new ArrayList>(); /** @@ -39,9 +43,10 @@ public class TupleImpl implements Tuple { */ public X get(TupleElement tupleElement) { if (!elements.contains(tupleElement)) { - throw new IllegalArgumentException("tupleElement was not found in this tuple"); // TODO MDD improve + throw new IllegalArgumentException(_loc.get( + "tuple-element-not-found", + new Object[] { tupleElement, elements }).getMessage()); } - TupleElementImpl impl = (TupleElementImpl) tupleElement; return impl.getValue(); } @@ -61,13 +66,13 @@ public class TupleImpl implements Tuple { @SuppressWarnings("unchecked") public X get(String alias, Class type) { if (type == null) { - throw new IllegalArgumentException("Type was null"); + throw new IllegalArgumentException(_loc.get("tuple-was-null", "type").getMessage()); } Object rval = get(alias); if (!type.isAssignableFrom(rval.getClass())) { - throw new IllegalArgumentException(String.format( - "TupleElement type did not match for alias: %s. Provided type: %s actual type: %s", alias, type, rval - .getClass().toString())); + throw new IllegalArgumentException(_loc.get( + "tuple-element-wrong-type", + new Object[] { alias, type, rval.getClass() }).getMessage()); } return (X) rval; } @@ -83,14 +88,21 @@ public class TupleImpl implements Tuple { */ public Object get(String alias) { if (alias == null) { - throw new IllegalArgumentException(String.format("Alias was null.")); + // TODO MDD determine if we can support this. + throw new IllegalArgumentException(_loc.get("typle-was-null", "alias").getMessage()); } for (TupleElement te : elements) { if (alias.equals(te.getAlias())) { return ((TupleElementImpl) te).getValue(); } } - throw new IllegalArgumentException(String.format("Alias %s was not found.", alias)); + + List knownAliases = new ArrayList(); + for(TupleElement te : elements) { + knownAliases.add(te.getAlias()); + } + throw new IllegalArgumentException(_loc.get("tuple-alias-not-found", + new Object[] { alias, knownAliases }).getMessage()); } /** @@ -107,13 +119,13 @@ public class TupleImpl implements Tuple { @SuppressWarnings("unchecked") public X get(int i, Class type) { if (type == null) { - throw new IllegalArgumentException("Type was null"); + throw new IllegalArgumentException(_loc.get("tuple-was-null", "type").getMessage()); } Object rval = get(i); if(! type.isAssignableFrom(rval.getClass())) { - throw new IllegalArgumentException(String.format( - "Type did not match for position: %d. Provided type: %s actual type: %s", i, type.getClass(), - rval.getClass())); + throw new IllegalArgumentException(_loc.get( + "tuple-element-wrong-type", + new Object[] { "position", i, type, type.getClass() }).getMessage()); } return (X) rval; } @@ -129,11 +141,11 @@ public class TupleImpl implements Tuple { */ public Object get(int i) { if (i > elements.size()) { - throw new IllegalArgumentException(String.format( - "Attempt to read TupleElement %d when there are only %d elements available", i, elements.size())); + throw new IllegalArgumentException(_loc.get("tuple-exceeded-size", + new Object[] { i, elements.size() }).getMessage()); } - if (i == -1) { - throw new IllegalArgumentException("Cannot obtain the -1th element in this tuple. Thank you for playing"); + if (i <= -1) { + throw new IllegalArgumentException(_loc.get("tuple-stop-thinking-in-python").getMessage()); } return toArray()[i]; } diff --git a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties index 7a939bd9d..d8736ce33 100644 --- a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties +++ b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties @@ -198,4 +198,9 @@ dynamic-agent: OpenJPA dynamically loaded the class enhancer. Any classes \ loaded by the JVM. vlem-creation-info: OpenJPA dynamically loaded a validation provider. no-embeddable-metadata: Unable to load metadata for embeddable class "{0}". - +tuple-element-not-found: TupleElement "{0}" "{1}" was not found in this Tuple. Contents of the Tuple: {2}. +tuple-was-null: Input argument {0} was null. Unable to proceed. +tuple-element-wrong-type: TupleElement type did not match for {0} "{1}". Provided type "{2}" , actual type "{3}" +tuple-alias-not-found: Alias "{0}" was not found in this tuple. Aliases found : "{1}" +tuple-exceeded-size : Attempt to read TupleElement {0} when there are only {1} elements available +tuple-stop-thinking-in-python: Currently we do not support negative indexes into a Tuple. diff --git a/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestTupleImpl.java b/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestTupleImpl.java index bbad26a18..01bc89d45 100644 --- a/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestTupleImpl.java +++ b/openjpa-persistence/src/test/java/org/apache/openjpa/persistence/TestTupleImpl.java @@ -21,10 +21,11 @@ package org.apache.openjpa.persistence; import java.util.List; -import javax.persistence.Entity; import javax.persistence.Tuple; import javax.persistence.TupleElement; +import org.apache.openjpa.lib.util.Localizer; + import junit.framework.TestCase; /**