Skip to content

Replace StringUtils + Partially cleanup FileUtils + Fix unit test #8688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 26, 2019
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<classpathentry kind="lib" path="app/lib/jackson-module-mrbean-2.9.5.jar"/>
<classpathentry kind="lib" path="app/lib/jackson-annotations-2.9.5.jar"/>
<classpathentry kind="lib" path="app/lib/commons-compress-1.8.jar"/>
<classpathentry kind="lib" path="app/lib/commons-lang3-3.8.1.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/arduino-core"/>
<classpathentry kind="output" path="app/bin"/>
</classpath>
Binary file removed app/lib/commons-lang3-3.3.2.jar
Binary file not shown.
Binary file added app/lib/commons-lang3-3.8.1.jar
Binary file not shown.
11 changes: 2 additions & 9 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -1691,19 +1691,12 @@ public int compare(File file, File file2) {
});

boolean ifound = false;

for (File subfolder : files) {
if (FileUtils.isSCCSOrHiddenFile(subfolder)) {
continue;
}

if (!subfolder.isDirectory()) continue;

if (addSketchesSubmenu(menu, subfolder.getName(), subfolder)) {
if (!FileUtils.isSCCSOrHiddenFile(subfolder) && subfolder.isDirectory()
&& addSketchesSubmenu(menu, subfolder.getName(), subfolder)) {
ifound = true;
}
}

return ifound;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,29 @@
import processing.app.helpers.FileUtils;

import java.io.File;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class MergeSketchWithUploaderTest {

private File sketch;
private File bootloader;

@Before
public void setup() throws Exception {
File originalSketch = new File(MergeSketchWithUploaderTest.class.getResource("/sketch.hex").getFile());
File originalSketch = getResourceFile("/sketch.hex");
sketch = new File(System.getProperty("java.io.tmpdir"), "sketch.hex");
FileUtils.copyFile(originalSketch, sketch);
removeCariageReturns(sketch);

File originalBootloader = getResourceFile("/optiboot_atmega328.hex");
bootloader = new File(System.getProperty("java.io.tmpdir"), "optiboot_atmega328.hex");
FileUtils.copyFile(originalBootloader, bootloader);
removeCariageReturns(bootloader);
}

@After
Expand All @@ -57,11 +68,24 @@ public void removeTmpFile() {
@Test
public void shouldMergeWithOptiboot() throws Exception {
assertEquals(11720, sketch.length());
assertEquals(1432, bootloader.length());

File bootloader = new File(MergeSketchWithUploaderTest.class.getResource("/optiboot_atmega328.hex").getFile());
File bootloader = getResourceFile("/optiboot_atmega328.hex");
new MergeSketchWithBooloader().merge(sketch, bootloader);
assertEquals(13140, sketch.length());
}

private static File getResourceFile(String resourcePath) throws Exception {
return new File(URLDecoder.decode(
MergeSketchWithUploaderTest.class.getResource(resourcePath).getFile(), "UTF-8"));
}

private static void removeCariageReturns(File file) throws Exception {
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
StringBuilder contentBuilder = new StringBuilder();
for(String line : lines) {
contentBuilder.append(line).append('\n');
}
Files.write(file.toPath(), contentBuilder.toString().getBytes(StandardCharsets.UTF_8));
}
}
10 changes: 5 additions & 5 deletions app/test/processing/app/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ public void testCommandLineVersion() throws Exception {
pr.waitFor();

Assertions.assertThat(pr.exitValue())
.as("Process will finish with exit code 0 in --version")
.isEqualTo(0);
.as("Process will finish with exit code 0 in --version")
.isEqualTo(0);
Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream())))
.matches("Arduino: \\d+\\.\\d+\\.\\d+.*\n");
.matches("Arduino: \\d+\\.\\d+\\.\\d+.*\r?\n");
}

@Test
Expand All @@ -156,7 +156,7 @@ public void testCommandLineMultipleAction() throws Exception {
pr.waitFor();

Assertions.assertThat(pr.exitValue())
.as("Multiple Action will be rejected")
.isEqualTo(3);
.as("Multiple Action will be rejected")
.isEqualTo(3);
}
}
42 changes: 0 additions & 42 deletions app/test/processing/app/helpers/StringUtilsTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion arduino-core/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<classpathentry kind="lib" path="lib/bcprov-jdk15on-152.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.7.jar"/>
<classpathentry kind="lib" path="lib/commons-compress-1.8.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.3.2.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.8.1.jar"/>
<classpathentry kind="lib" path="lib/jackson-annotations-2.9.5.jar"/>
<classpathentry kind="lib" path="lib/jackson-core-2.9.5.jar"/>
<classpathentry kind="lib" path="lib/jackson-databind-2.9.5.jar"/>
Expand Down
Binary file removed arduino-core/lib/commons-lang3-3.3.2.jar
Binary file not shown.
Binary file added arduino-core/lib/commons-lang3-3.8.1.jar
Binary file not shown.
20 changes: 10 additions & 10 deletions arduino-core/src/cc/arduino/packages/Uploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,35 @@
import processing.app.debug.MessageConsumer;
import processing.app.debug.MessageSiphon;
import processing.app.helpers.ProcessUtils;
import processing.app.helpers.StringUtils;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;

