Repository navigation
Expand file tree
/
Copy pathSysfsTest.java
More file actions
182 lines (125 loc) · 5.75 KB
/
Copy pathSysfsTest.java
File metadata and controls
182 lines (125 loc) · 5.75 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package ev3dev.utils;
import ev3dev.hardware.EV3DevPlatform;
import ev3dev.hardware.EV3DevPropertyLoader;
import fake_ev3dev.ev3dev.sensors.FakeBattery;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@Slf4j
public class SysfsTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
String BATTERY;
String BATTERY_EV3;
String BATTERY_PISTORMS;
String BATTERY_BRICKPI;
String BATTERY_BRICKPI3;
String VOLTAGE = "voltage_now";
String CURRENT = "current_now";
@Before
public void resetTest() throws IOException {
FakeBattery.resetEV3DevInfrastructure();
final EV3DevPropertyLoader ev3DevPropertyLoader = new EV3DevPropertyLoader();
final Properties ev3DevProperties = ev3DevPropertyLoader.getEV3DevProperties();
BATTERY = ev3DevProperties.getProperty("battery");
BATTERY_EV3 = ev3DevProperties.getProperty("ev3.battery");
BATTERY_PISTORMS = ev3DevProperties.getProperty("pistorms.battery");
BATTERY_BRICKPI = ev3DevProperties.getProperty("brickpi.battery");
BATTERY_BRICKPI3 = ev3DevProperties.getProperty("brickpi3.battery");
}
//OK
@Test
public void existPathSuccessTest() throws IOException {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
final boolean result = Sysfs.existPath(fakeBattery.EV3DEV_FAKE_SYSTEM_PATH);
assertThat(result, is(true));
}
@Test
public void readString() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/" + BATTERY_EV3 + "/" + VOLTAGE;
String result = Sysfs.readString(pathToAssert);
assertThat(result, is(fakeBattery.BATTERY_FIELD_VOLTAGE_VALUE));
}
@Test
public void readInteger() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/" + BATTERY_EV3 + "/" + VOLTAGE;
int result = Sysfs.readInteger(pathToAssert);
assertThat(result, is(Integer.parseInt(FakeBattery.BATTERY_FIELD_VOLTAGE_VALUE)));
}
@Test
public void readFloat() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/" + BATTERY_EV3 + "/" + VOLTAGE;
float result = Sysfs.readFloat(pathToAssert);
assertThat(result, is(Float.parseFloat(FakeBattery.BATTERY_FIELD_VOLTAGE_VALUE)));
}
@Test
public void getElements() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY;
final List<File> fileList = new ArrayList<>();
fileList.add(new File(pathToAssert + "/" + BATTERY_EV3));
assertThat(Sysfs.getElements(pathToAssert), is(fileList));
}
@Test
public void existFile() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/"+ BATTERY_EV3 + "/" + VOLTAGE;
final Path path = Paths.get(pathToAssert);
boolean result = Sysfs.existFile(path);
assertThat(result, is(true));
}
@Test
public void notExistFile() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/"+ BATTERY_EV3 + "/" + CURRENT + "-ERROR";
final Path path = Paths.get(pathToAssert);
assertThat(Sysfs.existFile(path), is(false));
}
@Test
public void writeString() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/"+ BATTERY_EV3 + "/" + VOLTAGE;
final Path path = Paths.get(pathToAssert);
Sysfs.writeString(pathToAssert, "10");
assertThat(Sysfs.readString(pathToAssert), is("10"));
}
@Test
public void readStringWithException() throws Exception {
thrown.expect(RuntimeException.class);
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/"+ BATTERY_EV3 + "/" + VOLTAGE + "-ERROR";
final Path path = Paths.get(pathToAssert);
Sysfs.readString(pathToAssert);
}
@Test
public void writeInteger() throws Exception {
final FakeBattery fakeBattery = new FakeBattery(EV3DevPlatform.EV3BRICK);
String pathToAssert = FakeBattery.EV3DEV_FAKE_SYSTEM_PATH + "/"+ BATTERY + "/"+ BATTERY_EV3 + "/" + VOLTAGE;
final Path path = Paths.get(pathToAssert);
Sysfs.writeInteger(pathToAssert, 10);
assertThat(Sysfs.readInteger(pathToAssert), is(10));
}
@Ignore("Review error in detail for Travis CI")
@Test(expected = RuntimeException.class)
public void writeBytesTest() {
int BUFFER_SIZE = 0;
byte[] buf = new byte[BUFFER_SIZE];
final String FB_PATH = "/dev/MY_PERSONAL_PATH";
Sysfs.writeBytes(FB_PATH, buf);
}
}