Skip to content

Commit 919113e

Browse files
committed
added tests
1 parent 385defd commit 919113e

4 files changed

Lines changed: 308 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2023 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.logging.home;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
25+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
28+
29+
/**
30+
* Tests for the HomeController class.
31+
*
32+
* @author Dominik Schadow
33+
*/
34+
@WebMvcTest(HomeController.class)
35+
class HomeControllerTest {
36+
@Autowired
37+
private MockMvc mockMvc;
38+
39+
@Test
40+
void home_returnsIndexView() throws Exception {
41+
mockMvc.perform(get("/"))
42+
.andExpect(status().isOk())
43+
.andExpect(view().name("index"))
44+
.andExpect(model().attributeExists("login"));
45+
}
46+
47+
@Test
48+
void home_addsEmptyLoginToModel() throws Exception {
49+
mockMvc.perform(get("/"))
50+
.andExpect(status().isOk())
51+
.andExpect(model().attribute("login", new Login("", "")));
52+
}
53+
54+
@Test
55+
void login_returnsLoginView() throws Exception {
56+
mockMvc.perform(post("/login")
57+
.param("username", "testuser")
58+
.param("password", "testpassword"))
59+
.andExpect(status().isOk())
60+
.andExpect(view().name("login"))
61+
.andExpect(model().attributeExists("login"));
62+
}
63+
64+
@Test
65+
void login_addsLoginToModel() throws Exception {
66+
mockMvc.perform(post("/login")
67+
.param("username", "testuser")
68+
.param("password", "testpassword"))
69+
.andExpect(status().isOk())
70+
.andExpect(model().attribute("login", new Login("testuser", "testpassword")));
71+
}
72+
73+
@Test
74+
void login_withEmptyCredentials_returnsLoginView() throws Exception {
75+
mockMvc.perform(post("/login")
76+
.param("username", "")
77+
.param("password", ""))
78+
.andExpect(status().isOk())
79+
.andExpect(view().name("login"))
80+
.andExpect(model().attribute("login", new Login("", "")));
81+
}
82+
}

