-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.fungover.haze; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class RespInputParser { | ||
|
||
RespInputParser() { | ||
} | ||
private static final Logger logger = LogManager.getLogger(RespInputParser.class); | ||
|
||
public static void readInputStream(BufferedReader input, List<String> inputList, String firstReading) throws IOException { | ||
if (firstReading.startsWith("*")) { | ||
|
||
logger.debug("readInputStream: {} {} {}", input, inputList, firstReading); | ||
int size = Integer.parseInt(firstReading.substring(1)) * 2; | ||
for (int i = 0; i < size; i++) { | ||
String temp = input.readLine(); | ||
if (!temp.contains("$")) | ||
inputList.add(temp); | ||
} | ||
} else { | ||
String[] separated = firstReading.split("\\s"); | ||
inputList.addAll(Arrays.asList(separated)); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.fungover.haze; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class RespInputParserTest { | ||
RespInputParser respInputParser = new RespInputParser(); | ||
@Test | ||
void testReadInputStreamBulk() throws IOException { | ||
String inputString = "*3\r\n$5\r\nHello\r\n$5\r\nWorld\r\n$5\r\nRedis\r\n"; | ||
List<String> expectedOutput = Arrays.asList("Hello", "World", "Redis"); | ||
|
||
BufferedReader input = new BufferedReader(new StringReader(inputString)); | ||
List<String> actualOutput = new ArrayList<>(); | ||
respInputParser.readInputStream(input, actualOutput, input.readLine()); | ||
|
||
assertEquals(expectedOutput, actualOutput); | ||
} | ||
|
||
@Test | ||
void testReadInputStreamSimple() throws IOException { | ||
String inputString = "+OK\r\n"; | ||
List<String> expectedOutput = Arrays.asList("+OK"); | ||
|
||
BufferedReader input = new BufferedReader(new StringReader(inputString)); | ||
List<String> actualOutput = new ArrayList<>(); | ||
respInputParser.readInputStream(input, actualOutput, input.readLine()); | ||
|
||
assertEquals(expectedOutput, actualOutput); | ||
} | ||
|
||
@Test | ||
void testReadInputStreamError() throws IOException { | ||
String inputString = "-ERR unknown command\r\n"; | ||
List<String> expectedOutput = Arrays.asList("-ERR", "unknown", "command"); | ||
|
||
BufferedReader input = new BufferedReader(new StringReader(inputString)); | ||
List<String> actualOutput = new ArrayList<>(); | ||
respInputParser.readInputStream(input, actualOutput, input.readLine()); | ||
|
||
assertEquals(expectedOutput, actualOutput); | ||
} | ||
|
||
} |