Return-Path: X-Original-To: apmail-qpid-commits-archive@www.apache.org Delivered-To: apmail-qpid-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D735011EB7 for ; Thu, 12 Jun 2014 07:22:04 +0000 (UTC) Received: (qmail 78123 invoked by uid 500); 12 Jun 2014 07:22:04 -0000 Delivered-To: apmail-qpid-commits-archive@qpid.apache.org Received: (qmail 77416 invoked by uid 500); 12 Jun 2014 07:22:03 -0000 Mailing-List: contact commits-help@qpid.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@qpid.apache.org Delivered-To: mailing list commits@qpid.apache.org Received: (qmail 77407 invoked by uid 99); 12 Jun 2014 07:22:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Jun 2014 07:22:03 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Jun 2014 07:22:02 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B7B802388A33; Thu, 12 Jun 2014 07:21:37 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1602078 - in /qpid/trunk/qpid/java/broker-core/src: main/java/org/apache/qpid/server/model/ test/java/org/apache/qpid/server/model/ Date: Thu, 12 Jun 2014 07:21:37 -0000 To: commits@qpid.apache.org From: kwall@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140612072137.B7B802388A33@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kwall Date: Thu Jun 12 07:21:37 2014 New Revision: 1602078 URL: http://svn.apache.org/r1602078 Log: QPID-5721: Improve exception message used when String operand cannot be interpreted as JSON * Also made AVC consistently return unmodifiable when converting from a JSON string Added: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AbstractConfiguredObjectTest.java Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java?rev=1602078&r1=1602077&r2=1602078&view=diff ============================================================================== --- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java (original) +++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java Thu Jun 12 07:21:37 2014 @@ -196,16 +196,7 @@ abstract class AttributeValueConverter SET_CONVERTER = new AttributeValueConverter() { @Override @@ -233,16 +225,7 @@ abstract class AttributeValueConverter T convertFromJson(final String value, final ConfiguredObject object, final Class valueType) + { + String interpolated = AbstractConfiguredObject.interpolate(object, value); + ObjectMapper objectMapper = new ObjectMapper(); + try + { + return objectMapper.readValue(interpolated, valueType); + } + catch (IOException e) + { + throw new IllegalArgumentException("Cannot convert String '" + + value + "'" + + (value.equals(interpolated) + ? "" : (" (interpolated to '" + interpolated + "')")) + + " to a " + valueType.getSimpleName()); + } + } + static AttributeValueConverter getConverter(final Class type, final Type returnType) { if(type == String.class) @@ -408,7 +392,7 @@ abstract class AttributeValueConverter) new ConfiguredObjectConverter(type); } - throw new IllegalArgumentException("Cannot create attributes of type " + type.getName()); + throw new IllegalArgumentException("Cannot create attribute converter of type " + type.getName()); } abstract T convert(Object value, final ConfiguredObject object); Modified: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AbstractConfiguredObjectTest.java URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AbstractConfiguredObjectTest.java?rev=1602078&r1=1602077&r2=1602078&view=diff ============================================================================== --- qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AbstractConfiguredObjectTest.java (original) +++ qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AbstractConfiguredObjectTest.java Thu Jun 12 07:21:37 2014 @@ -174,7 +174,7 @@ public class AbstractConfiguredObjectTes Map attributes = new HashMap<>(); attributes.put(ConfiguredObject.NAME, objectName); - attributes.put("context", Collections.singletonMap("myReplacement", "myValue")); + attributes.put(ConfiguredObject.CONTEXT, Collections.singletonMap("myReplacement", "myValue")); attributes.put(TestRootCategory.STRING_VALUE, contextToken); TestRootCategory object1 = _model.getObjectFactory().create(TestRootCategory.class, Added: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java?rev=1602078&view=auto ============================================================================== --- qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java (added) +++ qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/AttributeValueConverterTest.java Thu Jun 12 07:21:37 2014 @@ -0,0 +1,196 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.qpid.server.model; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import static java.util.Collections.unmodifiableSet; +import static java.util.Collections.unmodifiableList; + +import static org.apache.qpid.server.model.AttributeValueConverter.getConverter; + +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import junit.framework.TestCase; + +import org.apache.qpid.server.model.testmodel.TestModel; +import org.apache.qpid.server.model.testmodel.TestRootCategory; + +public class AttributeValueConverterTest extends TestCase +{ + private final ConfiguredObjectFactory _objectFactory = TestModel.getInstance().getObjectFactory(); + private final Map _attributes = new HashMap<>(); + private final Map _context = new HashMap<>(); + + @Override + protected void setUp() throws Exception + { + super.setUp(); + + _attributes.put(ConfiguredObject.NAME, "objectName"); + _attributes.put(ConfiguredObject.CONTEXT, _context); + } + + public void testMapConverter() + { + _context.put("simpleMap", "{\"a\" : \"b\"}"); + _context.put("mapWithInterpolatedContents", "{\"${mykey}\" : \"b\"}"); + _context.put("mykey", "mykey1"); + + ConfiguredObject object = _objectFactory.create(TestRootCategory.class, _attributes); + + AttributeValueConverter mapConverter = getConverter(Map.class, Map.class); + + Map nullMap = mapConverter.convert(null, object); + assertNull(nullMap); + + Map emptyMap = mapConverter.convert("{ }", object); + assertEquals(emptyMap(), emptyMap); + + Map map = mapConverter.convert("{\"a\" : \"b\"}", object); + assertEquals(singletonMap("a", "b"), map); + + Map mapFromInterpolatedVar = mapConverter.convert("${simpleMap}", object); + assertEquals(singletonMap("a", "b"), mapFromInterpolatedVar); + + Map mapFromInterpolatedVarWithInterpolatedContents = + mapConverter.convert("${mapWithInterpolatedContents}", object); + assertEquals(singletonMap("mykey1", "b"), mapFromInterpolatedVarWithInterpolatedContents); + + try + { + mapConverter.convert("not a map", object); + fail("Exception not thrown"); + } + catch (IllegalArgumentException e) + { + // PASS + } + } + + public void testNonGenericCollectionConverter() + { + _context.put("simpleCollection", "[\"a\", \"b\"]"); + + ConfiguredObject object = _objectFactory.create(TestRootCategory.class, _attributes); + + AttributeValueConverter collectionConverter = getConverter(Collection.class, Collection.class); + + Collection nullCollection = collectionConverter.convert(null, object); + assertNull(nullCollection); + + Collection emptyCollection = collectionConverter.convert("[ ]", object); + assertTrue(emptyCollection.isEmpty()); + + Collection collection = collectionConverter.convert("[\"a\", \"b\"]", object); + assertEquals(2, collection.size()); + assertTrue(collection.contains("a")); + assertTrue(collection.contains("b")); + + Collection collectionFromInterpolatedVar = collectionConverter.convert("${simpleCollection}", object); + assertEquals(2, collectionFromInterpolatedVar.size()); + assertTrue(collectionFromInterpolatedVar.contains("a")); + assertTrue(collectionFromInterpolatedVar.contains("b")); + + try + { + collectionConverter.convert("not a collection", object); + fail("Exception not thrown"); + } + catch (IllegalArgumentException e) + { + // PASS + } + } + + public void testNonGenericListConverter() + { + _context.put("simpleList", "[\"a\", \"b\"]"); + + ConfiguredObject object = _objectFactory.create(TestRootCategory.class, _attributes); + + AttributeValueConverter listConverter = getConverter(List.class, List.class); + + List nullList = listConverter.convert(null, object); + assertNull(nullList); + + List emptyList = listConverter.convert("[ ]", object); + assertTrue(emptyList.isEmpty()); + + List expectedList = unmodifiableList(asList("a", "b")); + + List list = listConverter.convert("[\"a\", \"b\"]", object); + assertEquals(expectedList, list); + + List listFromInterpolatedVar = listConverter.convert("${simpleList}", object); + assertEquals(expectedList, listFromInterpolatedVar); + + try + { + listConverter.convert("not a list", object); + fail("Exception not thrown"); + } + catch (IllegalArgumentException e) + { + // PASS + } + } + + public void testNonGenericSetConverter() + { + _context.put("simpleSet", "[\"a\", \"b\"]"); + + ConfiguredObject object = _objectFactory.create(TestRootCategory.class, _attributes); + + AttributeValueConverter setConverter = getConverter(Set.class, Set.class);; + + Set nullSet = setConverter.convert(null, object); + assertNull(nullSet); + + Set emptySet = setConverter.convert("[ ]", object); + assertTrue(emptySet.isEmpty()); + + Set expectedSet = unmodifiableSet(new HashSet<>(asList("a", "b"))); + + Set set = setConverter.convert("[\"a\", \"b\"]", object); + assertEquals(expectedSet, set); + + Set setFromInterpolatedVar = setConverter.convert("${simpleSet}", object); + assertEquals(expectedSet, setFromInterpolatedVar); + + try + { + setConverter.convert("not a set", object); + fail("Exception not thrown"); + } + catch (IllegalArgumentException e) + { + // PASS + } + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org For additional commands, e-mail: commits-help@qpid.apache.org