Skip to content

Commit b47bc49

Browse files
Added another exception case, related to gluonhq#327.
1 parent 974ee34 commit b47bc49

File tree

1 file changed

+75
-27
lines changed

1 file changed

+75
-27
lines changed

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

+75-27
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,31 @@
3232
package com.oracle.javafx.scenebuilder.kit.fxom;
3333

3434
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
3536
import static org.junit.Assert.assertNotNull;
3637
import static org.junit.Assert.assertThrows;
3738
import static org.junit.Assert.assertTrue;
3839

3940
import java.io.IOException;
4041
import java.net.URL;
42+
import java.util.concurrent.ExecutionException;
43+
import java.util.concurrent.FutureTask;
4144

45+
import org.junit.BeforeClass;
4246
import org.junit.Test;
4347
import org.xml.sax.SAXParseException;
4448

49+
import com.oracle.javafx.scenebuilder.kit.JfxInitializer;
50+
51+
import javafx.application.Platform;
52+
4553
public class FXOMDocumentTest {
54+
55+
@BeforeClass
56+
public static void init() {
57+
JfxInitializer.initialize();
58+
}
59+
4660
@Test
4761
public void that_IOException_is_thrown_in_case_FXMLLoader_error() throws Exception {
4862
URL resource = getClass().getResource("BrokenByUserData.fxml");
@@ -51,26 +65,26 @@ public void that_IOException_is_thrown_in_case_FXMLLoader_error() throws Excepti
5165
String message = t.getMessage();
5266
assertTrue(message.startsWith("javafx.fxml.LoadException:"));
5367
}
54-
68+
5569
@Test
5670
public void that_illegal_null_value_for_fxmlText_raises_AssertionError() throws Exception {
5771
URL resource = getClass().getResource("BrokenByUserData.fxml");
5872
String fxmlText = null;
5973
assertThrows(AssertionError.class, () -> new FXOMDocument(fxmlText, resource, null, null));
6074
}
61-
75+
6276
@Test
6377
public void that_exception_in_case_of_broken_XML_is_captured() throws Exception {
6478
URL resource = getClass().getResource("IncompleteXml.fxml");
6579
String fxmlText = FXOMDocument.readContentFromURL(resource);
6680
Throwable t = assertThrows(IOException.class, () -> new FXOMDocument(fxmlText, resource, null, null));
6781
String message = t.getMessage();
6882
assertTrue(message.startsWith("org.xml.sax.SAXParseException;"));
69-
83+
7084
Throwable cause = t.getCause();
7185
assertTrue(cause instanceof SAXParseException);
7286
}
73-
87+
7488
@Test
7589
public void that_no_exception_is_created_with_empty_FXML() throws Exception {
7690
URL resource = getClass().getResource("Empty.fxml");
@@ -79,53 +93,87 @@ public void that_no_exception_is_created_with_empty_FXML() throws Exception {
7993
FXOMDocument classUnderTest = new FXOMDocument(fxmlText, resource, null, null, normalizeFxom);
8094
assertNotNull(classUnderTest);
8195
}
82-
96+
8397
@Test
8498
public void that_FXOMDocument_is_created_for_valid_FXML() throws Exception {
8599
URL validResource = getClass().getResource("ValidFxml.fxml");
86100
String validFxmlText = FXOMDocument.readContentFromURL(validResource);
87101
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, validResource, null, null);
88102
assertNotNull(classUnderTest);
89103
}
90-
104+
91105
@Test
92106
public void that_wildcard_imports_are_built_on_demand() throws Exception {
93107
URL validResource = getClass().getResource("PublicStaticImport.fxml");
94108
String validFxmlText = FXOMDocument.readContentFromURL(validResource);
95109
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, validResource, null, null);
96110
boolean withWildCardImports = true;
97-
111+
98112
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"
113+
String expectedFxmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "\n"
114+
+ "<?import javafx.scene.effect.*?>\n" + "<?import javafx.scene.layout.*?>\n"
115+
+ "<?import javafx.scene.text.*?>\n" + "\n"
106116
+ "<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"
117+
+ " <children>\n" + " <Text stroke=\"BLACK\" text=\"Some simple text\">\n"
109118
+ " <effect>\n"
110119
+ " <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+
+ " <light>\n" + " <Light.Distant />\n" + " </light>\n"
121+
+ " </Lighting>\n" + " </effect>\n" + " </Text>\n" + " </children>\n"
122+
+ "</StackPane>\n" + "";
120123
assertEquals(expectedFxmlText, generatedFxmlText);
121124
}
122-
125+
123126
@Test
124127
public void that_generated_FXML_text_is_empty_for_empty_FXOMDocument() throws Exception {
125128
FXOMDocument classUnderTest = new FXOMDocument();
126129
boolean withWildCardImports = false;
127130
String generatedFxmlText = classUnderTest.getFxmlText(withWildCardImports);
128-
131+
129132
assertEquals("", generatedFxmlText);
130133
}
134+
135+
@Test
136+
public void that_fxml_with_defines_loads_without_error_without_normalization() throws Exception {
137+
URL resource = getClass().getResource("DynamicScreenSize.fxml");
138+
String validFxmlText = FXOMDocument.readContentFromURL(resource);
139+
140+
invokeAndWait(() -> {
141+
try {
142+
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, resource, null, null, false);
143+
String beforeNormalization = classUnderTest.getFxmlText(false);
144+
assertFalse(beforeNormalization.isBlank());
145+
} catch (IOException e) {
146+
throw new AssertionError("unexpected error: " + e);
147+
}
148+
});
149+
}
150+
151+
@Test
152+
public void that_missing_imports_during_defines_resolution_cause_exception() throws Exception {
153+
URL resource = getClass().getResource("DynamicScreenSize.fxml");
154+
String validFxmlText = FXOMDocument.readContentFromURL(resource);
155+
156+
Throwable t = assertThrows(ExecutionException.class,
157+
() -> invokeAndWait(
158+
() -> {
159+
try {
160+
/* normalization enabled */
161+
FXOMDocument classUnderTest = new FXOMDocument(validFxmlText, resource, null, null, true);
162+
String beforeNormalization = classUnderTest.getFxmlText(false);
163+
assertFalse(beforeNormalization.isBlank());
164+
} catch (IOException e) {
165+
throw new AssertionError("unexpected error: " + e);
166+
}
167+
}));
168+
169+
assertTrue(t.getCause() instanceof IllegalStateException);
170+
String message = t.getCause().getMessage();
171+
assertTrue(message.startsWith("Bug in FXOMRefresher: FXML dumped in "));
172+
}
173+
174+
void invokeAndWait(Runnable r) throws Exception {
175+
FutureTask<?> task = new FutureTask<>(r, null);
176+
Platform.runLater(task);
177+
task.get();
178+
}
131179
}

0 commit comments

Comments
 (0)