Skip to content
This repository was archived by the owner on Apr 16, 2022. It is now read-only.

Issue 844 portable metadata files #847

Merged
merged 3 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/org/opendatakit/briefcase/model/form/FormMetadata.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opendatakit.briefcase.model.form;

import static org.opendatakit.briefcase.model.form.AsJson.getJson;
import static org.opendatakit.briefcase.util.Host.isWindows;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -110,9 +111,12 @@ FormMetadata withLastExportedSubmission(String instanceId, OffsetDateTime submis

@Override
public ObjectNode asJson(ObjectMapper mapper) {
String portableFormDir = isWindows()
? formDir.toString().replace("\\", "/")
: formDir.toString();
ObjectNode root = mapper.createObjectNode();
root.putObject("key").setAll(key.asJson(mapper));
root.put("formDir", formDir.toString());
root.put("formDir", portableFormDir);
root.put("hasBeenPulled", hasBeenPulled);
root.putObject("cursor").setAll(cursor.asJson(mapper));
lastExportedSubmission.ifPresent(o -> root.putObject("lastExportedSubmission").setAll(o.asJson(mapper)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ public Matcher<XFormParameters> apply(XFormParameters value) {
return matcherProvider.apply(value);
}
}
}
}
73 changes: 30 additions & 43 deletions test/java/org/opendatakit/briefcase/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,40 @@
package org.opendatakit.briefcase.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Assert;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.opendatakit.briefcase.util.StringUtils.stripIllegalChars;

import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(value = Parameterized.class)
public class StringUtilsTest {

@Test
public void stripIllegalChars_shouldReturnNullForNullInput() {
Assert.assertNull(StringUtils.stripIllegalChars(null));
}

@Test
public void stripIllegalChars_shouldRemovePunctuationChars() {
String input = "abcdef.:;";
String output = StringUtils.stripIllegalChars(input);

Pattern pattern = Pattern.compile("\\p{Punct}");
Matcher matcher = pattern.matcher(output);
Assert.assertTrue(!matcher.matches());
Assert.assertEquals("abcdef___", output);
@Parameterized.Parameter(value = 0)
public String testCase;

@Parameterized.Parameter(value = 1)
public String input;

@Parameterized.Parameter(value = 2)
public String expectedOutput;

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"null input produces null output", null, null},
{"Common punctuation chars get replaced by underscore", "abcdef.:;", "abcdef___"},
{"Windows and *nix dir separators get replaced by underscore", "a\\b/c", "a_b_c"},
{"Whitespace chars get normalized", "abcdef\t\n\r ", "abcdef "},
{"Unicode chars should get through", "シャンプー", "シャンプー"},
});
}

@Test
public void stripIllegalChars_shouldReplaceWhitespaceWithSpace() {
String input = "abcdef\t\n\r";
String output = StringUtils.stripIllegalChars(input);

Pattern pattern = Pattern.compile("[\\t\\n\\x0B\\f\\r]");
Matcher matcher = pattern.matcher(output);
Assert.assertTrue(String.format("Input: %s, output: %s", input, output), !matcher.matches());
Assert.assertEquals("abcdef ", output);
public void test_stripIllegalChars() {
assertThat(stripIllegalChars(input), expectedOutput == null ? is(nullValue()) : is(expectedOutput));
}

@Test
public void stripIllegalChars_shouldWorkWithUnicodeCharacters() {
String input = ".:;%&シャンプー \n\r";
String output = StringUtils.stripIllegalChars(input);

Pattern pattern = Pattern.compile("\\p{Punct}");
Matcher matcher = pattern.matcher(output);
Assert.assertTrue(String.format("Input: %s, output: %s", input, output), !matcher.matches());

pattern = Pattern.compile("[\\t\\n\\x0B\\f\\r]");
matcher = pattern.matcher(output);
Assert.assertTrue(String.format("Input: %s, output: %s", input, output), !matcher.matches());

Assert.assertEquals("_____シャンプー ", output);
}

}