import static processing.app.I18n.tr;

public abstract class Uploader implements MessageConsumer {

private static final List<String> STRINGS_TO_SUPPRESS;
private static final List<String> AVRDUDE_PROBLEMS;
private static final String[] STRINGS_TO_SUPPRESS;
private static final String[] AVRDUDE_PROBLEMS;

static {
STRINGS_TO_SUPPRESS = Arrays.asList("Connecting to programmer:",
STRINGS_TO_SUPPRESS = new String[] {"Connecting to programmer:",
"Found programmer: Id = \"CATERIN\"; type = S",
"Software Version = 1.0; No Hardware Version given.",
"Programmer supports auto addr increment.",
"Programmer supports buffered memory access with buffersize=128 bytes.",
"Programmer supports the following devices:", "Device code: 0x44");
"Programmer supports the following devices:", "Device code: 0x44"};

AVRDUDE_PROBLEMS = Arrays.asList("Programmer is not responding",
AVRDUDE_PROBLEMS = new String[] {"Programmer is not responding",
"programmer is not responding",
"protocol error", "avrdude: ser_open(): can't open device",
"avrdude: ser_drain(): read error",
"avrdude: ser_send(): write error",
"avrdude: error: buffered memory access not supported.");
"avrdude: error: buffered memory access not supported."};
}

protected final boolean verbose;
Expand Down Expand Up @@ -155,7 +155,7 @@ public String getFailureMessage() {
@Override
public void message(String s) {
// selectively suppress a bunch of avrdude output for AVR109/Caterina that should already be quelled but isn't
if (!verbose && StringUtils.stringContainsOneOf(s, STRINGS_TO_SUPPRESS)) {
if (!verbose && StringUtils.containsAny(s, STRINGS_TO_SUPPRESS)) {
s = "";
}

Expand All @@ -175,7 +175,7 @@ public void message(String s) {
error = tr("Device is not responding, check the right serial port is selected or RESET the board right before exporting");
return;
}
if (StringUtils.stringContainsOneOf(s, AVRDUDE_PROBLEMS)) {
if (StringUtils.containsAny(s, AVRDUDE_PROBLEMS)) {
error = tr("Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonFactory;
Expand All @@ -48,7 +50,6 @@
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import processing.app.PreferencesData;
import processing.app.helpers.StringUtils;

public class PluggableDiscovery implements Discovery {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@

package cc.arduino.packages.uploaders;

import processing.app.helpers.FileUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

public class MergeSketchWithBooloader {

public void merge(File sketch, File bootloader) throws IOException {
List<String> mergedSketch = FileUtils.readFileToListOfStrings(sketch);
List<String> mergedSketch = Files.readAllLines(sketch.toPath(), StandardCharsets.UTF_8);
mergedSketch.remove(mergedSketch.size() - 1);
mergedSketch.addAll(FileUtils.readFileToListOfStrings(bootloader));
mergedSketch.addAll(Files.readAllLines(bootloader.toPath(), StandardCharsets.UTF_8));

FileWriter writer = null;
try {
Expand Down
11 changes: 8 additions & 3 deletions arduino-core/src/cc/arduino/packages/uploaders/SSHUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,23 @@
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.PreferencesMapException;
import processing.app.helpers.StringReplacer;
import processing.app.helpers.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import static processing.app.I18n.tr;

public class SSHUploader extends Uploader {

private static final List<String> FILES_NOT_TO_COPY = Arrays.asList(".DS_Store", ".Trash", "Thumbs.db", "__MACOSX");
private static final Set<String> FILES_NOT_TO_COPY =
Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(".DS_Store", ".Trash", "Thumbs.db", "__MACOSX")));

private final BoardPort port;

Expand Down Expand Up @@ -223,7 +228,7 @@ private void recursiveSCP(File from, SCP scp) throws IOException {
}

for (File file : files) {
if (!StringUtils.stringContainsOneOf(file.getName(), FILES_NOT_TO_COPY)) {
if (!FILES_NOT_TO_COPY.contains(file.getName())) {
if (file.isDirectory() && file.canExecute()) {
scp.startFolder(file.getName());
recursiveSCP(file, scp);
Expand Down
6 changes: 3 additions & 3 deletions arduino-core/src/processing/app/BaseNoGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ static public void initPackages() throws Exception {
} catch (JsonProcessingException | SignatureVerificationFailedException e) {
File indexFile = indexer.getIndexFile(Constants.DEFAULT_INDEX_FILE_NAME);
File indexSignatureFile = indexer.getIndexFile(Constants.DEFAULT_INDEX_FILE_NAME + ".sig");
FileUtils.deleteIfExists(indexFile);
FileUtils.deleteIfExists(indexSignatureFile);
indexFile.delete();
indexSignatureFile.delete();
throw e;
}
indexer.syncWithFilesystem();
Expand All @@ -502,7 +502,7 @@ static public void initPackages() throws Exception {
librariesIndexer.parseIndex();
} catch (JsonProcessingException e) {
File librariesIndexFile = librariesIndexer.getIndexFile();
FileUtils.deleteIfExists(librariesIndexFile);
librariesIndexFile.delete();
}

if (discoveryManager == null) {
Expand Down
Loading