Created
August 6, 2014 14:13
-
-
Save jdbevan/914157cfd13245e1dd63 to your computer and use it in GitHub Desktop.
OrientDB getCollection() hotfix unit test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ut.com.jon.test.orient; | |
import com.orientechnologies.common.parser.OStringParser; | |
import org.junit.Test; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import static org.hamcrest.MatcherAssert.assertThat; | |
import static org.hamcrest.Matchers.is; | |
public class FixOrientGetCollection { | |
@Test | |
public void test(){ | |
final List<String> stringItems = new ArrayList<String>(); | |
getCollection("['f\\\'oo', 'don\\\'t can\\\'t', \"\\\"bar\\\"\", 'b\\\"a\\\'z', \"q\\\"u\\'x\"]", 0, stringItems, '[', ']', ','); | |
assertThat(getStringContent(stringItems.get(0)), is("f'oo")); | |
assertThat(getStringContent(stringItems.get(1)), is("don't can't")); | |
assertThat(getStringContent(stringItems.get(2)), is("\"bar\"")); | |
assertThat(getStringContent(stringItems.get(3)), is("b\"a\'z")); | |
assertThat(getStringContent(stringItems.get(4)), is("q\"u\'x")); | |
} | |
// com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper.java | |
public static int getCollection(final String iText, final int iStartPosition, final Collection<String> iCollection, | |
final char iCollectionBegin, final char iCollectionEnd, final char iCollectionSeparator) { | |
final StringBuilder buffer = new StringBuilder(); | |
int openPos = iText.indexOf(iCollectionBegin, iStartPosition); | |
if (openPos == -1) | |
return -1; | |
boolean escape = false; | |
int currentPos, deep; | |
int maxPos = iText.length() - 1; | |
for (currentPos = openPos + 1, deep = 1; deep > 0; currentPos++) { | |
if (currentPos > maxPos) | |
return -1; | |
char c = iText.charAt(currentPos); | |
if (buffer.length() == 0 && c == ' ') | |
continue; | |
if (c == iCollectionBegin) { | |
// BEGIN | |
buffer.append(c); | |
deep++; | |
} else if (c == iCollectionEnd) { | |
// END | |
if (deep > 1) | |
buffer.append(c); | |
deep--; | |
} else if (c == iCollectionSeparator) { | |
// SEPARATOR | |
if (deep > 1) { | |
buffer.append(c); | |
} else { | |
iCollection.add(buffer.toString().trim()); | |
buffer.setLength(0); | |
} | |
} else { | |
// COLLECT | |
if (!escape && c == '\\' && (currentPos + 1 <= maxPos)) { | |
// ESCAPE CHARS | |
final char nextChar = iText.charAt(currentPos + 1); | |
if (nextChar == 'u') { | |
currentPos = OStringParser.readUnicode(iText, currentPos + 2, buffer); | |
} else if (nextChar == 'n') { | |
buffer.append("\n"); | |
currentPos++; | |
} else if (nextChar == 'r') { | |
buffer.append("\r"); | |
currentPos++; | |
} else if (nextChar == 't') { | |
buffer.append("\t"); | |
currentPos++; | |
} else if (nextChar == 'f') { | |
buffer.append("\f"); | |
currentPos++; | |
} else | |
escape = true; | |
continue; | |
} | |
buffer.append(c); | |
escape = false; | |
} | |
} | |
if (buffer.length() > 0) | |
iCollection.add(buffer.toString().trim()); | |
return --currentPos; | |
} | |
// com.orientechnologies.common.io.OIOUtils.java | |
public static String getStringContent(final Object iValue) { | |
if (iValue == null) | |
return null; | |
final String s = iValue.toString(); | |
if (s == null) | |
return null; | |
if (s.length() > 1 | |
&& (s.charAt(0) == '\'' && s.charAt(s.length() - 1) == '\'' || s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"')) | |
return s.substring(1, s.length() - 1); | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WDYT about sending a Pull Request against 1.7.8 branch?