package com.k13n.lt_codes;

import java.io.*;
import java.net.URL;

import org.junit.Test;
import static org.junit.Assert.*;

public class AppTest {

  @Test
  public void assertWorksWithPerfectChannel() throws Exception {
    String filename = "test.txt";
    byte[] data = readFile(filename);

    int packetSize = 100;
    Encoder enc = new Encoder(data, packetSize);
    final Decoder dec = new DefaultDecoder(enc.getSeed(), enc.getNPackets());

    enc.encode(new Encoder.Callback() {
      public boolean call(Encoder encoder, TransmissonPacket packet) {
        return dec.receive(packet);
      }
    });

    dec.write(new FileOutputStream("/tmp/" + filename + ".out"));
  }

  @Test
  public void assertWorksWithLossyChannel() throws Exception {
    String filename = "test.txt";
    byte[] data = readFile(filename);

    int packetSize = 100;
    Encoder enc = new Encoder(data, packetSize);
    final Decoder dec = new DefaultDecoder(enc.getSeed(), enc.getNPackets());

    final ErasureChannel channel = new ErasureChannel(
        new ErasureChannel.Callback() {
          @Override
          public void call(ErasureChannel channel, TransmissonPacket packet) {
            dec.receive(packet);
          }
        }, 0.3);
    enc.encode(new Encoder.Callback() {
      public boolean call(Encoder encoder, TransmissonPacket packet) {
        channel.transmit(packet);
        return dec.isDecodingFinished();
      }
    });

    dec.write(new FileOutputStream("/tmp/" + filename + ".out"));
  }

  private byte[] readFile(String filename) {
    filename = "/" + filename;
    byte[] data = null;
    try {
      URL url = this.getClass().getResource(filename);
      InputStream is = getClass().getResourceAsStream(filename);
      assertNotNull(url);
      File file = new File(url.getFile());

      data = new byte[(int) file.length()];
      DataInputStream s = new DataInputStream(is);
      s.readFully(data);
      s.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return data;
  }

}

@@ -141,16 +141,184 @@
 ppTest %7B
+%0A  private static final String FILENAME = %22test.txt%22;%0A  private static final String OUTFILE = %22/tmp/%22 + FILENAME + %22.out%22;%0A  private static final int PACKET_SIZE = 100;
 %0A%0A  @Tes
@@ -325,37 +325,33 @@
 t%0A  public void 
-asser
+i
 tWorksWithPerfec
@@ -388,101 +388,40 @@
     
-String filename = %22test.txt%22;%0A    byte%5B%5D data = readFile(filename);%0A%0A    int packetSize = 100
+byte%5B%5D data = readFile(FILENAME)
 ;%0A  
@@ -450,34 +450,35 @@
 coder(data, 
-packetSize
+PACKET_SIZE
 );%0A    final
@@ -750,35 +750,15 @@
 eam(
-%22/tmp/%22 + filename + %22.out%22
+OUTFILE
 ));%0A
@@ -784,21 +784,17 @@
 ic void 
-asser
+i
 tWorksWi
@@ -837,101 +837,40 @@
     
-String filename = %22test.txt%22;%0A    byte%5B%5D data = readFile(filename);%0A%0A    int packetSize = 100
+byte%5B%5D data = readFile(FILENAME)
 ;%0A  
@@ -907,18 +907,19 @@
 ta, 
-packetSize
+PACKET_SIZE
 );%0A 
@@ -1057,18 +1057,16 @@
 (%0A      
-  
 new Eras
@@ -1097,18 +1097,16 @@
         
-  
 @Overrid
@@ -1107,18 +1107,16 @@
 verride%0A
-  
         
@@ -1194,18 +1194,16 @@
         
-  
 dec.rece
@@ -1223,22 +1223,18 @@
         
-  %7D%0A  
+%7D%0A
       %7D,
@@ -1482,35 +1482,15 @@
 eam(
-%22/tmp/%22 + filename + %22.out%22
+OUTFILE
 ));%0A

Factor out common code in the two test-cases