Skip to content

Commit 4b0a98f

Browse files
committed
init commit - a guide to java initialization classes and resources
1 parent 5b25c39 commit 4b0a98f

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.initializationguide;
2+
3+
import java.io.Serializable;
4+
5+
public class User implements Serializable, Cloneable {
6+
private static final long serialVersionUID = 1L;
7+
static String forum;
8+
private String name;
9+
private int id;
10+
11+
{
12+
id = 0;
13+
System.out.println("Instance Initializer");
14+
}
15+
16+
static {
17+
forum = "Java";
18+
System.out.println("Static Initializer");
19+
}
20+
21+
public User(String name, int id) {
22+
super();
23+
this.name = name;
24+
this.id = id;
25+
}
26+
27+
public User() {
28+
System.out.println("Constructor");
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public int getId() {
40+
return id;
41+
}
42+
43+
public void setId(int id) {
44+
this.id = id;
45+
}
46+
47+
@Override
48+
protected Object clone() throws CloneNotSupportedException {
49+
return this;
50+
}
51+
52+
}
53+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.initializationguide;
2+
import org.junit.Before;
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.*;
6+
7+
import java.io.BufferedInputStream;
8+
import java.io.BufferedOutputStream;
9+
import java.io.FileInputStream;
10+
import java.io.FileNotFoundException;
11+
import java.io.FileOutputStream;
12+
import java.io.IOException;
13+
import java.io.ObjectInputStream;
14+
import java.io.ObjectOutputStream;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.nio.file.Files;
17+
import java.nio.file.Path;
18+
import java.nio.file.Paths;
19+
20+
public class UserTest {
21+
22+
23+
Path userData;
24+
25+
@Before
26+
public void init() throws IOException {
27+
userData = Paths.get("src/test/resources/user.data");
28+
if (!Files.exists(userData))
29+
Files.createFile(userData);
30+
}
31+
32+
@Test
33+
public void givenUserInstance_whenIntializedWithNew_thenInstanceIsNotNull() {
34+
User user = new User("Alice", 1);
35+
assertThat(user).isNotNull();
36+
}
37+
38+
@Test
39+
public void givenUserInstance_whenInitializedWithReflection_thenInstanceIsNotNull() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
40+
User user = User.class.getConstructor(String.class, int.class)
41+
.newInstance("Alice", 2);
42+
assertThat(user).isNotNull();
43+
}
44+
45+
@Test
46+
public void givenUserInstance_whenCopiedWithClone_thenExactMatchIsCreated() throws CloneNotSupportedException {
47+
User user = new User("Alice", 3);
48+
User clonedUser = (User) user.clone();
49+
assertThat(clonedUser).isEqualTo(user);
50+
}
51+
52+
@Test
53+
public void givenFileWithBitsRepresentingUser_whenDeserialized_thenUserIsCreated() throws FileNotFoundException, IOException, ClassNotFoundException {
54+
try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(userData.toFile())))) {
55+
User user = new User("Tom", 0);
56+
out.writeObject(user);
57+
}
58+
try (ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(userData.toFile())))) {
59+
User deserializedUser = (User) in.readObject();
60+
assertThat(deserializedUser).isNotNull();
61+
}
62+
}
63+
64+
@Test
65+
public void givenUserInstance_whenValuesAreNotInitialized_thenUserNameAndIdReturnDefault() {
66+
User user = new User();
67+
assertThat(user.getName()).isNull();
68+
assertThat(user.getId() == 0);
69+
}
70+
}

‎core-java/src/test/resources/user.data‎

Whitespace-only changes.

0 commit comments

Comments
 (0)