serialize-me/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
<groupId>com.google.guava</groupId>
2020
<artifactId>guava</artifactId>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.junit.jupiter</groupId>
24+
<artifactId>junit-jupiter</artifactId>
25+
<scope>test</scope>
26+
</dependency>
2227
</dependencies>
2328
</project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.serialize;
19+
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.BufferedInputStream;
25+
import java.io.File;
26+
import java.io.FileInputStream;
27+
import java.io.FileOutputStream;
28+
import java.io.ObjectInputStream;
29+
import java.io.ObjectOutputStream;
30+
31+
import static org.junit.jupiter.api.Assertions.*;
32+
33+
/**
34+
* Tests for the Deserializer class.
35+
*
36+
* @author Dominik Schadow
37+
*/
38+
class DeserializerTest {
39+
private static final String TEST_FILE = "test-deserialize-me.bin";
40+
41+
@AfterEach
42+
void tearDown() {
43+
File file = new File(TEST_FILE);
44+
if (file.exists()) {
45+
file.delete();
46+
}
47+
}
48+
49+
@Test
50+
void deserialize_validFile_returnsCorrectObject() throws Exception {
51+
SerializeMe original = new SerializeMe();
52+
original.setFirstname("Arthur");
53+
original.setLastname("Dent");
54+
55+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) {
56+
oos.writeObject(original);
57+
oos.flush();
58+
}
59+
60+
try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(TEST_FILE)))) {
61+
SerializeMe deserialized = (SerializeMe) is.readObject();
62+
63+
assertNotNull(deserialized);
64+
assertEquals("Arthur", deserialized.getFirstname());
65+
assertEquals("Dent", deserialized.getLastname());
66+
}
67+
}
68+
69+
@Test
70+
void deserialize_withNullValues_returnsObjectWithNullFields() throws Exception {
71+
SerializeMe original = new SerializeMe();
72+
73+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) {
74+
oos.writeObject(original);
75+
oos.flush();
76+
}
77+
78+
try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(TEST_FILE)))) {
79+
SerializeMe deserialized = (SerializeMe) is.readObject();
80+
81+
assertNotNull(deserialized);
82+
assertNull(deserialized.getFirstname());
83+
assertNull(deserialized.getLastname());
84+
}
85+
}
86+
87+
@Test
88+
void deserialize_nonExistentFile_throwsException() {
89+
assertThrows(Exception.class, () -> {
90+
try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream("non-existent-file.bin")))) {
91+
is.readObject();
92+
}
93+
});
94+
}
95+
96+
@Test
97+
void deserialize_multipleObjects_returnsAllCorrectly() throws Exception {
98+
SerializeMe first = new SerializeMe();
99+
first.setFirstname("Ford");
100+
first.setLastname("Prefect");
101+
102+
SerializeMe second = new SerializeMe();
103+
second.setFirstname("Zaphod");
104+
second.setLastname("Beeblebrox");
105+
106+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) {
107+
oos.writeObject(first);
108+
oos.writeObject(second);
109+
oos.flush();
110+
}
111+
112+
try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(TEST_FILE)))) {
113+
SerializeMe deserializedFirst = (SerializeMe) is.readObject();
114+
SerializeMe deserializedSecond = (SerializeMe) is.readObject();
115+
116+
assertEquals("Ford", deserializedFirst.getFirstname());
117+
assertEquals("Prefect", deserializedFirst.getLastname());
118+
assertEquals("Zaphod", deserializedSecond.getFirstname());
119+
assertEquals("Beeblebrox", deserializedSecond.getLastname());
120+
}
121+
}
122+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com
3+
*
4+
* This file is part of the Java Security project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package de.dominikschadow.javasecurity.serialize;
19+
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.io.File;
24+
import java.io.FileInputStream;
25+
import java.io.FileOutputStream;
26+
import java.io.ObjectInputStream;
27+
import java.io.ObjectOutputStream;
28+
29+
import static org.junit.jupiter.api.Assertions.*;
30+
31+
/**
32+
* Tests for the Serializer class.
33+
*
34+
* @author Dominik Schadow
35+
*/
36+
class SerializerTest {
37+
private static final String TEST_FILE = "test-serialize-me.bin";
38+
39+
@AfterEach
40+
void tearDown() {
41+
File file = new File(TEST_FILE);
42+
if (file.exists()) {
43+
file.delete();
44+
}
45+
}
46+
47+
@Test
48+
void serializeMe_canBeSerializedAndDeserialized() throws Exception {
49+
SerializeMe serializeMe = new SerializeMe();
50+
serializeMe.setFirstname("Arthur");
51+
serializeMe.setLastname("Dent");
52+
53+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) {
54+
oos.writeObject(serializeMe);
55+
oos.flush();
56+
}
57+
58+
File file = new File(TEST_FILE);
59+
assertTrue(file.exists(), "Serialized file should exist");
60+
61+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(TEST_FILE))) {
62+
SerializeMe deserialized = (SerializeMe) ois.readObject();
63+
assertEquals("Arthur", deserialized.getFirstname());
64+
assertEquals("Dent", deserialized.getLastname());
65+
}
66+
}
67+
68+
@Test
69+
void serializeMe_createsFile() throws Exception {
70+
SerializeMe serializeMe = new SerializeMe();
71+
serializeMe.setFirstname("Ford");
72+
serializeMe.setLastname("Prefect");
73+
74+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) {
75+
oos.writeObject(serializeMe);
76+
oos.flush();
77+
}
78+
79+
File file = new File(TEST_FILE);
80+
assertTrue(file.exists(), "Serialized file should be created");
81+
assertTrue(file.length() > 0, "Serialized file should not be empty");
82+
}
83+
84+
@Test
85+
void serializeMe_withNullValues_canBeSerialized() throws Exception {
86+
SerializeMe serializeMe = new SerializeMe();
87+
88+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) {
89+
oos.writeObject(serializeMe);
90+
oos.flush();
91+
}
92+
93+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(TEST_FILE))) {
94+
SerializeMe deserialized = (SerializeMe) ois.readObject();
95+
assertNull(deserialized.getFirstname());
96+
assertNull(deserialized.getLastname());
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)