Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit78f7a80

Browse files
committed
adds new test for testing encoding and compressing email.
1 parent59eb36e commit78f7a80

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‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ public CompressingDecorator(Sender sender) {
1616

1717
@Override
1818
publicStringsend(Stringcontent) {
19-
StringcompressedContent =newString(compressContent(content));
19+
byte[]compressedBytes =compressContent(content);
20+
StringcompressedContent =newString(compressedBytes);
2021
super.send(compressedContent);
2122
returncompressedContent;
2223
}
2324

2425
privatebyte[]compressContent(Stringcontent) {
25-
try{
26-
ByteArrayOutputStreambaostream =newByteArrayOutputStream();
27-
OutputStreamoutStream =newGZIPOutputStream(baostream);
28-
outStream.write(content.getBytes("UTF-8"));
26+
try (ByteArrayOutputStreambaostream =newByteArrayOutputStream();
27+
OutputStreamoutStream =newGZIPOutputStream(baostream)){
28+
outStream.write(content.getBytes());
2929
outStream.close();
30-
byte[]compressedBytes =baostream.toByteArray();// toString not always possible
31-
returncompressedBytes;
30+
returnbaostream.toByteArray();
3231
}catch (IOExceptione) {
3332
thrownewRuntimeException("exception happened while compressing email content");
34-
3533
}
3634
}
3735

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
packagecom.designpatterns.decorator;
22

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

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

2419
@Test
25-
publicvoidtestDecorator_sendEmailAsEncodedTest() {
20+
publicvoidtestDecorator_sendEmailAsEncodedText() {
2621
Stringmessage ="test message";
2722
Sendersender =newSenderDecorator(
2823
newEncodingDecorator(
2924
newEmailSender()
3025
)
3126
);
32-
3327
StringencodedContent =sender.send(message);
34-
3528
Assertions.assertEquals(
3629
newString(Base64.getDecoder().decode (encodedContent)),
3730
message
3831
);
32+
}
3933

34+
@Test
35+
publicvoidtestDecorator_sendEmailAsCompressedText() {
36+
Stringmessage ="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+
Sendersender =newSenderDecorator(
38+
newCompressingDecorator(
39+
newEmailSender()
40+
)
41+
);
42+
StringcompressedContent =sender.send(message);
43+
Assertions.assertTrue(message.length()>=compressedContent.length());
44+
}
45+
46+
@Test
47+
publicvoidtestDecorator_sendEmailAsEncodedCompressedText() {
48+
Stringmessage ="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+
Sendersender =newSenderDecorator(
50+
newEncodingDecorator(
51+
newCompressingDecorator(
52+
newEmailSender()
53+
)
54+
)
4055

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp