Merge "Use plain bytes for rule serializers"
This commit is contained in:
committed by
Android (Google) Code Review
commit
5d8707ade7
@@ -32,7 +32,7 @@ public class RuleBinarySerializer implements RuleSerializer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serialize(List<Rule> rules, Optional<Integer> formatVersion) {
|
||||
public byte[] serialize(List<Rule> rules, Optional<Integer> formatVersion) {
|
||||
// TODO: Implement text serializer.
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public interface RuleSerializer {
|
||||
void serialize(List<Rule> rules, Optional<Integer> formatVersion, OutputStream outputStream)
|
||||
throws RuleSerializeException;
|
||||
|
||||
/** Serialize rules to a string. */
|
||||
String serialize(List<Rule> rule, Optional<Integer> formatVersion)
|
||||
/** Serialize rules to a ByteArray. */
|
||||
byte[] serialize(List<Rule> rule, Optional<Integer> formatVersion)
|
||||
throws RuleSerializeException;
|
||||
}
|
||||
|
||||
@@ -62,14 +62,14 @@ public class RuleXmlSerializer implements RuleSerializer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serialize(List<Rule> rules, Optional<Integer> formatVersion)
|
||||
public byte[] serialize(List<Rule> rules, Optional<Integer> formatVersion)
|
||||
throws RuleSerializeException {
|
||||
try {
|
||||
XmlSerializer xmlSerializer = Xml.newSerializer();
|
||||
StringWriter writer = new StringWriter();
|
||||
xmlSerializer.setOutput(writer);
|
||||
serializeRules(rules, xmlSerializer);
|
||||
return writer.toString();
|
||||
return writer.toString().getBytes(StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
throw new RuleSerializeException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.junit.runners.JUnit4;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -49,11 +50,11 @@ public class RuleXmlSerializerTest {
|
||||
RuleSerializer xmlSerializer = new RuleXmlSerializer();
|
||||
String expectedRules = "<RL />";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Collections.singletonList(rule), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -82,11 +83,11 @@ public class RuleXmlSerializerTest {
|
||||
+ "</R>"
|
||||
+ "</RL>";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Arrays.asList(rule1, rule2), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,8 +129,8 @@ public class RuleXmlSerializerTest {
|
||||
/* formatVersion= */ Optional.empty(),
|
||||
outputStream);
|
||||
|
||||
String actualRules = outputStream.toString();
|
||||
assertEquals(expectedRules, actualRules);
|
||||
byte[] actualRules = outputStream.toString().getBytes(StandardCharsets.UTF_8);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -165,11 +166,11 @@ public class RuleXmlSerializerTest {
|
||||
+ "</R>"
|
||||
+ "</RL>";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Collections.singletonList(rule), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -215,11 +216,11 @@ public class RuleXmlSerializerTest {
|
||||
+ "</R>"
|
||||
+ "</RL>";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Collections.singletonList(rule), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -265,11 +266,11 @@ public class RuleXmlSerializerTest {
|
||||
+ "</R>"
|
||||
+ "</RL>";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Collections.singletonList(rule), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -297,11 +298,11 @@ public class RuleXmlSerializerTest {
|
||||
+ "</R>"
|
||||
+ "</RL>";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Collections.singletonList(rule), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -327,11 +328,11 @@ public class RuleXmlSerializerTest {
|
||||
+ "</R>"
|
||||
+ "</RL>";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Collections.singletonList(rule), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -355,11 +356,11 @@ public class RuleXmlSerializerTest {
|
||||
+ "</R>"
|
||||
+ "</RL>";
|
||||
|
||||
String actualRules =
|
||||
byte[] actualRules =
|
||||
xmlSerializer.serialize(
|
||||
Collections.singletonList(rule), /* formatVersion= */ Optional.empty());
|
||||
|
||||
assertEquals(expectedRules, actualRules);
|
||||
assertEquals(expectedRules, new String(actualRules, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user