Skip to content

Instantly share code, notes, and snippets.

@bmaggi
Created January 19, 2017 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmaggi/57def89563e6f3f844b28d7f016fe31d to your computer and use it in GitHub Desktop.
Save bmaggi/57def89563e6f3f844b28d7f016fe31d to your computer and use it in GitHub Desktop.
Utility class to store a String Array as String and get it back (don't work with ", ")
import java.util.Arrays;
/**
* Utility class to store a String Array as String and get it back
*/
public class StringArrayStringStore {
private String backup;
public String[] storeRestore(String[] datas) {
store(datas);
return restore();
}
public void store(String[] datas) {
backup = Arrays.toString(datas);
}
public String[] restore() {
return backup.substring(1, backup.length()-1).split(", ");
}
}
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class StringArrayStringStoreTest {
@Parameters
public static Collection<String[][]> data() {
return Arrays.asList(new String[][][] {
{{"a","b"}},
{{"c"}}
});
}
private String[] data;
public StringArrayStringStoreTest(String[] data) {
this.data = data;
}
@Test
public void testStoreRestoreEmptyArray() {
Assert.assertArrayEquals("Data was corrupted during store restore",data, new StringArrayStringStore().storeRestore(data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment