Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1350] Escape chars in DotFileFormatSimple #1360

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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");
}
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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("\"");
}
}
}
Expand Up @@ -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;"));
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}