Q. Complete the following “DataProcessingImpl.java” class ….. so that the unit tests shown below pass?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.passing.unittests; public class DataProcessingImpl implements DataProcessing { public String getContents(String path) { return null; } public String getSortedByFirstName(String input) { return null; } public String getSortedByLastName(String input) { return null; } public String getSortedByYear(String input) { return null; } } |
Following unit tests fail. You need to implement the logic to pass these tests.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.passing.unittests; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class DataProcessingTest { private static final String PATH = "src/test/java/employees.txt"; private DataProcessing dp = null; @Before public void init() { dp = new DataProcessingImpl(); } @Test public void testGetContents() { String contents = dp.getContents(PATH); Assert.assertEquals("Wrong", "Samuel, Peter, 1995\nJohn, Elliot, 2013\nSimon, Gregory, 1990", contents); } @Test public void sortByFirstName() { String contents = dp.getContents(PATH); String sortedContents = dp.getSortedByFirstName(contents); Assert.assertEquals("Wrong", "John, Elliot, 2013\nSamuel, Peter, 1995\nSimon, Gregory, 1990", sortedContents); } @Test public void sortByLastName() { String contents = dp.getContents(PATH); String sortedContents = dp.getSortedByLastName(contents); Assert.assertEquals("Wrong", "John, Elliot, 2013\nSimon, Gregory, 1990\nSamuel, Peter, 1995", sortedContents); } @Test public void sortByYear() { String contents = dp.getContents(PATH); String sortedContents = dp.getSortedByYear(contents); Assert.assertEquals("Wrong", "Simon, Gregory, 1990\nSamuel, Peter, 1995\nJohn, Elliot, 2013", sortedContents); } } |
Solution:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package com.passing.unittests; import java.io.File; import java.io.FileInputStream; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class DataProcessingImpl implements DataProcessing { public String getContents(String path) { Scanner scanner = null; StringBuilder sb = new StringBuilder(20); try { FileInputStream fis = new FileInputStream(new File(path)); scanner = new Scanner(fis); while ((scanner.hasNextLine())) { String next = scanner.nextLine(); sb.append(next + "\n"); } } catch (Exception ex) { ex.printStackTrace(); } finally { if(scanner != null) { scanner.close(); } } return sb.toString().trim(); } public String getSortedByFirstName(String input) { if(input == null || input.length() == 0){ throw new IllegalArgumentException("input=" + input); } return getSortedContents(input, 0); } public String getSortedByLastName(String input) { if(input == null || input.length() == 0){ throw new IllegalArgumentException("input=" + input); } return getSortedContents(input, 1); } public String getSortedByYear(String input) { if(input == null || input.length() == 0){ throw new IllegalArgumentException("input=" + input); } return getSortedContents(input, 2); } /** * Generic method to sort using a TreeMap. * @param input CSV format: first name, last name, year joined * @param sortIndex index of CSV line to sort by * @return */ private String getSortedContents(String input, int sortIndex){ Map<String, String> map = new TreeMap<String, String>(); String[] split = input.split("\n"); for (String line : split) { String key = getSortKey(line, sortIndex); map.put(key, line); } StringBuilder sb = new StringBuilder(20); for (String key : map.keySet()) { sb.append(map.get(key) + "\n"); } return sb.toString().trim(); } /** * Splits it into an array using "," as delimiter * and returns the given index, 0 is first name, 1 is last name, and 2 is year joined * @param line * @param index * @return */ private static String getSortKey(String line, int index) { return line.split(",")[index];// extract value you want to sort on } } |
Key Points If you are using Java…