32
32
package com .oracle .javafx .scenebuilder .kit .fxom ;
33
33
34
34
import static org .junit .Assert .assertEquals ;
35
+ import static org .junit .Assert .assertFalse ;
35
36
import static org .junit .Assert .assertNotNull ;
36
37
import static org .junit .Assert .assertThrows ;
37
38
import static org .junit .Assert .assertTrue ;
38
39
39
40
import java .io .IOException ;
40
41
import java .net .URL ;
42
+ import java .util .concurrent .ExecutionException ;
43
+ import java .util .concurrent .FutureTask ;
41
44
45
+ import org .junit .BeforeClass ;
42
46
import org .junit .Test ;
43
47
import org .xml .sax .SAXParseException ;
44
48
49
+ import com .oracle .javafx .scenebuilder .kit .JfxInitializer ;
50
+
51
+ import javafx .application .Platform ;
52
+
45
53
public class FXOMDocumentTest {
54
+
55
+ @ BeforeClass
56
+ public static void init () {
57
+ JfxInitializer .initialize ();
58
+ }
59
+
46
60
@ Test
47
61
public void that_IOException_is_thrown_in_case_FXMLLoader_error () throws Exception {
48
62
URL resource = getClass ().getResource ("BrokenByUserData.fxml" );
@@ -51,26 +65,26 @@ public void that_IOException_is_thrown_in_case_FXMLLoader_error() throws Excepti
51
65
String message = t .getMessage ();
52
66
assertTrue (message .startsWith ("javafx.fxml.LoadException:" ));
53
67
}
54
-
68
+
55
69
@ Test
56
70
public void that_illegal_null_value_for_fxmlText_raises_AssertionError () throws Exception {
57
71
URL resource = getClass ().getResource ("BrokenByUserData.fxml" );
58
72
String fxmlText = null ;
59
73
assertThrows (AssertionError .class , () -> new FXOMDocument (fxmlText , resource , null , null ));
60
74
}
61
-
75
+
62
76
@ Test
63
77
public void that_exception_in_case_of_broken_XML_is_captured () throws Exception {
64
78
URL resource = getClass ().getResource ("IncompleteXml.fxml" );
65
79
String fxmlText = FXOMDocument .readContentFromURL (resource );
66
80
Throwable t = assertThrows (IOException .class , () -> new FXOMDocument (fxmlText , resource , null , null ));
67
81
String message = t .getMessage ();
68
82
assertTrue (message .startsWith ("org.xml.sax.SAXParseException;" ));
69
-
83
+
70
84
Throwable cause = t .getCause ();
71
85
assertTrue (cause instanceof SAXParseException );
72
86
}
73
-
87
+
74
88
@ Test
75
89
public void that_no_exception_is_created_with_empty_FXML () throws Exception {
76
90
URL resource = getClass ().getResource ("Empty.fxml" );
@@ -79,53 +93,87 @@ public void that_no_exception_is_created_with_empty_FXML() throws Exception {
79
93
FXOMDocument classUnderTest = new FXOMDocument (fxmlText , resource , null , null , normalizeFxom );
80
94
assertNotNull (classUnderTest );
81
95
}
82
-
96
+
83
97
@ Test
84
98
public void that_FXOMDocument_is_created_for_valid_FXML () throws Exception {
85
99
URL validResource = getClass ().getResource ("ValidFxml.fxml" );
86
100
String validFxmlText = FXOMDocument .readContentFromURL (validResource );
87
101
FXOMDocument classUnderTest = new FXOMDocument (validFxmlText , validResource , null , null );
88
102
assertNotNull (classUnderTest );
89
103
}
90
-
104
+
91
105
@ Test
92
106
public void that_wildcard_imports_are_built_on_demand () throws Exception {
93
107
URL validResource = getClass ().getResource ("PublicStaticImport.fxml" );
94
108
String validFxmlText = FXOMDocument .readContentFromURL (validResource );
95
109
FXOMDocument classUnderTest = new FXOMDocument (validFxmlText , validResource , null , null );
96
110
boolean withWildCardImports = true ;
97
-
111
+
98
112
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 "
106
116
+ "<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 "
109
118
+ " <effect>\n "
110
119
+ " <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 " + "" ;
120
123
assertEquals (expectedFxmlText , generatedFxmlText );
121
124
}
122
-
125
+
123
126
@ Test
124
127
public void that_generated_FXML_text_is_empty_for_empty_FXOMDocument () throws Exception {
125
128
FXOMDocument classUnderTest = new FXOMDocument ();
126
129
boolean withWildCardImports = false ;
127
130
String generatedFxmlText = classUnderTest .getFxmlText (withWildCardImports );
128
-
131
+
129
132
assertEquals ("" , generatedFxmlText );
130
133
}
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
+ }
131
179
}
0 commit comments