Skip to content

Commit 78f7a80

Browse files
committed
adds new test for testing encoding and compressing email.
1 parent 59eb36e commit 78f7a80

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/main/java/com/designpatterns/structural/decorator/CompressingDecorator.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ public CompressingDecorator(Sender sender) {
1616

1717
@Override
1818
public String send(String content) {
19-
String compressedContent = new String(compressContent(content));
19+
byte[] compressedBytes = compressContent(content);
20+
String compressedContent = new String(compressedBytes);
2021
super.send(compressedContent);
2122
return compressedContent;
2223
}
2324

2425
private byte[] compressContent(String content) {
25-
try{
26-
ByteArrayOutputStream baostream = new ByteArrayOutputStream();
27-
OutputStream outStream = new GZIPOutputStream(baostream);
28-
outStream.write(content.getBytes("UTF-8"));
26+
try (ByteArrayOutputStream baostream = new ByteArrayOutputStream();
27+
OutputStream outStream = new GZIPOutputStream(baostream)){
28+
outStream.write(content.getBytes());
2929
outStream.close();
30-
byte[] compressedBytes = baostream.toByteArray(); // toString not always possible
31-
return compressedBytes;
30+
return baostream.toByteArray();
3231
} catch (IOException e) {
3332
throw new RuntimeException("exception happened while compressing email content");
34-
3533
}
3634
}
3735

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.designpatterns.decorator;
22

3-
import com.designpatterns.structural.decorator.EmailSender;
4-
import com.designpatterns.structural.decorator.EncodingDecorator;
5-
import com.designpatterns.structural.decorator.Sender;
6-
import com.designpatterns.structural.decorator.SenderDecorator;
3+
import com.designpatterns.structural.decorator.*;
74
import org.junit.jupiter.api.Assertions;
85
import org.junit.jupiter.api.Test;
96

@@ -16,28 +13,50 @@ public void testDecorator_sendEmailAsPlainText() {
1613
String message = "test message";
1714
EmailSender sender = new EmailSender();
1815
String content = sender.send(message);
19-
2016
Assertions.assertEquals(content, message);
21-
2217
}
2318

2419
@Test
25-
public void testDecorator_sendEmailAsEncodedTest() {
20+
public void testDecorator_sendEmailAsEncodedText() {
2621
String message = "test message";
2722
Sender sender = new SenderDecorator(
2823
new EncodingDecorator(
2924
new EmailSender()
3025
)
3126
);
32-
3327
String encodedContent = sender.send(message);
34-
3528
Assertions.assertEquals(
3629
new String(Base64.getDecoder().decode (encodedContent)),
3730
message
3831
);
32+
}
3933

34+
@Test
35+
public void testDecorator_sendEmailAsCompressedText() {
36+
String message = "Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.[16] Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but it has fewer low-level facilities than either of them. As of 2019, Java was one of the most popular programming languages in use according to GitHub,[17][18] particularly for client-server web applications, with a reported 9 million developers.";
37+
Sender sender = new SenderDecorator(
38+
new CompressingDecorator(
39+
new EmailSender()
40+
)
41+
);
42+
String compressedContent = sender.send(message);
43+
Assertions.assertTrue(message.length()>= compressedContent.length());
44+
}
45+
46+
@Test
47+
public void testDecorator_sendEmailAsEncodedCompressedText() {
48+
String message = "Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA),[15] meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.[16] Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but it has fewer low-level facilities than either of them. As of 2019, Java was one of the most popular programming languages in use according to GitHub,[17][18] particularly for client-server web applications, with a reported 9 million developers.";
49+
Sender sender = new SenderDecorator(
50+
new EncodingDecorator(
51+
new CompressingDecorator(
52+
new EmailSender()
53+
)
54+
)
4055

56+
);
57+
String encodedCompressedContent = sender.send(message);
4158

59+
String decodedCompressed = new String( Base64.getDecoder().decode (encodedCompressedContent));
60+
Assertions.assertTrue(message.length()>= decodedCompressed.length());
4261
}
4362
}

0 commit comments

Comments
 (0)