Skip to content

Commit a46ca9f

Browse files
Added a test to show that errors caused by broken XML are detected as well.
1 parent 4a71738 commit a46ca9f

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

kit/src/test/java/com/oracle/javafx/scenebuilder/kit/fxom/FXOMDocumentTest.java

+82-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Gluon and/or its affiliates.
2+
* Copyright (c) 2022, Gluon and/or its affiliates.
33
* All rights reserved. Use is subject to license terms.
44
*
55
* This file is available and licensed under the following license:
@@ -31,21 +31,101 @@
3131
*/
3232
package com.oracle.javafx.scenebuilder.kit.fxom;
3333

34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNotNull;
3436
import static org.junit.Assert.assertThrows;
3537
import static org.junit.Assert.assertTrue;
3638

3739
import java.io.IOException;
3840
import java.net.URL;
3941

4042
import org.junit.Test;
43+
import org.xml.sax.SAXParseException;
4144

4245
public class FXOMDocumentTest {
4346
@Test
44-
public void that_IOexception_is_thrown_in_case_FXMLLoader_error() throws Exception {
47+
public void that_IOException_is_thrown_in_case_FXMLLoader_error() throws Exception {
4548
URL resource = getClass().getResource("BrokenByUserData.fxml");
4649
String fxmlText = FXOMDocument.readContentFromURL(resource);
4750
Throwable t = assertThrows(IOException.class, () -> new FXOMDocument(fxmlText, resource, null, null));
4851
String message = t.getMessage();
4952
assertTrue(message.startsWith("javafx.fxml.LoadException:"));
5053
}
54+
55+
@Test
56+
public void that_illegal_null_value_for_fxmlText_raises_AssertionError() throws Exception {
57+
URL resource = getClass().getResource("BrokenByUserData.fxml");
58+
String fxmlText = null;
59+
assertThrows(AssertionError.class, () -> new FXOMDocument(fxmlText, resource, null, null));
60+
}
61+
62+
@Test
63+
public void that_exception_in_case_of_broken_XML_is_captured() throws Exception {
64+
URL resource = getClass().getResource("IncompleteXml.fxml");
65+
String fxmlText = FXOMDocument.readContentFromURL(resource);
66+
Throwable t = assertThrows(IOException.class, () -> new FXOMDocument(fxmlText, resource, null, null));
67+
String message = t.getMessage();
68+
assertTrue(message.startsWith("org.xml.sax.SAXParseException;"));
69+
70+
Throwable cause = t.getCause();
71+
assertTrue(cause instanceof SAXParseException);
72+
}
73+
74+
@Test
75+
public void that_no_exception_is_created_with_empty_FXML() throws Exception {
76+
URL resource = getClass().getResource("Empty.fxml");
77+
String fxmlText = "";
78+
boolean normalizeFxom = false;
79+
FXOMDocument classUnderTest = new FXOMDocument(fxmlText, resource, null, null, normalizeFxom);
80+
assertNotNull(classUnderTest);
81+
}
82+
83+
@Test
84+
public void that_FXOMDocument_is_created_for_valid_FXML() throws Exception {
85+
URL validResource = getClass().getResource("ValidFxml.fxml");
86+
String validFxmlText = FXOMDocument.readContentFromURL(validResource);
87+
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, validResource, null, null);
88+
assertNotNull(classUnderTest);
89+
}
90+
91+
@Test
92+
public void that_wildcard_imports_are_built_on_demand() throws Exception {
93+
URL validResource = getClass().getResource("PublicStaticImport.fxml");
94+
String validFxmlText = FXOMDocument.readContentFromURL(validResource);
95+
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, validResource, null, null);
96+
boolean withWildCardImports = true;
97+
98+
String generatedFxmlText = classUnderTest.getFxmlText(withWildCardImports);
99+
String expectedFxmlText =
100+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
101+
+ "\n"
102+
+ "<?import javafx.scene.effect.*?>\n"
103+
+ "<?import javafx.scene.layout.*?>\n"
104+
+ "<?import javafx.scene.text.*?>\n"
105+
+ "\n"
106+
+ "<StackPane xmlns=\"http://javafx.com/javafx/null\" xmlns:fx=\"http://javafx.com/fxml/1\">\n"
107+
+ " <children>\n"
108+
+ " <Text stroke=\"BLACK\" text=\"Some simple text\">\n"
109+
+ " <effect>\n"
110+
+ " <Lighting diffuseConstant=\"2.0\" specularConstant=\"0.9\" specularExponent=\"10.5\" surfaceScale=\"9.3\">\n"
111+
+ " <light>\n"
112+
+ " <Light.Distant />\n"
113+
+ " </light>\n"
114+
+ " </Lighting>\n"
115+
+ " </effect>\n"
116+
+ " </Text>\n"
117+
+ " </children>\n"
118+
+ "</StackPane>\n"
119+
+ "";
120+
assertEquals(expectedFxmlText, generatedFxmlText);
121+
}
122+
123+
@Test
124+
public void that_generated_FXML_text_is_empty_for_empty_FXOMDocument() throws Exception {
125+
FXOMDocument classUnderTest = new FXOMDocument();
126+
boolean withWildCardImports = false;
127+
String generatedFxmlText = classUnderTest.getFxmlText(withWildCardImports);
128+
129+
assertEquals("", generatedFxmlText);
130+
}
51131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.*?>
4+
<?import javafx.scene.layout.*?>
5+
<?import com.oracle.javafx.scenebuilder.kit.fxom.*?>
6+
7+
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
8+
<children>
9+
<ComboBox layoutX="68.0" layout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.effect.Lighting?>
4+
<?import javafx.scene.text.Text?>
5+
<?import javafx.scene.layout.StackPane?>
6+
<?import javafx.scene.effect.Light.Distant?>
7+
8+
<StackPane xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
9+
<children>
10+
<Text stroke="BLACK" text="Some simple text">
11+
<effect>
12+
<Lighting diffuseConstant="2.0" specularConstant="0.9" specularExponent="10.5" surfaceScale="9.3">
13+
<light>
14+
<Light.Distant />
15+
</light>
16+
</Lighting>
17+
</effect>
18+
</Text>
19+
</children>
20+
</StackPane>

0 commit comments

Comments
 (0)