/
BundleParserTest.java
105 lines (85 loc) · 3.95 KB
/
BundleParserTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.vaadin.flow.component.polymertemplate;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.jsoup.nodes.Element;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import elemental.json.Json;
import elemental.json.JsonObject;
import static com.vaadin.flow.server.Constants.VAADIN_SERVLET_RESOURCES;
public class BundleParserTest {
private static final String statsFile = VAADIN_SERVLET_RESOURCES
+ "config/stats.json";
private static JsonObject stats;
@BeforeClass
public static void initClass() throws IOException {
InputStream stream = BundleParserTest.class.getClassLoader()
.getResourceAsStream(statsFile);
String statsFileContents = IOUtils.toString(stream,
StandardCharsets.UTF_8);
stats = BundleParser.parseJsonStatistics(statsFileContents);
}
@Test
public void nonLocalTemplate_sourcesShouldBeFound() {
final String source = BundleParser
.getSourceFromStatistics("./src/hello-world.js", stats);
Assert.assertNotNull("Source expected in stats.json", source);
}
@Test
public void nonLocalTemplate_sourcesShouldBeFoundInTargetFolder() {
final String source = BundleParser
.getSourceFromStatistics("./src/hello-world2.js", stats);
Assert.assertNotNull("Source expected in stats.json", source);
}
@Test
public void frontendPrefix_sourcesShouldBeFound() {
final String source = BundleParser.getSourceFromStatistics(
"./frontend/src/hello-world.js", stats);
Assert.assertNotNull("Source expected in stats.json", source);
}
@Test
public void typeScriptExtension_sourcesShouldBeFound() {
final String source = BundleParser
.getSourceFromStatistics("./frontend/my-form.ts", stats);
Assert.assertNotNull("TypeScript sources expected in stats.json",
source);
}
@Test
public void frontendProtocol_sourcesShouldBeFound() {
final String source = BundleParser.getSourceFromStatistics(
"frontend:///src/hello-world.js", stats);
Assert.assertNotNull("Source expected in stats.json", source);
}
@Test
public void startsWithSingleLetterDirector_sourcesShouldNotBeFound() {
// This test exposes a common error in String#replaceFirst (unescaped
// period in regex) in BundleParser#getSourceFromObject
final JsonObject module = Json.createObject();
module.put("name", "a/src/hello-world.js");
module.put("source", "some-source.js");
final String source = BundleParser.getSourceFromStatistics(
"a/frontend/src/hello-world.js", module);
Assert.assertNull("Source not expected in module", source);
}
@Test
public void parseTemplateElement_stringContentNotSeenAsComment() {
String source = "static get template() { return html`<vaadin-text-field label=\"Nats Url(s)\" placeholder=\"nats://server:port\" id=\"natsUrlTxt\" style=\"width:100%\"></vaadin-text-field>`;}";
Element element = BundleParser.parseTemplateElement("nats.js", source);
Element natsElement = element.getElementById("natsUrlTxt");
Assert.assertNotNull("Found element by Id", natsElement);
Assert.assertEquals("Invalid tag for element", "vaadin-text-field",
natsElement.tagName());
Assert.assertEquals(
"Parsed value for attribute 'placeholder' was wrong.",
"nats://server:port", natsElement.attr("placeholder"));
}
@Test
public void parseTemplateElement_spacesBetweenHtmlAndTick() {
String source = "static get template() { return html `<div id='bar'></div>`;}";
Element element = BundleParser.parseTemplateElement("foo.js", source);
Assert.assertNotNull(element.getElementById("bar"));
}
}