|
| 1 | +package com.designpatterns.decorator; |
| 2 | + |
| 3 | +import com.designpatterns.structural.decorator.*; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.util.Base64; |
| 8 | + |
| 9 | +public class DecoratorDemo { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void testDecorator_sendEmailAsPlainText() { |
| 13 | + String message = "test message"; |
| 14 | + EmailSender sender = new EmailSender(); |
| 15 | + String content = sender.send(message); |
| 16 | + Assertions.assertEquals(content, message); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testDecorator_sendEmailAsEncodedText() { |
| 21 | + String message = "test message"; |
| 22 | + Sender sender = new SenderDecorator( |
| 23 | + new EncodingDecorator( |
| 24 | + new EmailSender() |
| 25 | + ) |
| 26 | + ); |
| 27 | + String encodedContent = sender.send(message); |
| 28 | + Assertions.assertEquals( |
| 29 | + new String(Base64.getDecoder().decode (encodedContent)), |
| 30 | + message |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 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 | + ) |
| 55 | + |
| 56 | + ); |
| 57 | + String encodedCompressedContent = sender.send(message); |
| 58 | + |
| 59 | + String decodedCompressed = new String( Base64.getDecoder().decode (encodedCompressedContent)); |
| 60 | + Assertions.assertTrue(message.length()>= decodedCompressed.length()); |
| 61 | + } |
| 62 | +} |
0 commit comments