Skip to content

Commit

Permalink
Refractor and new scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
dinkoz committed Mar 6, 2013
1 parent dcdedc4 commit 3169ba7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
55 changes: 55 additions & 0 deletions util/src/main/java/io/jvm/Join.java
@@ -0,0 +1,55 @@
package io.jvm;

public class Join {
public static final String escaping(final String URI[], final char separator) {
String ret= "";

if (URI == null) return null;
if (URI.length==0) return "";

int charCnt = 0;

for (int i=0; i<URI.length; i++) {
if (URI[i]==null) {
throw new IllegalArgumentException("URI["+ i +"] cannot contain null string!");
}else {
for (int j=0; j<URI[i].length(); j++) {
if (URI[i].charAt(j)==separator) {
//separator found
charCnt+=2;
}else {
charCnt++;
}

}
}
}

char[] allChars = new char[charCnt];



return ret;
}

public static final String escapingLegacy(final String uris[], final char separator) {
String ret= "";

if (uris == null) return null;
if (uris.length==0) return "";

final String searchStr= String.valueOf(separator);
final String replaceStr = searchStr+separator;

boolean existsBefore=false;

for (int i=0; i<uris.length; i++) {
if (uris[i]!=null) {
ret+= (existsBefore ? separator : "") + uris[i].replace(searchStr, replaceStr);
existsBefore=true;
}
}

return ret;
}
}
44 changes: 44 additions & 0 deletions util/src/test/scala/io/jvm/test/QuoteFeatureSpec.scala
Expand Up @@ -145,4 +145,48 @@ class QuoteFeatureSpec extends FeatureSpec with GivenWhenThen with MustMatchers
escape("""It's '\OK\'!""", '\'', '\\') must equal ("""'It\'s \'\\OK\\\'!'""") escape("""It's '\OK\'!""", '\'', '\\') must equal ("""'It\'s \'\\OK\\\'!'""")
} }
} }



feature("jvm.io.Join"){

def testBuildURI(uris: Array[String], separator: Character) =
uris.map(_ replace(separator.toString, separator.toString * 2)).mkString(separator.toString)

scenario("Test 1 : null array argument") {
Join.escaping(null, 'x') must equal (null)
}

scenario("Test 2.1 : array argunent contains null") {
Join.escaping(Array(null, "fx"), 'x') must equal ("""fxx""")
}

scenario("Test 2.2 : array argunent contains multiple nulls") {
Join.escaping(Array(null, "ax", null, "bx", null, "cx", "dx", null), 'x') must equal ("""axxxbxxxcxxxdxx""")
}

scenario("Test 3 : array argument does not contain separator") {
Join.escaping(Array("ab", "f"), 'x') must equal ("""abxf""")
}

scenario("Test 4 : array argument size = 0") {
Join.escaping(Array(), 'x') must equal ("""""")
}

scenario("Test 5 : normal array argument") {
Join.escaping(Array("axb", "fx"), 'x') must equal ("""axxbxfxx""")
}

scenario("Test 6 : array argument contains empty string") {
Join.escaping(Array("", "fx"), 'x') must equal ("""xfxx""")
}

scenario("Joining with a regular expression special character") {
Given("a string consisting of some special characters")
When("it is escaped via that character")
Then("the special characters must be duplicated")
Join.escaping(Array("a+b","c+d"), '+') must equal ("""a++b+c++d""")
}
}

} }

0 comments on commit 3169ba7

Please sign in to comment.