You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/08/23 18:47:42 UTC
[text] Better exception messages.
Repository: commons-text
Updated Branches:
refs/heads/master 56bfa55f5 -> 915645f37
Better exception messages.
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/915645f3
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/915645f3
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/915645f3
Branch: refs/heads/master
Commit: 915645f37cfc0d2f5b8bdc7490661bace7e150a0
Parents: 56bfa55
Author: Gary Gregory <ga...@gmail.com>
Authored: Thu Aug 23 12:47:39 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Thu Aug 23 12:47:39 2018 -0600
----------------------------------------------------------------------
.../commons/text/lookup/FileStringLookup.java | 6 +-
.../text/lookup/PropertiesStringLookup.java | 4 +-
.../text/lookup/ResourceBundleStringLookup.java | 164 +++++++++----------
.../commons/text/lookup/ScriptStringLookup.java | 4 +-
.../commons/text/lookup/XmlStringLookup.java | 5 +-
.../text/lookup/FileStringLookupTest.java | 8 +
6 files changed, 100 insertions(+), 91 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/915645f3/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
index 8df227c..111ddb8 100644
--- a/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
@@ -63,15 +63,15 @@ final class FileStringLookup extends AbstractStringLookup {
final String[] keys = key.split(":");
final int keyLen = keys.length;
if (keyLen != 2) {
- throw IllegalArgumentExceptions
- .format("Bad Properties key format [%s]. Expected format is DocumentPath:Key.", key);
+ throw IllegalArgumentExceptions.format("Bad file key format [%s],expected format is DocumentPath:Key.",
+ key);
}
final String charsetName = keys[0];
final String fileName = keys[1];
try {
return new String(Files.readAllBytes(Paths.get(fileName)), charsetName);
} catch (final Exception e) {
- throw IllegalArgumentExceptions.format(e, "Error looking up File [%s] with Charset [%s].", fileName,
+ throw IllegalArgumentExceptions.format(e, "Error looking up file [%s] with charset [%s].", fileName,
charsetName);
}
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/915645f3/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
index 909943a..c94019d 100644
--- a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
@@ -65,7 +65,7 @@ final class PropertiesStringLookup extends AbstractStringLookup {
final int keyLen = keys.length;
if (keyLen != 2) {
throw IllegalArgumentExceptions
- .format("Bad Properties key format [%s]. Expected format is DocumentPath:Key.", key);
+ .format("Bad properties key format [%s]; expected format is DocumentPath:Key.", key);
}
final String documentPath = keys[0];
final String propertyKey = keys[1];
@@ -74,7 +74,7 @@ final class PropertiesStringLookup extends AbstractStringLookup {
properties.load(Files.newInputStream(Paths.get(documentPath)));
return properties.getProperty(propertyKey);
} catch (final Exception e) {
- throw IllegalArgumentExceptions.format(e, "Error looking up Properties [%s] and Key [%s].", documentPath,
+ throw IllegalArgumentExceptions.format(e, "Error looking up properties [%s] and key [%s].", documentPath,
propertyKey);
}
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/915645f3/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
index e5ae494..9e3e6d5 100644
--- a/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/ResourceBundleStringLookup.java
@@ -1,82 +1,82 @@
-/*
- * 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.commons.text.lookup;
-
-import java.util.ResourceBundle;
-
-/**
- * Looks up keys from resource bundles.
- * <p>
- * Looks up the value for a given key in the format "BundleName:BundleKey".
- * </p>
- * <p>
- * For example: "com.domain.messages:MyKey".
- * </p>
- *
- * @see ResourceBundle
- * @since 1.3
- */
-final class ResourceBundleStringLookup extends AbstractStringLookup {
-
- /**
- * Defines the singleton for this class.
- */
- static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup();
-
- /**
- * No need to build instances for now.
- */
- private ResourceBundleStringLookup() {
- // empty
- }
-
- /**
- * Looks up the value for the key in the format "BundleName:BundleKey".
- *
- * For example: "com.domain.messages:MyKey".
- *
- * @param key
- * the key to be looked up, may be null
- * @return The value associated with the key.
- * @see ResourceBundle
- * @see ResourceBundle#getBundle(String)
- * @see ResourceBundle#getString(String)
- */
- @Override
- public String lookup(final String key) {
- if (key == null) {
- return null;
- }
- final String[] keys = key.split(":");
- final int keyLen = keys.length;
- if (keyLen != 2) {
- throw IllegalArgumentExceptions
- .format("Bad ResourceBundle key format [%s]. Expected format is BundleName:KeyName.", key);
- }
- final String bundleName = keys[0];
- final String bundleKey = keys[1];
- try {
- // The ResourceBundle class caches bundles, no need to cache here.
- return ResourceBundle.getBundle(bundleName).getString(bundleKey);
- } catch (final Exception e) {
- throw IllegalArgumentExceptions.format(e, "Error looking up ResourceBundle [%s] and key [%s].", bundleName,
- bundleKey);
- }
- }
-
-}
+/*
+ * 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.commons.text.lookup;
+
+import java.util.ResourceBundle;
+
+/**
+ * Looks up keys from resource bundles.
+ * <p>
+ * Looks up the value for a given key in the format "BundleName:BundleKey".
+ * </p>
+ * <p>
+ * For example: "com.domain.messages:MyKey".
+ * </p>
+ *
+ * @see ResourceBundle
+ * @since 1.3
+ */
+final class ResourceBundleStringLookup extends AbstractStringLookup {
+
+ /**
+ * Defines the singleton for this class.
+ */
+ static final ResourceBundleStringLookup INSTANCE = new ResourceBundleStringLookup();
+
+ /**
+ * No need to build instances for now.
+ */
+ private ResourceBundleStringLookup() {
+ // empty
+ }
+
+ /**
+ * Looks up the value for the key in the format "BundleName:BundleKey".
+ *
+ * For example: "com.domain.messages:MyKey".
+ *
+ * @param key
+ * the key to be looked up, may be null
+ * @return The value associated with the key.
+ * @see ResourceBundle
+ * @see ResourceBundle#getBundle(String)
+ * @see ResourceBundle#getString(String)
+ */
+ @Override
+ public String lookup(final String key) {
+ if (key == null) {
+ return null;
+ }
+ final String[] keys = key.split(":");
+ final int keyLen = keys.length;
+ if (keyLen != 2) {
+ throw IllegalArgumentExceptions
+ .format("Bad resource bundle key format [%s]; expected format is BundleName:KeyName.", key);
+ }
+ final String bundleName = keys[0];
+ final String bundleKey = keys[1];
+ try {
+ // The ResourceBundle class caches bundles, no need to cache here.
+ return ResourceBundle.getBundle(bundleName).getString(bundleKey);
+ } catch (final Exception e) {
+ throw IllegalArgumentExceptions.format(e, "Error looking up resource bundle [%s] and key [%s].", bundleName,
+ bundleKey);
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/915645f3/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
index 7156a4c..6d6474c 100644
--- a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
@@ -66,7 +66,7 @@ final class ScriptStringLookup extends AbstractStringLookup {
final int keyLen = keys.length;
if (keyLen != 2) {
throw IllegalArgumentExceptions
- .format("Bad Properties key format [%s]. Expected format is DocumentPath:Key.", key);
+ .format("Bad script key format [%s]; expected format is DocumentPath:Key.", key);
}
final String engineName = keys[0];
final String script = keys[1];
@@ -78,7 +78,7 @@ final class ScriptStringLookup extends AbstractStringLookup {
final Object eval = scriptEngine.eval(script);
return Objects.toString(eval, null);
} catch (final Exception e) {
- throw IllegalArgumentExceptions.format(e, "Error looking up Properties [%s] and Key [%s].", engineName,
+ throw IllegalArgumentExceptions.format(e, "Error looking up script engine [%s] for script [%s].", engineName,
script);
}
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/915645f3/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java b/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
index c7a44e3..88e7a8b 100644
--- a/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
@@ -67,7 +67,7 @@ final class XmlStringLookup extends AbstractStringLookup {
final String[] keys = key.split(":");
final int keyLen = keys.length;
if (keyLen != 2) {
- throw IllegalArgumentExceptions.format("Bad XML key format [%s]. Expected format is DocumentPath:XPath.",
+ throw IllegalArgumentExceptions.format("Bad XML key format [%s]; expected format is DocumentPath:XPath.",
key);
}
final String documentPath = keys[0];
@@ -76,7 +76,8 @@ final class XmlStringLookup extends AbstractStringLookup {
return XPathFactory.newInstance().newXPath().evaluate(xpath,
new InputSource(Files.newInputStream(Paths.get(documentPath))));
} catch (final Exception e) {
- throw IllegalArgumentExceptions.format(e, "Error looking up XML [%s] and XPath [%s].", documentPath, xpath);
+ throw IllegalArgumentExceptions.format(e, "Error looking up XML document [%s] and XPath [%s].",
+ documentPath, xpath);
}
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/915645f3/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java
index e2bb555..9f51e5d 100644
--- a/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java
@@ -34,4 +34,12 @@ public class FileStringLookupTest {
FileStringLookup.INSTANCE.lookup("UTF-8:src/test/resources/document.properties"));
}
+ @Test
+ public void testUrl() throws Exception {
+ final byte[] expectedBytes = Files.readAllBytes(Paths.get("src/test/resources/document.properties"));
+ String expectedString = new String(expectedBytes, StandardCharsets.UTF_8);
+ Assertions.assertEquals(expectedString,
+ FileStringLookup.INSTANCE.lookup("UTF-8:http://google.com"));
+ }
+
}