StringTokenizerTest refactor (#1611)
* TokenizerRefactor * TokenizerRefactor * TokenizerRefactor
This commit is contained in:
parent
13a42ea281
commit
60c2edaaeb
|
@ -9,10 +9,10 @@ import java.util.List;
|
|||
import java.util.StringTokenizer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Application {
|
||||
public class MyTokenizer {
|
||||
|
||||
public List<String> getTokens(String str) {
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
List<String> tokens = new ArrayList<>();
|
||||
// StringTokenizer tokenizer = new StringTokenizer( str );
|
||||
StringTokenizer tokenizer = new StringTokenizer(str, ",");
|
||||
// StringTokenizer tokenizer = new StringTokenizer( str , "," , true );
|
||||
|
@ -25,18 +25,18 @@ public class Application {
|
|||
}
|
||||
|
||||
public List<String> getTokensWithCollection(String str) {
|
||||
StringTokenizer tokenizer = new StringTokenizer(str, ",");
|
||||
|
||||
return Collections.list(tokenizer).stream()
|
||||
return Collections
|
||||
.list(new StringTokenizer(str, ","))
|
||||
.stream()
|
||||
.map(token -> (String) token)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<String> getTokensFromFile(String path, String delim) {
|
||||
List<String> tokens = new ArrayList<>();
|
||||
String currLine = "";
|
||||
String currLine;
|
||||
StringTokenizer tokenizer;
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(Application.class.getResourceAsStream("/" + path)))) {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(MyTokenizer.class.getResourceAsStream("/" + path)))) {
|
||||
while ((currLine = br.readLine()) != null) {
|
||||
tokenizer = new StringTokenizer(currLine, delim);
|
||||
while (tokenizer.hasMoreElements()) {
|
|
@ -1,47 +0,0 @@
|
|||
package com.baeldung.stringtokenizer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ApplicationTest {
|
||||
|
||||
Application application = new Application();
|
||||
List<String> expectedTokensForString = new ArrayList<String>();
|
||||
List<String> expectedTokensForFile = new ArrayList<String>();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
expectedTokensForString.add("Welcome");
|
||||
expectedTokensForString.add("to");
|
||||
expectedTokensForString.add("baeldung.com");
|
||||
|
||||
expectedTokensForFile.add("1");
|
||||
expectedTokensForFile.add("IND");
|
||||
expectedTokensForFile.add("India");
|
||||
expectedTokensForFile.add("2");
|
||||
expectedTokensForFile.add("MY");
|
||||
expectedTokensForFile.add("Malaysia");
|
||||
expectedTokensForFile.add("3");
|
||||
expectedTokensForFile.add("AU");
|
||||
expectedTokensForFile.add("Australia");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_thenGetListOfString() {
|
||||
String str = "Welcome,to,baeldung.com";
|
||||
List<String> actualTokens = application.getTokens(str);
|
||||
assertEquals(expectedTokensForString, actualTokens);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_thenGetListOfString() {
|
||||
List<String> actualTokens = application.getTokensFromFile("data.csv", "|");
|
||||
assertEquals(expectedTokensForFile, actualTokens);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.stringtokenizer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TokenizerTest {
|
||||
|
||||
private final MyTokenizer myTokenizer = new MyTokenizer();
|
||||
private final List<String> expectedTokensForString = Arrays.asList("Welcome", "to", "baeldung.com");
|
||||
private final List<String> expectedTokensForFile = Arrays.asList("1", "IND", "India", "2", "MY", "Malaysia", "3", "AU", "Australia");
|
||||
|
||||
@Test
|
||||
public void givenString_thenGetListOfString() {
|
||||
String str = "Welcome,to,baeldung.com";
|
||||
List<String> actualTokens = myTokenizer.getTokens(str);
|
||||
assertEquals(expectedTokensForString, actualTokens);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_thenGetListOfString() {
|
||||
List<String> actualTokens = myTokenizer.getTokensFromFile("data.csv", "|");
|
||||
assertEquals(expectedTokensForFile, actualTokens);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue