/
ConsistFileTest.java
93 lines (83 loc) · 3.21 KB
/
ConsistFileTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package jmri.jmrit.consisttool;
import java.io.File;
import java.lang.reflect.Field;
import jmri.Consist;
import jmri.ConsistManager;
import jmri.DccLocoAddress;
import jmri.InstanceManager;
import jmri.jmrit.roster.Roster;
import jmri.util.JUnitUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
/**
* Test simple functioning of ConsistFile
*
* @author Paul Bender Copyright (C) 2015
*/
public class ConsistFileTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void testCtor() {
ConsistFile file = new ConsistFile();
Assert.assertNotNull("exists", file);
}
@Test
public void testDefaultFileLocation(){
String fileName = Roster.getDefault().getRosterLocation() +
"roster" + File.separator +
"consist" + File.separator;
Assert.assertEquals("Consist File Location",fileName,
ConsistFile.getFileLocation());
}
@Test
public void testWriteFile() throws java.io.IOException {
ConsistFile file = new ConsistFile();
ConsistManager cm = InstanceManager.getDefault(ConsistManager.class);
DccLocoAddress addr = new DccLocoAddress(5,false);
Consist c = cm.getConsist(addr);
c.add(new DccLocoAddress(10,false),true);
c.add(new DccLocoAddress(1000,true),false);
String fileName = folder.newFolder().getPath() + File.separator + "consist.xml";
file.writeFile(cm.getConsistList(),fileName);
Assert.assertTrue("file created",(new File(fileName)).exists());
}
@Test
public void testWriteDefaultFile() throws java.io.IOException {
ConsistFile file = new ConsistFile();
ConsistManager cm = InstanceManager.getDefault(ConsistManager.class);
DccLocoAddress addr = new DccLocoAddress(5,false);
Consist c = cm.getConsist(addr);
c.add(new DccLocoAddress(10,false),true);
c.add(new DccLocoAddress(1000,true),false);
file.writeFile(cm.getConsistList());
String fileName = ConsistFile.defaultConsistFilename();
Assert.assertTrue("file created",(new File(fileName)).exists());
}
@Before
public void setUp() throws java.io.IOException {
JUnitUtil.setUp();
jmri.profile.Profile profile = new jmri.profile.NullProfile(folder.newFolder(jmri.profile.Profile.PROFILE));
JUnitUtil.resetProfileManager(profile );
Roster.getDefault().setRosterLocation("");
InstanceManager.setDefault(ConsistPreferencesManager.class,new ConsistPreferencesManager());
InstanceManager.setDefault(ConsistManager.class, new TestConsistManager());
}
@After
public void tearDown() {
// use reflection to reset the static file location.
try {
Class<?> c = ConsistFile.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 ConsistFile static fileLocation " + x);
}
JUnitUtil.tearDown();
}
}