Skip to content
Permalink
Browse files

fix: avoid potential use of ProfileManager.getDefault() while creatin…

…g that default
  • Loading branch information...
rhwood committed Aug 6, 2019
1 parent e41a6bf commit 404f0a9eb6492e8d734c994e9722a8bb4967e42d
@@ -260,7 +260,7 @@ protected void saveActiveProfile(@CheckForNull Profile profile, boolean autoStar
os = new FileOutputStream(config);
p.storeToXML(os, "Active profile configuration (saved at " + (new Date()).toString() + ")"); // NOI18N
os.close();
} catch (Throwable ex) { // IOException, but also misc errors in Java XML processing
} catch (IOException ex) {
log.error("While trying to save active profile {}", config, ex);
if (os != null) {
os.close();
@@ -536,14 +536,14 @@ private void writeProfiles() throws IOException {
this.profiles.stream().map((p) -> {
Element e = new Element(PROFILE);
e.setAttribute(Profile.ID, p.getId());
e.setAttribute(Profile.PATH, FileUtil.getPortableFilename(p.getPath(), true, true));
e.setAttribute(Profile.PATH, FileUtil.getPortableFilename(null, p.getPath(), true, true));
return e;
}).forEach((e) -> {
profilesElement.addContent(e);
});
this.searchPaths.stream().map((f) -> {
Element e = new Element(Profile.PATH);
e.setAttribute(Profile.PATH, FileUtil.getPortableFilename(f.getPath(), true, true));
e.setAttribute(Profile.PATH, FileUtil.getPortableFilename(null, f.getPath(), true, true));
e.setAttribute(DEFAULT, Boolean.toString(f.equals(this.defaultSearchPath)));
return e;
}).forEach((e) -> {
@@ -825,7 +825,7 @@ public void export(@Nonnull Profile profile, @Nonnull File target, boolean expor
InstanceManager.getDefault(RosterConfigManager.class).initialize(profile);
InstanceManager.getDefault(RosterConfigManager.class).initialize(tempProfile);
if (exportExternalUserFiles) {
FileUtil.copy(new File(FileUtil.getUserFilesPath()), tempProfilePath);
FileUtil.copy(new File(FileUtil.getUserFilesPath(profile)), tempProfilePath);
FileUtil.setUserFilesPath(tempProfile, FileUtil.getProfilePath(tempProfile));
InstanceManager.getDefault(FileLocationsPreferences.class).savePreferences(tempProfile);
}
@@ -2,7 +2,6 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -321,9 +320,8 @@ public void testAttributeList() throws Exception {
public void testDefaultLocation() {
Assert.assertTrue("creates a default", Roster.getDefault() != null);
Assert.assertEquals("always same", Roster.getDefault(), Roster.getDefault());

// since we created it when we referenced it, should be in InstanceManager
Assert.assertTrue("registered a default", jmri.InstanceManager.getNullableDefault(Roster.class) != null);
// Default roster not stored in InstanceManager
Assert.assertNull("registered a default", jmri.InstanceManager.getNullableDefault(Roster.class));
}

@Test
@@ -408,6 +406,7 @@ public void setUp() {
// use the default reset.
JUnitUtil.resetProfileManager();
}
JUnitUtil.initRosterConfigManager();
}

@After
@@ -273,6 +273,7 @@ public void setUp() {
jmri.InstanceManager.setDefault(jmri.jmrix.ConnectionConfigManager.class, new jmri.jmrix.ConnectionConfigManager());
jmri.InstanceManager.setDefault(jmri.jmrit.symbolicprog.ProgrammerConfigManager.class, new jmri.jmrit.symbolicprog.ProgrammerConfigManager());
JUnitUtil.initDebugProgrammerManager();
JUnitUtil.initRosterConfigManager();
Roster.getDefault(); // ensure exists
if(!GraphicsEnvironment.isHeadless()){
frame = new RosterFrame();
@@ -266,6 +266,7 @@ public void testQueryMomentary() {
public void setUp() throws Exception {
JUnitUtil.setUp();
JUnitUtil.initDebugThrottleManager();
JUnitUtil.initRosterConfigManager();
cis = new ControllerInterfaceScaffold();
tcls = new ThrottleControllerListenerScaffold();
controller = new MultiThrottleController('A',"test",tcls,cis);
@@ -18,7 +18,7 @@
public void testCTor() {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
EcosTrafficController tc = new EcosInterfaceScaffold();
EcosSystemConnectionMemo memo = new jmri.jmrix.ecos.EcosSystemConnectionMemo(tc){
EcosSystemConnectionMemo memo = new EcosSystemConnectionMemo(tc){
@Override
public EcosPreferences getPreferenceManager(){
return new EcosPreferences(this){
@@ -36,7 +36,7 @@ public boolean getPreferencesLoaded(){
@Test
public void testCTorHeadLess() {
EcosTrafficController tc = new EcosInterfaceScaffold();
EcosSystemConnectionMemo memo = new jmri.jmrix.ecos.EcosSystemConnectionMemo(tc){
EcosSystemConnectionMemo memo = new EcosSystemConnectionMemo(tc){
@Override
public EcosPreferences getPreferenceManager(){
return new EcosPreferences(this){
@@ -76,12 +76,12 @@ public int getRemoveLocoFromJMRI(){
Assert.assertNotNull("exists",t);
}

// The minimal setup for log4J
@Before
public void setUp() {
JUnitUtil.setUp();
jmri.util.JUnitUtil.resetProfileManager();
jmri.util.JUnitUtil.initDefaultUserMessagePreferences();
JUnitUtil.resetProfileManager();
JUnitUtil.initDefaultUserMessagePreferences();
JUnitUtil.initRosterConfigManager();
}

@After
@@ -56,6 +56,7 @@
import jmri.jmrit.display.layoutEditor.LayoutBlockManager;
import jmri.jmrit.logix.OBlockManager;
import jmri.jmrit.logix.WarrantManager;
import jmri.jmrit.roster.RosterConfigManager;
import jmri.jmrix.ConnectionConfigManager;
import jmri.jmrix.debugthrottle.DebugThrottleManager;
import jmri.jmrix.internal.InternalReporterManager;
@@ -83,6 +84,7 @@
import jmri.util.managers.SignalMastManagerThrowExceptionScaffold;
import jmri.util.managers.TurnoutManagerThrowExceptionScaffold;
import jmri.util.managers.WarrantManagerThrowExceptionScaffold;
import jmri.util.prefs.InitializationException;
import jmri.util.prefs.JmriConfigurationProvider;
import jmri.util.prefs.JmriPreferencesProvider;
import jmri.util.prefs.JmriUserInterfaceConfigurationProvider;
@@ -956,6 +958,16 @@ public static void initConnectionConfigManager() {
InstanceManager.setDefault(ConnectionConfigManager.class, new ConnectionConfigManager());
}

public static void initRosterConfigManager() {
RosterConfigManager manager = new RosterConfigManager();
try {
manager.initialize(ProfileManager.getDefault().getActiveProfile());
} catch (InitializationException ex) {
log.error("Failed to initialize RosterConfigManager", ex);
}
InstanceManager.setDefault(RosterConfigManager.class, manager);
}

/*
* Use reflection to reset the jmri.Application instance
*/

0 comments on commit 404f0a9

Please sign in to comment.
You can’t perform that action at this time.