forked from Suryakant-Bharti/Important-Java-Concepts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtectionProxy.kt
More file actions
36 lines (30 loc) Β· 792 Bytes
/
ProtectionProxy.kt
File metadata and controls
36 lines (30 loc) Β· 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import org.junit.jupiter.api.Test
interface File {
fun read(name: String)
}
class NormalFile : File {
override fun read(name: String) = println("Reading file: $name")
}
//Proxy:
class SecuredFile(private val normalFile: File) : File {
var password: String = ""
override fun read(name: String) {
if (password == "secret") {
println("Password is correct: $password")
normalFile.read(name)
} else {
println("Incorrect password. Access denied!")
}
}
}
class ProtectionProxyTest {
@Test
fun `Protection Proxy`() {
val securedFile = SecuredFile(NormalFile())
with(securedFile) {
read("readme.md")
password = "secret"
read("readme.md")
}
}
}