- TestNG
- Commons-IO
Last active
December 19, 2015 11:59
-
-
Save RaffaeleSgarro/51414f00af7c85858d08 to your computer and use it in GitHub Desktop.
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
package com.stackoverflow; | |
import org.apache.commons.io.IOUtils; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Test; | |
import java.io.*; | |
import java.nio.file.Files; | |
import static org.testng.Assert.assertEquals; | |
public class FindLastRecordForGroups { | |
private final PrintWriter out; | |
private final File tmpFile; | |
private BufferedReader in; | |
private int index; | |
private int lastD1 = -1; | |
private int lastD2 = -1; | |
private String line; | |
private String[] values; | |
private String previousG; | |
private String previousDep; | |
public FindLastRecordForGroups(BufferedReader in, PrintWriter out) throws IOException { | |
tmpFile = Files.createTempFile("data", ".txt").toFile(); | |
tmpFile.deleteOnExit(); | |
IOUtils.copy(in, new FileOutputStream(tmpFile)); | |
this.out = out; | |
} | |
public void processSorted() throws Exception { | |
reset(); | |
previousG = null; | |
while (next()) { | |
finishLastLine(); | |
out.print(line); | |
previousDep = dep(); | |
previousG = group(); | |
} | |
finishLastLine(); | |
} | |
private void finishLastLine() { | |
if (previousG != null && previousDep != null) { | |
if (!group().equals(previousG) || !dep().equals(previousDep)) { | |
if (previousG.equals("G1") && previousDep.equals("D1")) { | |
lastD1 = index - 1; | |
out.print("|LL"); | |
} else if (previousG.equals("G2") && previousDep.equals("D2")) { | |
lastD2 = index - 1; | |
out.print("|LL"); | |
} | |
} | |
out.println(); | |
} | |
} | |
public void scanUnsorted() throws Exception { | |
reset(); | |
while (next()) { | |
switch (group()) { | |
case "G1": | |
if (dep().equals("D1")) | |
lastD1 = index; | |
break; | |
case "G2": | |
if (dep().equals("D2")) | |
lastD2 = index; | |
break; | |
} | |
} | |
} | |
public void write() throws Exception { | |
reset(); | |
while (next()) { | |
out.print(line); | |
if (lastD1 >= 0 && index == lastD1 | |
|| lastD2 >= 0 && index == lastD2 | |
) { | |
out.print("|LL"); | |
} | |
out.println(); | |
} | |
out.flush(); | |
} | |
private void reset() throws FileNotFoundException { | |
index = -1; | |
in = new BufferedReader(new FileReader(tmpFile)); | |
} | |
private String dep() { | |
return values[2]; | |
} | |
private String group() { | |
return values[5]; | |
} | |
private boolean next() throws IOException { | |
line = in.readLine(); | |
if (line != null) { | |
index++; | |
values = line.split("\\|"); | |
} | |
return line != null; | |
} | |
public static class TestCases { | |
private FindLastRecordForGroups target; | |
private StringWriter out; | |
@BeforeMethod | |
public void setUp() throws Exception { | |
BufferedReader in = new BufferedReader(new StringReader( | |
"0000027788|001400000000000000000001224627|G1|||G1\n" + | |
"0000027789|001400000000000000000001224627|D1|||G1\n" + | |
"0000027790|001400000000000000000001224627|D1|||G1\n" + | |
"0000027790|001400000000000000000001224627|D1|||G1\n" + | |
"0000027791|001400000000000000000001224627|G2|||G2\n" + | |
"0000027792|001400000000000000000001224627|D2|||G2\n" + | |
"0000027793|001400000000000000000001224627|D2|||G2\n" + | |
"0000027794|001400000000000000000001224627|G6|||G6")); | |
out = new StringWriter(); | |
target = new FindLastRecordForGroups(in, new PrintWriter(out)); | |
} | |
@Test | |
public void testUnsorted() throws Exception { | |
target.scanUnsorted(); | |
assertEquals(target.lastD1, 3, "Wrong D1"); | |
assertEquals(target.lastD2, 6, "Wrong D2"); | |
target.write(); | |
checkOutputLines(); | |
} | |
@Test | |
public void testSorted() throws Exception { | |
target.processSorted(); | |
assertEquals(target.lastD1, 3, "Wrong D1"); | |
assertEquals(target.lastD2, 6, "Wrong D2"); | |
checkOutputLines(); | |
} | |
private void checkOutputLines() throws IOException { | |
String[] expected = { | |
"0000027788|001400000000000000000001224627|G1|||G1", | |
"0000027789|001400000000000000000001224627|D1|||G1", | |
"0000027790|001400000000000000000001224627|D1|||G1", | |
"0000027790|001400000000000000000001224627|D1|||G1|LL", | |
"0000027791|001400000000000000000001224627|G2|||G2", | |
"0000027792|001400000000000000000001224627|D2|||G2", | |
"0000027793|001400000000000000000001224627|D2|||G2|LL", | |
"0000027794|001400000000000000000001224627|G6|||G6" | |
}; | |
BufferedReader actual = new BufferedReader(new StringReader(out.toString())); | |
String actualLine; | |
int currentLine = 0; | |
while ((actualLine = actual.readLine()) != null) { | |
currentLine++; | |
assertEquals(actualLine, expected[currentLine - 1], "Mismatch at line " + currentLine); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment