Skip to content

Commit

Permalink
#19 merge branch with default SB-16
Browse files Browse the repository at this point in the history
  • Loading branch information
tiainen committed Nov 16, 2015
2 parents f6e5102 + 3596ea4 commit c9cb57e
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
*/
package com.oracle.javafx.scenebuilder.kit.fxom;

import com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueAuxiliary;
import com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueDocument;
import com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueInstruction;
import java.util.HashSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -86,54 +88,44 @@ private void updateNameSpace(FXOMDocument fxomDocument) {


}



private void updateImportInstructions(FXOMDocument fxomDocument) {
assert fxomDocument.getFxomRoot() != null;

// Collects all packages already declared by import processing instructions
final GlueDocument glue = fxomDocument.getGlue();
final List<GlueInstruction> imports = glue.collectInstructions("import");
final Set<String> existingPackageNames = new HashSet<>();
for (GlueInstruction i : imports) {
final String data = i.getData();
if (data.endsWith(".*")) {
final String packageName = data.substring(0, data.length()-2);
existingPackageNames.add(packageName);
}
}

// Collects all the classes declared in the fxom document,
// constructs the set of packages to be imported.
final Set<String> newPackageNames = new TreeSet<>(); // Sorted
for (Class<?> declaredClass
: fxomDocument.getFxomRoot().collectDeclaredClasses()) {
newPackageNames.add(declaredClass.getPackage().getName());
}
newPackageNames.add("java.lang");

// Removes package names already declared
newPackageNames.removeAll(existingPackageNames);

// Chooses where to insert the new import processing intructions.
// If there are some, we insert the new ones afterward.
final int firstImportIndex;
if (imports.isEmpty()) {
firstImportIndex = 0;
} else {
final GlueInstruction firstImport = imports.get(0);
firstImportIndex = glue.getHeader().indexOf(firstImport);
}

// Creates a glue instruction for each package to be declared.
// We insert after the last import instruction.
int i = firstImportIndex;
for (String packageName : newPackageNames) {
final GlueInstruction instruction
= new GlueInstruction(glue, "import", packageName+".*");
glue.getHeader().add(i, instruction);
i++;

// gets list of the imports to be added to the FXML document.
List<GlueInstruction> importList = getHeaderIncludes(fxomDocument);

// synchronizes the glue with the list of glue instructions
synchronizeHeader(fxomDocument.getGlue(), importList);
}

private List<GlueInstruction> getHeaderIncludes(FXOMDocument fxomDocument) {
// Collects all the classes declared in the fxomdocument,
// constructs the set of classes to be imported. No doubles allowed.
final Set<String> imports = new TreeSet<>(); // Sorted
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

// Creates a List of glue instruction for each class that was declared.
List<GlueInstruction> importsList = new ArrayList<>();
imports.forEach(className -> {
final GlueInstruction instruction = new GlueInstruction(fxomDocument.getGlue(), "import", className);
importsList.add(instruction);
});

return importsList;
}

private void synchronizeHeader(GlueDocument glue, List<GlueInstruction> importList) {
List<GlueAuxiliary> temp = glue.getHeader();
synchronized (this) {
glue.getHeader().clear();
glue.getHeader().addAll(importList);

if (!glue.getHeader().equals(importList))
glue.getHeader().addAll(temp);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
package com.oracle.javafx.scenebuilder.kit.fxom;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.junit.BeforeClass;
import org.junit.Test;

import javafx.embed.swing.JFXPanel;

/**
* Unit test for {@link FXOMSaver#updateImportInstructions(FXOMDocument)}.
*/
public class FXOMSaverUpdateImportInstructionsTest {

private static FXOMDocument fxomDocument;
private static FXOMSaver serviceUnderTest;

@BeforeClass
public static void initialize() {
new JFXPanel();
}

@Test
public void testEmptyFXML() throws IOException {
setupTestCase(1);

assertTrue("fxml is empty", fxomDocument.getFxmlText().isEmpty());
}

@Test
public void testNoWildcard() {
setupTestCase(2);

Set<String> imports = new TreeSet<>();
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

Set<String> givenImports = new TreeSet<>();
givenImports.add("javafx.scene.control.Button");
givenImports.add("javafx.scene.control.ComboBox");
givenImports.add("javafx.scene.control.TextField");
givenImports.add("javafx.scene.layout.AnchorPane");
assertTrue(imports.containsAll(givenImports));

}

@Test
public void testUnusedImports() {
setupTestCase(3);

Set<String> imports = new TreeSet<>();
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

Set<String> unusedImports = new TreeSet<>();
unusedImports.add("java.util.Date");
unusedImports.add("java.math.*");
unusedImports.add("java.util.Set");
unusedImports.add("org.junit.Test");
assertFalse("unused imports are not present", imports.containsAll(unusedImports));
}

@Test
public void testWithWildcard() {
setupTestCase(4);

Set<String> imports = new TreeSet<>();
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

assertFalse("fxml import does not contain javafx.scene.*", imports.contains("javafx.scene.*"));

}

@Test
public void testWithMoreWildcards() {
setupTestCase(5);

Set<String> imports = new TreeSet<>();
// java.lang.* is not a declared class, therefore not in the imports Set
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

assertFalse("fxml import does not contain javafx.scene.*", imports.contains("javafx.scene.*"));
assertFalse("fxml import does not contain javafx.scene.layout.*", imports.contains("javafx.control.*"));
}

@Test
public void testDuplicates() {
setupTestCase(6);

Set<String> imports = new TreeSet<>();
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

// java.lang.* is not a declared class, therefore not in the imports Set
assertTrue("fxml contain only 4 imports", (imports.size() == 4));
}

@Test
public void testWildcardsAndDuplicates() {
setupTestCase(7);

Set<String> imports = new TreeSet<>();
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

// java.lang.* is not a declared class, therefore not in the imports Set
imports.forEach(i -> {
assertFalse("fxml does not contain .*", i.contains(".*"));
});
assertTrue("fxml contains only 4 imports", (imports.size() == 4));

}

@Test
public void testCustomButton() {
setupTestCase(8);

Set<String> imports = new TreeSet<>();
fxomDocument.getFxomRoot().collectDeclaredClasses().forEach(dc -> {
imports.add(dc.getName());
});

assertTrue("fxml has import com.oracle.javafx.scenebuilder.kit.fxom.TestCustomButton",
imports.contains("com.oracle.javafx.scenebuilder.kit.fxom.TestCustomButton"));
assertFalse("fxml does not contain com.oracle.javafx.scenebuilder.*",
imports.contains("com.oracle.javafx.scenebuilder.*"));

// java.lang.* is not a declared class, therefore not in the imports Set
imports.forEach(i -> {
assertFalse("fxml does not contain .*", i.contains(".*"));
});

}

private String callService() {
return serviceUnderTest.save(fxomDocument);
}

private void setupTestCase(int n) {
String fileName = null;
switch (n) {
case 1:
fileName = "Empty";
break;
case 2:
fileName = "NoWildcard";
break;
case 3:
fileName = "UnusedImports";
break;
case 4:
fileName = "WithWildcard";
break;
case 5:
fileName = "WithMoreWildcards";
break;
case 6:
fileName = "Duplicates";
break;
case 7:
fileName = "WildcardsAndDuplicates";
break;
case 8:
fileName = "CustomButton";
break;
default:
fileName = "NoFXMLFound";
break;
}
Path pathToFXML = Paths.get("src/test/resources/com/oracle/javafx/scenebuilder/kit/fxom/" + fileName + ".fxml");
Path pathToTestFXML = Paths.get("src/test/resources/com/oracle/javafx/scenebuilder/kit/fxom/testerFXML.fxml");
try {
Files.deleteIfExists(pathToTestFXML);

// Setup for the fxomDocument from the FXML file that will be tested
setupFXOMDocument(pathToFXML);

String savedFXML = callService();

// Creates new FXML file with the new output
Files.write(pathToTestFXML, savedFXML.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}

}

// setup for the FXOMDocument that will be tested
private void setupFXOMDocument(Path relativePath) {
// relativePath =
// "src/test/resources/com/oracle/javafx/scenebuilder/kit/fxom/[fxmlname].fxml"
File fxmlTesterFile = new File(relativePath.getParent() + "/testerFXML.fxml");
serviceUnderTest = new FXOMSaver();
try {
URL location = fxmlTesterFile.toURI().toURL();
String fxmlString = getFxmlAsString(relativePath);

fxomDocument = new FXOMDocument(fxmlString, location, null, null);
} catch (IOException e) {
e.printStackTrace();
}
}

// reads FXML file and gives it back as a String
private static String getFxmlAsString(Path path) throws IOException {
List<String> fxml = null;
fxml = Files.readAllLines(path, StandardCharsets.UTF_8);

StringBuilder sb = new StringBuilder();
fxml.forEach(line -> {
sb.append(line + "\n");
});

return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.oracle.javafx.scenebuilder.kit.fxom;

import javafx.scene.control.Button;

/**
* Needed for the
* {@link FXOMSaverUpdateImportInstructionsTest#testCustomButton()}
*
*/
public class TestCustomButton extends Button {

public TestCustomButton() {
this.setText("CustomButton");
this.setOnAction((ActionEvent) -> {
System.out.println("Custom Button clicked!");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import com.oracle.javafx.scenebuilder.*?>
<?import java.lang.*?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.AnchorPane?>
<?import com.oracle.javafx.scenebuilder.kit.fxom.*?>

<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">
<children>
<ComboBox layoutX="68.0" layoutY="61.0" prefWidth="150.0" />
<TestCustomButton layoutX="-111.0" layoutY="-188.0" text="test" />
</children>
</AnchorPane>
Loading

0 comments on commit c9cb57e

Please sign in to comment.