Skip to content
Permalink
Browse files

[#1350] Escape chars in DotFileFormatSimple (#1360)

fixes #1350
  • Loading branch information...
Rascat authored and galpha committed Sep 4, 2019
1 parent dd38c06 commit 9a357e754c816c784ae675b36b66c8b9b21b4d3d
@@ -127,7 +127,7 @@ private void writeEdges(GraphTransaction transaction, StringBuilder builder, Str
.append(edge.getTargetId())
.append(suffix)
.append(" [");
// write dot attributes if existent
// write dot attributes if present
writeLabel(builder, edge);
builder.append("];\n");
}
@@ -22,6 +22,8 @@
import org.gradoop.common.model.impl.properties.Property;
import org.gradoop.flink.model.impl.layouts.transactional.tuples.GraphTransaction;

import static org.apache.commons.lang3.StringEscapeUtils.escapeJava;

/**
* Converts a GraphTransaction to the following .dot format:
* <p>{@code
@@ -79,17 +81,17 @@ void writeLabel(StringBuilder builder, Element element) {
Properties properties = element.getProperties();

if (properties != null && properties.size() > 0) {
builder.append("label=\"").append(label).append("\"");
builder.append("label=\"").append(escapeJava(label)).append("\"");

for (Property property: properties) {
builder.append(",")
.append(property.getKey())
.append(escapeJava(property.getKey()))
.append("=\"")
.append(property.getValue().toString())
.append(escapeJava(property.getValue().toString()))
.append("\"");
}
} else {
builder.append("label=\"").append(label).append("\"");
builder.append("label=\"").append(escapeJava(label)).append("\"");
}
}
}
@@ -128,7 +128,7 @@ private void checkWriteOutput(List<String> lines, DOTDataSink.DotFormat format)
countSubgraphLines++;
}

if (format == DOTDataSink.DotFormat.HTML) {
if (format.equals(DOTDataSink.DotFormat.HTML)) {
int index = line.indexOf('&');
if (index != -1) {
assertTrue("HTML entity & should have been escaped", line.substring(index).startsWith("&amp;"));
@@ -54,18 +54,21 @@ public void initGraphTransactionMock() {
propertiesMap1.put("name", "Tom");
propertiesMap1.put("age", 25);
GradoopId idVertex1 = GradoopId.fromString("bbbbbbbbbbbbbbbbbbbbbbbb");
EPGMVertex vertex1 = new EPGMVertexFactory().initVertex(idVertex1, "Person", Properties.createFromMap(propertiesMap1));
EPGMVertex vertex1 = new EPGMVertexFactory().initVertex(
idVertex1, "Person", Properties.createFromMap(propertiesMap1));
// init vertex 2
Map<String, Object> propertiesMap2 = new HashMap<>();
propertiesMap2.put("lan", "EN");
GradoopId idVertex2 = GradoopId.fromString("cccccccccccccccccccccccc");
EPGMVertex vertex2 = new EPGMVertexFactory().initVertex(idVertex2, "Forum", Properties.createFromMap(propertiesMap2));
EPGMVertex vertex2 = new EPGMVertexFactory().initVertex(
idVertex2, "Forum", Properties.createFromMap(propertiesMap2));
// init vertex 3
Map<String, Object> propertiesMap3 = new HashMap<>();
propertiesMap3.put("name", "Anna");
propertiesMap3.put("age", 27);
GradoopId idVertex3 = GradoopId.fromString("dddddddddddddddddddddddd");
EPGMVertex vertex3 = new EPGMVertexFactory().initVertex(idVertex3, "Person", Properties.createFromMap(propertiesMap3));
EPGMVertex vertex3 = new EPGMVertexFactory().initVertex(
idVertex3, "Person", Properties.createFromMap(propertiesMap3));
// create vertex set
Set<EPGMVertex> vertices = new HashSet<>();
vertices.add(vertex1);
@@ -103,11 +106,54 @@ public void testFormat() {
"vddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaa [label=\"Person\",name=\"Anna\",age=\"27\"];\n" +
"vbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa [label=\"Person\",name=\"Tom\",age=\"25\"];\n" +
"vccccccccccccccccccccccccaaaaaaaaaaaaaaaaaaaaaaaa [label=\"Forum\",lan=\"EN\"];\n" +
"vddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaa->vbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa [label=\"knows\"];\n" +
"vccccccccccccccccccccccccaaaaaaaaaaaaaaaaaaaaaaaa->vddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaa [label=\"hasModerator\"];\n" +
"vbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa->vddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaa [label=\"knows\"];\n" +
"vddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaa->" +
"vbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa [label=\"knows\"];\n" +
"vccccccccccccccccccccccccaaaaaaaaaaaaaaaaaaaaaaaa->" +
"vddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaa [label=\"hasModerator\"];\n" +
"vbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa->" +
"vddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaa [label=\"knows\"];\n" +
"}\n";

assertEquals(expected, dotFileFormat.format(transaction));
}

/**
* Tests whether label and property value strings are being escaped according to java string rules.
*/
@Test
public void testStringEscaping() {
EPGMGraphHead graphHead = new EPGMGraphHeadFactory()
.initGraphHead(GradoopId.fromString("aaaaaaaaaaaaaaaaaaaaaaaa"), "graph");

String key = "Title";
String value = "Why was Wojo occasionally known to say \"I'm too lazy!\"?";

// init vertex 1
Map<String, Object> propertiesMap1 = new HashMap<>();
propertiesMap1.put(key, value);
GradoopId idVertex1 = GradoopId.fromString("bbbbbbbbbbbbbbbbbbbbbbbb");
EPGMVertex vertex1 = new EPGMVertexFactory().initVertex(
idVertex1, "weird\nlabe\"l", Properties.createFromMap(propertiesMap1));

// create vertex set
Set<EPGMVertex> vertices = new HashSet<>();
vertices.add(vertex1);
// create empty edge set
Set<EPGMEdge> edges = new HashSet<>();

GraphTransaction transactionMock = mock(GraphTransaction.class);
when(transactionMock.getGraphHead()).thenReturn(graphHead);
when(transactionMock.getVertices()).thenReturn(vertices);
when(transactionMock.getEdges()).thenReturn(edges);

DotFileFormatSimple dotFileFormatSimple = new DotFileFormatSimple(true);

String expected = "subgraph cluster_gaaaaaaaaaaaaaaaaaaaaaaaa{\n" +
"label=\"graph\";\n" +
"vbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaa [label=\"weird\\nlabe\\\"l\"," +
"Title=\"Why was Wojo occasionally known to say \\\"I'm too lazy!\\\"?\"];\n" +
"}\n";

assertEquals(expected, dotFileFormatSimple.format(transactionMock));
}
}

0 comments on commit 9a357e7

Please sign in to comment.
You can’t perform that action at this time.