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 <laszlo.hornyak@gmail.com>
This commit is contained in:
Laszlo Hornyak 2014-02-09 21:57:07 +01:00
parent 8d801bffab
commit 8ed9162de7
2 changed files with 38 additions and 8 deletions

View File

@ -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

View File

@ -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"));
}
}