Skip to content

Commit

Permalink
Correctly assigned application preferences nodes in tests and importer
Browse files Browse the repository at this point in the history
classes.
  • Loading branch information
Oliver-Loeffler committed Jan 23, 2022
1 parent d05133b commit a371a5f
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<buildDate>${maven.build.timestamp}</buildDate>
<javaVersion>${java.version}, ${java.runtime.name}</javaVersion>
<javafxVersion>${javafx.version}</javafxVersion>
<maven.compiler.release>17</maven.compiler.release>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,22 @@ AppPlatformDirectories getPlatformDirectories() {
* @return {@link ImportResult} describing how the activity was completed.
*/
ImportResult performImportWhenDesired() {
String forceImport = System.getProperty("forceImport", "false");
if ("true".equalsIgnoreCase(forceImport)) {
preferences.remove(PREF_IMPORT_USER_LIBRARY);
}
boolean forcedImport = importForced();
boolean requiresImport = preferences.getBoolean(PREF_IMPORT_USER_LIBRARY, true);
if (requiresImport) {
if (requiresImport || forcedImport) {
if (forcedImport) {
logger.log(Level.FINE, "detected -DforceImport=true");
}
return importPreviousVersionUserLibrary();
}
logger.log(Level.INFO, "Previous version user library will not be imported.");
return ImportResult.SKIPPED;
}

private boolean importForced() {
String forceImport = System.getProperty("forceImport", "false");
return "true".equalsIgnoreCase(forceImport);
}

void clearImportDecision() {
preferences.remove(PREF_IMPORT_USER_LIBRARY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,6 @@ public PreferencesImporter getImporter() {
}

public UserLibraryImporter getUserLibraryImporter() {
return new UserLibraryImporter(applicationPreferences);
return new UserLibraryImporter(applicationRootPreferences);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ private void copyChildren(Preferences source, Preferences targetNode) throws Bac
* @return true in case the key is not {@code PREF_PERFORM_IMPORT} set.
*/
boolean askForImport() {
String forceImport = System.getProperty("forceImport", "false");
if ("true".equalsIgnoreCase(forceImport)) {
target.remove(PREF_PERFORM_IMPORT);
}
boolean importRequired = target.getBoolean(PREF_PERFORM_IMPORT, true);
logger.log(Level.FINE, "preferences import required; {0}", importRequired);
return importRequired;
Expand Down Expand Up @@ -180,14 +176,24 @@ public void tryImportingPreviousVersionSettings() {
* @return true when previous version settings have been found and user has not yet decided
*/
public boolean askForImportIfOlderSettingsExist() {
return this.optionalSourceNode.map(this::previousVersionPrefsFound).isPresent() && askForImport();
boolean previousVersionFound = this.optionalSourceNode.isPresent();
logger.log(Level.FINE, "older preferences detected: {0}", previousVersionFound);
if (previousVersionFound) {
VersionedPreferences prefs = this.optionalSourceNode.get();
logger.log(Level.FINE, "Version: {0}", prefs.version());
logger.log(Level.FINE, "Node : {0}", prefs.node());
}
boolean forcedImport = importForced();
if (forcedImport) {
logger.log(Level.FINE, "detected -DforceImport=true");
}
return previousVersionFound && (forcedImport || askForImport());
}

private VersionedPreferences previousVersionPrefsFound(VersionedPreferences versionedpreferences) {
logger.log(Level.FINE, "{0}", versionedpreferences);
return versionedpreferences;
private boolean importForced() {
String forceImport = System.getProperty("forceImport", "false");
return "true".equalsIgnoreCase(forceImport);
}

/**
* Defines an activity which will be executed after import of settings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public List<VersionedPreferences> getPreviousVersions() {
public Optional<VersionedPreferences> previousVersionPrefs() {
List<VersionedPreferences> previousVersions = getPreviousVersions();
if (previousVersions.isEmpty()) {
Logger.getLogger(VersionedPreferencesFinder.class.getName()).log(Level.FINE,
"No previous versions preferences found!");
return Optional.empty();
}
return Optional.of(previousVersions.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public class PreferencesControllerTest {

@BeforeClass
public static void setup() {
PrefsHelper.setToNull(PreferencesController.getSingleton(),"singleton");
testNode = Preferences.userRoot()
.node("PREFS_CONTROLLER_TEST")
.node("com/oracle/javafx/scenebuilder/app/preferences");

PrefsHelper.removeAllChildNodes(testNode);
PrefsHelper.setToNull(PreferencesController.getSingleton(),"singleton");
classUnderTest = PreferencesController.getSingleton(testNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@
* Utility Class to help preparing Preferences nodes for tests
*/
public class PrefsHelper {

private PrefsHelper() {
/* not intended for instantiation */
}

public static void removeAllNonReleaseNodes(Preferences prefs) {
System.out.println(prefs);
String appVersion = AppSettings.getSceneBuilderVersion();
String versionSpecificNode = PreferencesController.SB_RELEASE_NODE_PREFIX + appVersion;
try {
Expand All @@ -59,7 +62,7 @@ public static void removeAllNonReleaseNodes(Preferences prefs) {
throw new RuntimeException(e);
}
}

public static void removeAllChildNodes(Preferences prefs) {
try {
String[] childnodes = prefs.childrenNames();
Expand All @@ -71,7 +74,7 @@ public static void removeAllChildNodes(Preferences prefs) {
throw new RuntimeException(e);
}
}

public static synchronized void setToNull(Object source, String fieldName) {
try {
Class<?> sourceClass = source.getClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public class VersionedPreferencesFinderTest {
@BeforeClass
public static void setup() {
testNode = Preferences.userRoot()
.node("/SBTEST")
.node("/com/oracle/javafx/scenebuilder/app/preferences");
.node("SBTEST")
.node("com/oracle/javafx/scenebuilder/app/preferences");

PrefsHelper.removeAllChildNodes(testNode);
classUnderTest = new VersionedPreferencesFinder("SB_", testNode);
}
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<mainClass>${main.class.name}</mainClass>
<options>
<option>--add-opens=javafx.fxml/javafx.fxml=ALL-UNNAMED</option>
<option>-DforceImport=true</option>
</options>
</configuration>
</plugin>
Expand Down

0 comments on commit a371a5f

Please sign in to comment.