|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2021, Gluon and/or its affiliates. |
| 2 | + * Copyright (c) 2022, Gluon and/or its affiliates. |
3 | 3 | * All rights reserved. Use is subject to license terms.
|
4 | 4 | *
|
5 | 5 | * This file is available and licensed under the following license:
|
|
31 | 31 | */
|
32 | 32 | package com.oracle.javafx.scenebuilder.kit.fxom;
|
33 | 33 |
|
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertNotNull; |
34 | 36 | import static org.junit.Assert.assertThrows;
|
35 | 37 | import static org.junit.Assert.assertTrue;
|
36 | 38 |
|
37 | 39 | import java.io.IOException;
|
38 | 40 | import java.net.URL;
|
39 | 41 |
|
40 | 42 | import org.junit.Test;
|
| 43 | +import org.xml.sax.SAXParseException; |
41 | 44 |
|
42 | 45 | public class FXOMDocumentTest {
|
43 | 46 | @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 { |
45 | 48 | URL resource = getClass().getResource("BrokenByUserData.fxml");
|
46 | 49 | String fxmlText = FXOMDocument.readContentFromURL(resource);
|
47 | 50 | Throwable t = assertThrows(IOException.class, () -> new FXOMDocument(fxmlText, resource, null, null));
|
48 | 51 | String message = t.getMessage();
|
49 | 52 | assertTrue(message.startsWith("javafx.fxml.LoadException:"));
|
50 | 53 | }
|
| 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 | + } |
51 | 131 | }
|
0 commit comments