Cleanup in UriUtils.encodeURIComponent - StringBuffer replaced with StringBuilder - nullcheck of tokens array removed since String.split does not return null - unit test added
Signed-off-by: Laszlo Hornyak <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/8ed9162d Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/8ed9162d Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/8ed9162d Branch: refs/heads/master Commit: 8ed9162de7bb6d32d719c5afb11dc62296d2c45e Parents: 8d801bf Author: Laszlo Hornyak <[email protected]> Authored: Sun Feb 9 21:57:07 2014 +0100 Committer: Laszlo Hornyak <[email protected]> Committed: Sun Feb 9 21:57:07 2014 +0100 ---------------------------------------------------------------------- utils/src/com/cloud/utils/UriUtils.java | 13 ++++----- utils/test/com/cloud/utils/UriUtilsTest.java | 33 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8ed9162d/utils/src/com/cloud/utils/UriUtils.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/UriUtils.java b/utils/src/com/cloud/utils/UriUtils.java index 2e771ae..42456b5 100644 --- a/utils/src/com/cloud/utils/UriUtils.java +++ b/utils/src/com/cloud/utils/UriUtils.java @@ -94,15 +94,12 @@ public class UriUtils { if (pathStart > 0) { String[] tokens = url.substring(pathStart + 1).split("/"); - if (tokens != null) { - StringBuffer sb = new StringBuffer(); - sb.append(url.substring(0, pathStart)); - for (String token : tokens) { - sb.append("/").append(URLEncoder.encode(token)); - } - - return sb.toString(); + StringBuilder sb = new StringBuilder(url.substring(0, pathStart)); + for (String token : tokens) { + sb.append("/").append(URLEncoder.encode(token)); } + + return sb.toString(); } // no need to do URL component encoding http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8ed9162d/utils/test/com/cloud/utils/UriUtilsTest.java ---------------------------------------------------------------------- diff --git a/utils/test/com/cloud/utils/UriUtilsTest.java b/utils/test/com/cloud/utils/UriUtilsTest.java new file mode 100644 index 0000000..8474bd0 --- /dev/null +++ b/utils/test/com/cloud/utils/UriUtilsTest.java @@ -0,0 +1,33 @@ +// 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 +// 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.cloud.utils; + +import junit.framework.Assert; + +import org.junit.Test; + +public class UriUtilsTest { + @Test + public void encodeURIComponent() { + Assert.assertEquals("http://localhost", + UriUtils.encodeURIComponent("http://localhost")); + Assert.assertEquals("http://localhost/", + UriUtils.encodeURIComponent("http://localhost/")); + Assert.assertEquals("http://localhost/foo/bar", + UriUtils.encodeURIComponent("http://localhost/foo/bar")); + } +}
