Skip to content

Commit 28ad89d

Browse files
committed
Make SystemProfilerParser.extractVIDAndPID() static
There is no need to instantiate a SystemProfilerParser object each time extractVIDAndPID is invoked. Added also `synchronized` keword to avoid reentrance problems if the method is being used in multiple threads.
1 parent 9f5efe2 commit 28ad89d

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

app/test/processing/app/macosx/SystemProfilerParserTest.java

+40-39
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,51 @@
3333
import processing.app.TestHelper;
3434

3535
import static org.junit.Assert.assertEquals;
36+
import static processing.app.macosx.SystemProfilerParser.extractVIDAndPID;
37+
38+
import java.io.IOException;
39+
import java.io.InputStream;
3640

3741
public class SystemProfilerParserTest {
3842

3943
@Test
4044
public void shouldCorrectlyParse() throws Exception {
41-
String output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output.txt"));
42-
43-
assertEquals("0X2341_0X0044", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodemfa121"));
44-
assertEquals("0X2341_0X0044", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfa121"));
45-
46-
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output2.txt"));
47-
48-
assertEquals("0X2341_0X8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodemfd131"));
49-
assertEquals("0X2341_0X8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfd131"));
50-
51-
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output3.txt"));
52-
53-
assertEquals("0X2341_0X8041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodemfd121"));
54-
assertEquals("0X2341_0X8041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfd121"));
55-
56-
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output4.txt"));
57-
58-
assertEquals("0X2341_0X0041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodem411"));
59-
assertEquals("0X2341_0X0041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodem411"));
60-
61-
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output5.txt"));
62-
63-
assertEquals("0X2341_0X8041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodem621"));
64-
assertEquals("0X2341_0X8041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodem621"));
65-
66-
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output6.txt"));
67-
68-
assertEquals("0X2341_0X8041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodem1421"));
69-
assertEquals("0X2341_0X8041", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodem1421"));
70-
71-
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output7.txt"));
72-
73-
assertEquals("0X2341_0X8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbmodem24131"));
74-
assertEquals("0X2341_0X8036", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodem24131"));
75-
assertEquals("0X0403_0X6015", new SystemProfilerParser().extractVIDAndPID(output, "/dev/cu.usbserial-DN0031EV"));
76-
assertEquals("0X0403_0X6015", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbserial-DN0031EV"));
77-
78-
output = TestHelper.inputStreamToString(SystemProfilerParserTest.class.getResourceAsStream("system_profiler_output8.txt"));
45+
String output = getFileContent("system_profiler_output.txt");
46+
assertEquals("0X2341_0X0044", extractVIDAndPID(output, "/dev/cu.usbmodemfa121"));
47+
assertEquals("0X2341_0X0044", extractVIDAndPID(output, "/dev/tty.usbmodemfa121"));
48+
49+
output = getFileContent("system_profiler_output2.txt");
50+
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/cu.usbmodemfd131"));
51+
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/tty.usbmodemfd131"));
52+
53+
output = getFileContent("system_profiler_output3.txt");
54+
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/cu.usbmodemfd121"));
55+
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/tty.usbmodemfd121"));
56+
57+
output = getFileContent("system_profiler_output4.txt");
58+
assertEquals("0X2341_0X0041", extractVIDAndPID(output, "/dev/cu.usbmodem411"));
59+
assertEquals("0X2341_0X0041", extractVIDAndPID(output, "/dev/tty.usbmodem411"));
60+
61+
output = getFileContent("system_profiler_output5.txt");
62+
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/cu.usbmodem621"));
63+
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/tty.usbmodem621"));
64+
65+
output = getFileContent("system_profiler_output6.txt");
66+
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/cu.usbmodem1421"));
67+
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/tty.usbmodem1421"));
68+
69+
output = getFileContent("system_profiler_output7.txt");
70+
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/cu.usbmodem24131"));
71+
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/tty.usbmodem24131"));
72+
assertEquals("0X0403_0X6015", extractVIDAndPID(output, "/dev/cu.usbserial-DN0031EV"));
73+
assertEquals("0X0403_0X6015", extractVIDAndPID(output, "/dev/tty.usbserial-DN0031EV"));
74+
75+
output = getFileContent("system_profiler_output8.txt");
76+
assertEquals("0X03EB_0X2157", extractVIDAndPID(output, "/dev/tty.usbmodemfd132"));
77+
}
7978

80-
assertEquals("0X03EB_0X2157", new SystemProfilerParser().extractVIDAndPID(output, "/dev/tty.usbmodemfd132"));
79+
private String getFileContent(String filename) throws IOException {
80+
InputStream resource = SystemProfilerParserTest.class.getResourceAsStream(filename);
81+
return TestHelper.inputStreamToString(resource);
8182
}
8283
}

arduino-core/src/processing/app/macosx/Platform.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public Map<String, Object> resolveDeviceAttachedTo(String serial, Map<String, Ta
172172
}
173173

174174
try {
175-
String vidPid = new SystemProfilerParser().extractVIDAndPID(devicesListOutput, serial);
175+
String vidPid = SystemProfilerParser.extractVIDAndPID(devicesListOutput, serial);
176176

177177
if (vidPid == null) {
178178
return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput);

arduino-core/src/processing/app/macosx/SystemProfilerParser.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ public class SystemProfilerParser {
1919
private static final String DEV_TTY_USBMODEM = "/dev/tty.usbmodem";
2020
private static final String DEV_CU_USBMODEM = "/dev/cu.usbmodem";
2121

22-
private final Pattern vidRegex;
23-
private final Pattern serialNumberRegex;
24-
private final Pattern locationRegex;
25-
private final Pattern pidRegex;
22+
private static final Pattern serialNumberRegex = Pattern.compile("^Serial Number: (.+)$");
23+
private static final Pattern locationRegex = Pattern.compile("^Location ID: (.+)$");
24+
private static final Pattern pidRegex = Pattern.compile("^Product ID: (.+)$");
25+
private static final Pattern vidRegex = Pattern.compile("^Vendor ID: (.+)$");
2626

27-
public SystemProfilerParser() {
28-
this.serialNumberRegex = Pattern.compile("^Serial Number: (.+)$");
29-
this.locationRegex = Pattern.compile("^Location ID: (.+)$");
30-
this.pidRegex = Pattern.compile("^Product ID: (.+)$");
31-
this.vidRegex = Pattern.compile("^Vendor ID: (.+)$");
32-
}
33-
34-
public String extractVIDAndPID(String output, String serial) throws IOException {
27+
public synchronized static String extractVIDAndPID(String output, String serial) throws IOException {
3528
BufferedReader reader = new BufferedReader(new StringReader(output));
3629

3730
String devicePrefix;

0 commit comments

Comments
 (0)