Created
January 19, 2017 20:47
-
-
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 ", ")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(", "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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