cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject git commit: updated refs/heads/master to 13e7a73
Date Mon, 30 Sep 2013 13:06:13 GMT
Updated Branches:
  refs/heads/master c61997f02 -> 13e7a7308


Test for URLEncoder

- test added
- source formatted

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/13e7a730
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/13e7a730
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/13e7a730

Branch: refs/heads/master
Commit: 13e7a7308b75bfccd1f3fb9686244ad2129ef7f0
Parents: c61997f
Author: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Authored: Sun Sep 29 15:25:53 2013 +0200
Committer: Daan Hoogland <dhoogland@schubergphilis.com>
Committed: Mon Sep 30 15:03:12 2013 +0200

----------------------------------------------------------------------
 .../com/cloud/utils/encoding/URLEncoder.java    | 44 +++++++++-----------
 .../cloud/utils/encoding/UrlEncoderTest.java    | 14 +++++++
 2 files changed, 34 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/13e7a730/utils/src/com/cloud/utils/encoding/URLEncoder.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/encoding/URLEncoder.java b/utils/src/com/cloud/utils/encoding/URLEncoder.java
index 30deb02..8c9e35c 100644
--- a/utils/src/com/cloud/utils/encoding/URLEncoder.java
+++ b/utils/src/com/cloud/utils/encoding/URLEncoder.java
@@ -34,19 +34,16 @@ import java.util.BitSet;
  * @author Remy Maucherat
  */
 
-
-
-
 public class URLEncoder {
     protected static final char[] hexadecimal = { '0', '1', '2', '3',
         '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
 
     static CharsetEncoder asciiEncoder = 
         Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1
-    
+
     //Array containing the safe characters set.
     protected BitSet safeCharacters = new BitSet(256);
-    
+
     public URLEncoder() {
         for (char i = 'a'; i <= 'z'; i++) {
             addSafeCharacter(i);
@@ -59,15 +56,14 @@ public class URLEncoder {
         }
     }
     
-    public void addSafeCharacter(char c) {
+    private void addSafeCharacter(char c) {
         safeCharacters.set(c);
     }
-    
+
     public String encode(String path) {
         int maxBytesPerChar = 10;
         StringBuffer rewrittenPath = new StringBuffer(path.length());
-        ByteArrayOutputStream buf = new ByteArrayOutputStream(
-                maxBytesPerChar);
+        ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
         OutputStreamWriter writer = null;
         try {
             writer = new OutputStreamWriter(buf, "UTF8");
@@ -75,10 +71,11 @@ public class URLEncoder {
             e.printStackTrace();
             writer = new OutputStreamWriter(buf);
         }
-    
+
         for (int i = 0; i < path.length(); i++) {
             int c = (int) path.charAt(i);
-            //NOTICE - !isPureAscii(path.charAt(i)) check was added by CloudStack
+            // NOTICE - !isPureAscii(path.charAt(i)) check was added by
+            // CloudStack
             if (safeCharacters.get(c) || !isPureAscii(path.charAt(i))) {
                 rewrittenPath.append((char) c);
             } else {
@@ -95,20 +92,19 @@ public class URLEncoder {
                     // Converting each byte in the buffer
                     byte toEncode = ba[j];
                     rewrittenPath.append('%');
-                        int low = (int) (toEncode & 0x0f);
-                        int high = (int) ((toEncode & 0xf0) >> 4);
-                        rewrittenPath.append(hexadecimal[high]);
-                        rewrittenPath.append(hexadecimal[low]);
-                    }
-                    buf.reset();
+                    int low = (int) (toEncode & 0x0f);
+                    int high = (int) ((toEncode & 0xf0) >> 4);
+                    rewrittenPath.append(hexadecimal[high]);
+                    rewrittenPath.append(hexadecimal[low]);
                 }
+                buf.reset();
             }
-            return rewrittenPath.toString();
-        }
-        
-    
-        //NOTICE - this part was added by CloudStack
-        public static boolean isPureAscii(Character v) {
-            return asciiEncoder.canEncode(v);
         }
+        return rewrittenPath.toString();
+    }        
+
+    // NOTICE - this part was added by CloudStack
+    public static boolean isPureAscii(Character v) {
+        return asciiEncoder.canEncode(v);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/13e7a730/utils/test/com/cloud/utils/encoding/UrlEncoderTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/encoding/UrlEncoderTest.java b/utils/test/com/cloud/utils/encoding/UrlEncoderTest.java
new file mode 100644
index 0000000..322ac78
--- /dev/null
+++ b/utils/test/com/cloud/utils/encoding/UrlEncoderTest.java
@@ -0,0 +1,14 @@
+package com.cloud.utils.encoding;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class UrlEncoderTest {
+    @Test
+    public void encode() {
+        Assert.assertEquals("%2Ftmp%2F", new URLEncoder().encode("/tmp/"));
+        Assert.assertEquals("%20", new URLEncoder().encode(" "));
+        Assert.assertEquals("%5F", new URLEncoder().encode("_"));
+        Assert.assertEquals("%25", new URLEncoder().encode("%"));
+    }
+}


Mime
View raw message