Skip to content
Snippets Groups Projects
Commit f9749918 authored by Nelson Tavares de Sousa's avatar Nelson Tavares de Sousa
Browse files

TokenizerTest uses unencrypted file for comparison

minor error corrections
parent 3afeaec1
No related branches found
No related tags found
No related merge requests found
...@@ -9,10 +9,10 @@ public class ByteArray2String extends ConsumerStage<byte[]> { ...@@ -9,10 +9,10 @@ public class ByteArray2String extends ConsumerStage<byte[]> {
@Override @Override
protected void execute(final byte[] element) { protected void execute(final byte[] element) {
this.send(outputPort, new String(element)); this.send(this.outputPort, new String(element));
} }
public OutputPort<? extends String> getOutputPort() { public OutputPort<? extends String> getOutputPort() {
return outputPort; return this.outputPort;
} }
} }
...@@ -15,14 +15,14 @@ public class CipherByteArray extends ConsumerStage<byte[]> { ...@@ -15,14 +15,14 @@ public class CipherByteArray extends ConsumerStage<byte[]> {
private final OutputPort<byte[]> outputPort = this.createOutputPort(); private final OutputPort<byte[]> outputPort = this.createOutputPort();
private Cipher cipher = null; private Cipher cipher = null;
private final byte[] salt = { 't', 'e', 's', 't' };
private SecretKeySpec skeyspec = null;
public enum CipherMode { public enum CipherMode {
ENCRYPT, DECRYPT ENCRYPT, DECRYPT
} }
public CipherByteArray(final String password, final CipherMode mode) { public CipherByteArray(final String password, final CipherMode mode) {
final byte[] salt = { 't', 'e', 's', 't' };
SecretKeySpec skeyspec = null;
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), KeySpec keySpec = new PBEKeySpec(password.toCharArray(),
salt, salt,
...@@ -40,11 +40,11 @@ public class CipherByteArray extends ConsumerStage<byte[]> { ...@@ -40,11 +40,11 @@ public class CipherByteArray extends ConsumerStage<byte[]> {
skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES"); skeyspec = new SecretKeySpec(secretKey.getEncoded(), "AES");
try { try {
cipher = Cipher.getInstance(skeyspec.getAlgorithm()); this.cipher = Cipher.getInstance(skeyspec.getAlgorithm());
if (mode == CipherMode.ENCRYPT) { if (mode == CipherMode.ENCRYPT) {
cipher.init(Cipher.ENCRYPT_MODE, skeyspec); this.cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
} else { } else {
cipher.init(Cipher.DECRYPT_MODE, skeyspec); this.cipher.init(Cipher.DECRYPT_MODE, skeyspec);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -57,16 +57,16 @@ public class CipherByteArray extends ConsumerStage<byte[]> { ...@@ -57,16 +57,16 @@ public class CipherByteArray extends ConsumerStage<byte[]> {
byte[] output = null; byte[] output = null;
try { try {
output = cipher.doFinal(element); output = this.cipher.doFinal(element);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
this.send(outputPort, output); this.send(this.outputPort, output);
} }
public OutputPort<? extends byte[]> getOutputPort() { public OutputPort<? extends byte[]> getOutputPort() {
return outputPort; return this.outputPort;
} }
} }
...@@ -16,14 +16,14 @@ public class Tokenizer extends ConsumerStage<String> { ...@@ -16,14 +16,14 @@ public class Tokenizer extends ConsumerStage<String> {
@Override @Override
protected void execute(final String element) { protected void execute(final String element) {
StringTokenizer st = new StringTokenizer(element, regex); StringTokenizer st = new StringTokenizer(element, this.regex);
while (st.hasMoreTokens()) { while (st.hasMoreTokens()) {
this.send(outputPort, st.nextToken()); this.send(this.outputPort, st.nextToken());
} }
} }
public OutputPort<? extends String> getOutputPort() { public OutputPort<? extends String> getOutputPort() {
return outputPort; return this.outputPort;
} }
} }
...@@ -40,7 +40,7 @@ public class ZipByteArray extends ConsumerStage<byte[]> { ...@@ -40,7 +40,7 @@ public class ZipByteArray extends ConsumerStage<byte[]> {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
this.send(outputPort, cache); this.send(this.outputPort, cache);
} }
private byte[] compress(final byte[] data) throws IOException { private byte[] compress(final byte[] data) throws IOException {
...@@ -82,7 +82,7 @@ public class ZipByteArray extends ConsumerStage<byte[]> { ...@@ -82,7 +82,7 @@ public class ZipByteArray extends ConsumerStage<byte[]> {
} }
public OutputPort<? extends byte[]> getOutputPort() { public OutputPort<? extends byte[]> getOutputPort() {
return outputPort; return this.outputPort;
} }
} }
...@@ -17,14 +17,14 @@ public class File2ByteArray extends ConsumerStage<File> implements HeadStage { ...@@ -17,14 +17,14 @@ public class File2ByteArray extends ConsumerStage<File> implements HeadStage {
protected void execute(final File element) { protected void execute(final File element) {
try { try {
byte[] cache = Files.toByteArray(element); byte[] cache = Files.toByteArray(element);
this.send(outputPort, cache); this.send(this.outputPort, cache);
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
} }
public OutputPort<? extends byte[]> getOutputPort() { public OutputPort<? extends byte[]> getOutputPort() {
return outputPort; return this.outputPort;
} }
@Override @Override
......
...@@ -15,20 +15,16 @@ import teetime.stage.io.File2ByteArray; ...@@ -15,20 +15,16 @@ import teetime.stage.io.File2ByteArray;
public class CipherConfiguration extends AnalysisConfiguration { public class CipherConfiguration extends AnalysisConfiguration {
private final File input, output;
private final String password;
public CipherConfiguration(final String inputFile, final String outputFile, final String password) { public CipherConfiguration(final String inputFile, final String outputFile, final String password) {
this.input = new File(inputFile); final File input = new File(inputFile);
this.output = new File(outputFile); final File output = new File(outputFile);
this.password = password;
InitialElementProducer<File> init = new InitialElementProducer<File>(this.input); InitialElementProducer<File> init = new InitialElementProducer<File>(input);
File2ByteArray f2b = new File2ByteArray(); File2ByteArray f2b = new File2ByteArray();
CipherByteArray enc = new CipherByteArray(this.password, CipherMode.ENCRYPT); CipherByteArray enc = new CipherByteArray(password, CipherMode.ENCRYPT);
ZipByteArray comp = new ZipByteArray(ZipMode.COMP); ZipByteArray comp = new ZipByteArray(ZipMode.COMP);
ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP); ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
CipherByteArray decrypt = new CipherByteArray(this.password, CipherMode.DECRYPT); CipherByteArray decrypt = new CipherByteArray(password, CipherMode.DECRYPT);
ByteArrayFileWriter writer = new ByteArrayFileWriter(output); ByteArrayFileWriter writer = new ByteArrayFileWriter(output);
PIPE_FACTORY_REGISTRY.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false) PIPE_FACTORY_REGISTRY.getPipeFactory(ThreadCommunication.INTRA, PipeOrdering.ARBITRARY, false)
......
...@@ -17,18 +17,15 @@ import teetime.stage.io.File2ByteArray; ...@@ -17,18 +17,15 @@ import teetime.stage.io.File2ByteArray;
public class TokenizerConfiguration extends AnalysisConfiguration { public class TokenizerConfiguration extends AnalysisConfiguration {
private final File input;
private final String password;
private final Counter<String> counter; private final Counter<String> counter;
public TokenizerConfiguration(final String inputFile, final String password) { public TokenizerConfiguration(final String inputFile, final String password) {
this.input = new File(inputFile); final File input = new File(inputFile);
this.password = password;
InitialElementProducer<File> init = new InitialElementProducer<File>(this.input); InitialElementProducer<File> init = new InitialElementProducer<File>(input);
File2ByteArray f2b = new File2ByteArray(); File2ByteArray f2b = new File2ByteArray();
ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP); ZipByteArray decomp = new ZipByteArray(ZipMode.DECOMP);
CipherByteArray decrypt = new CipherByteArray(this.password, CipherMode.DECRYPT); CipherByteArray decrypt = new CipherByteArray(password, CipherMode.DECRYPT);
ByteArray2String b2s = new ByteArray2String(); ByteArray2String b2s = new ByteArray2String();
Tokenizer tokenizer = new Tokenizer(" "); Tokenizer tokenizer = new Tokenizer(" ");
counter = new Counter<String>(); counter = new Counter<String>();
......
package teetime.examples.tokenizer; package teetime.examples.tokenizer;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import teetime.framework.Analysis; import teetime.framework.Analysis;
import com.google.common.io.Files;
public class TokenizerTest { public class TokenizerTest {
@Test @Test
public void executeTest() { public void executeTest() throws IOException {
// Encrypted lorem ipsum // Encrypted lorem ipsum
String inputFile = "src/test/resources/data/cipherInput.txt"; String inputFile = "src/test/resources/data/cipherInput.txt";
String password = "Password"; String password = "Password";
...@@ -18,7 +24,9 @@ public class TokenizerTest { ...@@ -18,7 +24,9 @@ public class TokenizerTest {
analysis.init(); analysis.init();
analysis.start(); analysis.start();
Assert.assertEquals(970, configuration.getTokenCount()); String string = Files.toString(new File("src/test/resources/data/input.txt"), Charset.forName("UTF-8"));
Assert.assertEquals(string.split(" ").length, configuration.getTokenCount());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment