1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

"static" makes sense for constants and few other things. Avoid it if you don't have a specific reason for using it.

This commit is contained in:
Federico Fissore 2015-10-26 15:49:31 +01:00
parent 54ec785fd2
commit 0102a58122
3 changed files with 43 additions and 29 deletions

View File

@ -29,55 +29,62 @@
package processing.app.macosx;
import org.junit.Before;
import org.junit.Test;
import processing.app.TestHelper;
import static org.junit.Assert.assertEquals;
import static processing.app.macosx.SystemProfilerParser.extractVIDAndPID;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
public class SystemProfilerParserTest {
private SystemProfilerParser parser;
@Before
public void setUp() throws Exception {
parser = new SystemProfilerParser();
}
@Test
public void shouldCorrectlyParse() throws Exception {
String output = getFileContent("system_profiler_output.txt");
assertEquals("0X2341_0X0044", extractVIDAndPID(output, "/dev/cu.usbmodemfa121"));
assertEquals("0X2341_0X0044", extractVIDAndPID(output, "/dev/tty.usbmodemfa121"));
assertEquals("0X2341_0X0044", parser.extractVIDAndPID(output, "/dev/cu.usbmodemfa121"));
assertEquals("0X2341_0X0044", parser.extractVIDAndPID(output, "/dev/tty.usbmodemfa121"));
output = getFileContent("system_profiler_output2.txt");
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/cu.usbmodemfd131"));
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/tty.usbmodemfd131"));
assertEquals("0X2341_0X8036", parser.extractVIDAndPID(output, "/dev/cu.usbmodemfd131"));
assertEquals("0X2341_0X8036", parser.extractVIDAndPID(output, "/dev/tty.usbmodemfd131"));
output = getFileContent("system_profiler_output3.txt");
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/cu.usbmodemfd121"));
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/tty.usbmodemfd121"));
assertEquals("0X2341_0X8041", parser.extractVIDAndPID(output, "/dev/cu.usbmodemfd121"));
assertEquals("0X2341_0X8041", parser.extractVIDAndPID(output, "/dev/tty.usbmodemfd121"));
output = getFileContent("system_profiler_output4.txt");
assertEquals("0X2341_0X0041", extractVIDAndPID(output, "/dev/cu.usbmodem411"));
assertEquals("0X2341_0X0041", extractVIDAndPID(output, "/dev/tty.usbmodem411"));
assertEquals("0X2341_0X0041", parser.extractVIDAndPID(output, "/dev/cu.usbmodem411"));
assertEquals("0X2341_0X0041", parser.extractVIDAndPID(output, "/dev/tty.usbmodem411"));
output = getFileContent("system_profiler_output5.txt");
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/cu.usbmodem621"));
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/tty.usbmodem621"));
assertEquals("0X2341_0X8041", parser.extractVIDAndPID(output, "/dev/cu.usbmodem621"));
assertEquals("0X2341_0X8041", parser.extractVIDAndPID(output, "/dev/tty.usbmodem621"));
output = getFileContent("system_profiler_output6.txt");
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/cu.usbmodem1421"));
assertEquals("0X2341_0X8041", extractVIDAndPID(output, "/dev/tty.usbmodem1421"));
assertEquals("0X2341_0X8041", parser.extractVIDAndPID(output, "/dev/cu.usbmodem1421"));
assertEquals("0X2341_0X8041", parser.extractVIDAndPID(output, "/dev/tty.usbmodem1421"));
output = getFileContent("system_profiler_output7.txt");
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/cu.usbmodem24131"));
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/tty.usbmodem24131"));
assertEquals("0X0403_0X6015", extractVIDAndPID(output, "/dev/cu.usbserial-DN0031EV"));
assertEquals("0X0403_0X6015", extractVIDAndPID(output, "/dev/tty.usbserial-DN0031EV"));
assertEquals("0X2341_0X8036", parser.extractVIDAndPID(output, "/dev/cu.usbmodem24131"));
assertEquals("0X2341_0X8036", parser.extractVIDAndPID(output, "/dev/tty.usbmodem24131"));
assertEquals("0X0403_0X6015", parser.extractVIDAndPID(output, "/dev/cu.usbserial-DN0031EV"));
assertEquals("0X0403_0X6015", parser.extractVIDAndPID(output, "/dev/tty.usbserial-DN0031EV"));
output = getFileContent("system_profiler_output8.txt");
assertEquals("0X03EB_0X2157", extractVIDAndPID(output, "/dev/tty.usbmodemfd132"));
assertEquals("0X03EB_0X2157", parser.extractVIDAndPID(output, "/dev/tty.usbmodemfd132"));
// OSX El Capitan
output = getFileContent("system_profiler_output9.txt");
assertEquals("0X2341_0X8036", extractVIDAndPID(output, "/dev/tty.usbmodemFA121"));
assertEquals("0X2341_0X8036", parser.extractVIDAndPID(output, "/dev/tty.usbmodemFA121"));
}
private String getFileContent(String filename) throws IOException {

View File

@ -172,7 +172,7 @@ public class Platform extends processing.app.Platform {
}
try {
String vidPid = SystemProfilerParser.extractVIDAndPID(devicesListOutput, serial);
String vidPid = new SystemProfilerParser().extractVIDAndPID(devicesListOutput, serial);
if (vidPid == null) {
return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput);

View File

@ -19,12 +19,19 @@ public class SystemProfilerParser {
private static final String DEV_TTY_USBMODEM = "/dev/tty.usbmodem";
private static final String DEV_CU_USBMODEM = "/dev/cu.usbmodem";
private static final Pattern serialNumberRegex = Pattern.compile("^Serial Number: (.+)$");
private static final Pattern locationRegex = Pattern.compile("^Location ID: (.+)$");
private static final Pattern pidRegex = Pattern.compile("^Product ID: (.+)$");
private static final Pattern vidRegex = Pattern.compile("^Vendor ID: (.+)$");
private final Pattern vidRegex;
private final Pattern serialNumberRegex;
private final Pattern locationRegex;
private final Pattern pidRegex;
public synchronized static String extractVIDAndPID(String output, String serial) throws IOException {
public SystemProfilerParser() {
this.serialNumberRegex = Pattern.compile("^Serial Number: (.+)$");
this.locationRegex = Pattern.compile("^Location ID: (.+)$");
this.pidRegex = Pattern.compile("^Product ID: (.+)$");
this.vidRegex = Pattern.compile("^Vendor ID: (.+)$");
}
public String extractVIDAndPID(String output, String serial) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(output));
String devicePrefix;
@ -38,7 +45,7 @@ public class SystemProfilerParser {
devicePrefix = DEV_CU_USBMODEM;
}
Map<String, String> device = new HashMap<String, String>();
Map<String, String> device = new HashMap<>();
String line;
Matcher matcher;
@ -77,7 +84,7 @@ public class SystemProfilerParser {
return (device.get(VID) + "_" + device.get(PID)).toUpperCase();
}
}
device = new HashMap<String, String>();
device = new HashMap<>();
}
}