Skip to content
Permalink
Browse files

fix: Do not retain profile locations as constant

Profile derived data is not constant from class loading time, so do not treat it as such.
  • Loading branch information...
rhwood committed Aug 9, 2019
1 parent 2a5f737 commit 1f5646c31c3c66e2f2b3beb22fdff00d0652444a
@@ -12,8 +12,6 @@

public class CTCFiles {

private static String fileLocation = FileUtil.getUserFilesPath() + "ctc/"; // NOI18N

/**
* Verify that the standard file path is valid.
* Create the ctc directory if needed.
@@ -33,10 +31,7 @@ public static File getFile(String fileName) {
}

public static String getFileLocation() {
if (fileLocation == null) {
fileLocation = FileUtil.getUserFilesPath() + "ctc/"; // NOI18N
}
return fileLocation;
return new File(FileUtil.getUserFilesPath(), "ctc").getAbsolutePath(); // NOI18N
}

/**
@@ -1,4 +1,3 @@
// I put this comment in to trigger another CI run. That's it.
/*
For this warning:
Note: C:\Users\NetBeansJMRI\Documents\NetBeansProjects\CTCTest\src\packageTest\FrmMainForm.java uses unchecked or unsafe operations.
@@ -30,26 +29,17 @@ At runtime (dynamic):
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
// import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
import jmri.InstanceManager;
import jmri.Sensor;
import jmri.SensorManager;
import jmri.jmrit.ctc.CTCFiles;
import jmri.jmrit.ctc.ctcserialdata.CTCSerialData;
import jmri.jmrit.ctc.ctcserialdata.CodeButtonHandlerData;
import jmri.jmrit.ctc.ctcserialdata.OtherData;
import jmri.jmrit.ctc.ctcserialdata.ProjectsCommonSubs;

/**
*
@@ -198,7 +188,6 @@ private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setLocation(new java.awt.Point(0, 0));
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
@@ -1159,11 +1148,10 @@ private void _mCheckEverythingWithJMRIActionPerformed(java.awt.event.ActionEvent
_mCheckJMRIObject.analyzeClass(codeButtonHandlerData, errors);
});
if (!errors.isEmpty()) {
StringBuffer stringBuffer = new StringBuffer();
for (String error : errors) {
stringBuffer.append(error);
stringBuffer.append("\n");
}
StringBuilder stringBuffer = new StringBuilder();
errors.forEach((error) -> {
stringBuffer.append(error).append("\n");
});
JOptionPane.showMessageDialog(this, stringBuffer.toString());
}
}
@@ -4,17 +4,15 @@
import jmri.JmriException;
import jmri.Sensor;
import jmri.SensorManager;
import jmri.SignalHead;
import jmri.SignalHeadManager;
import jmri.Turnout;
import jmri.TurnoutManager;
import jmri.util.JUnitUtil;
import org.junit.*;
import org.junit.rules.ExpectedException;
import org.netbeans.jemmy.EventTool;

/**
* Tests for the CtcRunAction Class
*
* @author Dave Sand Copyright (C) 2019
*/
public class CtcRunActionTest {
@@ -70,7 +68,6 @@ public void testAction() {
sh = hm.getSignalHead("Right-L");
Assert.assertFalse(sh.getHeld()); // NOI18N


// log.warn("Test SHM = {}", hm);
// log.warn("DEBUG = {}", sm.getSensor("IS:DEBUGCTC").getKnownState());
// log.warn("SN = {}", sm.getSensor("IS3:SN").getKnownState());
@@ -93,12 +90,12 @@ public void testAction() {

@Before
public void setUp() {
jmri.util.JUnitUtil.setUp();
JUnitUtil.setUp();
JUnitUtil.resetInstanceManager();

try {
JUnitUtil.resetProfileManager(new jmri.profile.NullProfile(folder.newFolder(jmri.profile.Profile.PROFILE)));
} catch(java.io.IOException ioe){
} catch (java.io.IOException ioe) {
Assert.fail("failed to setup profile for test");
}

@@ -108,18 +105,8 @@ public void setUp() {

@After
public void tearDown() {
// use reflection to reset the static file location.
try {
Class<?> c = jmri.jmrit.ctc.CTCFiles.class;
java.lang.reflect.Field f = c.getDeclaredField("fileLocation");
f.setAccessible(true);
f.set(new String(), null);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException x) {
Assert.fail("Failed to reset CTC static fileLocation " + x);
}

jmri.util.JUnitUtil.tearDown();
JUnitUtil.tearDown();
}

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CtcRunActionTest.class);
}
}
@@ -14,12 +14,6 @@
@Rule
public final ExpectedException thrown = ExpectedException.none();

@Test
public void testCreate() {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
new CtcRunStartup();
}

@Test
public void testGetTitle() {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
@@ -325,9 +325,9 @@ public void testMakePanel() {

@Before
public void setUp() {
jmri.util.JUnitUtil.setUp();
JUnitUtil.setUp();
JUnitUtil.resetInstanceManager();

JUnitUtil.resetFileUtilSupport();
try {
JUnitUtil.resetProfileManager(new jmri.profile.NullProfile(folder.newFolder(jmri.profile.Profile.PROFILE)));
} catch (java.io.IOException ioe) {
@@ -340,17 +340,7 @@ public void setUp() {

@After
public void tearDown() {
// use reflection to reset the static file location.
try {
Class<?> c = jmri.jmrit.ctc.CTCFiles.class;
java.lang.reflect.Field f = c.getDeclaredField("fileLocation");
f.setAccessible(true);
f.set(new String(), null);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException x) {
Assert.fail("Failed to reset CTC static fileLocation " + x);
}

jmri.util.JUnitUtil.tearDown();
JUnitUtil.tearDown();
}

// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CtcEditorActionTest.class);
@@ -4,7 +4,6 @@
import java.util.Locale;
import org.junit.*;
import org.junit.rules.ExpectedException;
import org.netbeans.jemmy.EventTool;

/**
* Tests for the CtcEditorStartup Class
@@ -3,8 +3,6 @@
import java.awt.GraphicsEnvironment;
import jmri.util.JUnitUtil;
import org.junit.*;
import org.netbeans.jemmy.EventTool;
import org.netbeans.jemmy.operators.*;

/*
* Tests for the FrmMainForm Class
@@ -20,14 +18,14 @@ public void testCtor() {

@Before
public void setUp() {
jmri.util.JUnitUtil.setUp();
JUnitUtil.setUp();
JUnitUtil.resetInstanceManager();
JUnitUtil.resetProfileManager();
}

@After
public void tearDown() {
jmri.util.JUnitUtil.tearDown();
JUnitUtil.tearDown();
}
}

@@ -4,7 +4,6 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import jmri.InstanceManager;
import jmri.Sensor;
@@ -175,34 +174,17 @@ public static void createTestFiles() {
final String PROP_FILE = "ProgramProperties.xml";
final String SYS_FILE = "CTCSystem.xml";

File fromFile;
File toFile;
Path fromPath;
Path toPath;

toFile = CTCFiles.getFile(PROP_FILE);
if (!toFile.exists()) {
fromFile = new File(SOURCE_PATH + PROP_FILE);
toPath = toFile.toPath();
fromPath = fromFile.toPath();
try {
Files.copy(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
File propsFile = new File(SOURCE_PATH, PROP_FILE);
File sysFile = new File(SOURCE_PATH, SYS_FILE);
try {
Files.copy(propsFile.toPath(), CTCFiles.getFile(PROP_FILE).toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
log.error("Copy CTC Poperties demo file failed", ex); // NOI18N
}
}

// Copy CTCSystem.xml
toFile = CTCFiles.getFile(SYS_FILE);
if (!toFile.exists()) {
fromFile = new File(SOURCE_PATH + SYS_FILE);
toPath = toFile.toPath();
fromPath = fromFile.toPath();
try {
Files.copy(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
log.error("Copy CTC Poperties demo file failed", ex); // NOI18N
}
try {
Files.copy(sysFile.toPath(), CTCFiles.getFile(SYS_FILE).toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
log.error("Copy CTC System demo file failed", ex); // NOI18N
}
}

0 comments on commit 1f5646c

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