Skip to content

Commit 24acf35

Browse files
MherBaghinyanpivovarit
authored andcommitted
Bael 1611 (eugenp#3799)
* Externalizable test * Guide to Externalizable interface +tests * serialize Community
1 parent 510ad19 commit 24acf35

4 files changed

Lines changed: 213 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.externalizable;
2+
3+
import java.io.*;
4+
5+
public class Community implements Serializable {
6+
7+
private int id;
8+
9+
public int getId() {
10+
return id;
11+
}
12+
13+
public void setId(int id) {
14+
this.id = id;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return "Community{" +
20+
"id=" + id +
21+
'}';
22+
}
23+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.externalizable;
2+
3+
import java.io.Externalizable;
4+
import java.io.IOException;
5+
import java.io.ObjectInput;
6+
import java.io.ObjectOutput;
7+
8+
public class Country implements Externalizable {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
private String name;
13+
private String capital;
14+
private int code;
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getCapital() {
25+
return capital;
26+
}
27+
28+
public void setCapital(String capital) {
29+
this.capital = capital;
30+
}
31+
32+
public int getCode() {
33+
return code;
34+
}
35+
36+
public void setCode(int code) {
37+
this.code = code;
38+
}
39+
40+
@Override
41+
public void writeExternal(ObjectOutput out) throws IOException {
42+
out.writeUTF(name);
43+
out.writeUTF(capital);
44+
out.writeInt(code);
45+
}
46+
47+
@Override
48+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
49+
this.name = in.readUTF();
50+
this.capital = in.readUTF();
51+
this.code = in.readInt();
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Country{" +
57+
"name='" + name + '\'' +
58+
", capital='" + capital + '\'' +
59+
", code=" + code +
60+
'}';
61+
}
62+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.externalizable;
2+
3+
import java.io.Externalizable;
4+
import java.io.IOException;
5+
import java.io.ObjectInput;
6+
import java.io.ObjectOutput;
7+
8+
public class Region extends Country implements Externalizable {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
private String climate;
13+
private Double population;
14+
private Community community;
15+
16+
public String getClimate() {
17+
return climate;
18+
}
19+
20+
public void setClimate(String climate) {
21+
this.climate = climate;
22+
}
23+
24+
public Double getPopulation() {
25+
return population;
26+
}
27+
28+
public void setPopulation(Double population) {
29+
this.population = population;
30+
}
31+
32+
@Override
33+
public void writeExternal(ObjectOutput out) throws IOException {
34+
super.writeExternal(out);
35+
out.writeUTF(climate);
36+
community = new Community();
37+
community.setId(5);
38+
out.writeObject(community);
39+
}
40+
41+
@Override
42+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
43+
super.readExternal(in);
44+
this.climate = in.readUTF();
45+
community = (Community) in.readObject();
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Region = {" +
51+
"country='" + super.toString() + '\'' +
52+
"community='" + community.toString() + '\'' +
53+
"climate='" + climate + '\'' +
54+
", population=" + population +
55+
'}';
56+
}
57+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.externalizable;
2+
3+
import org.junit.Test;
4+
5+
import java.io.*;
6+
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class ExternalizableUnitTest {
10+
11+
private final static String OUTPUT_FILE = "externalizable.txt";
12+
13+
@Test
14+
public void whenSerializing_thenUseExternalizable() throws IOException, ClassNotFoundException {
15+
16+
Country c = new Country();
17+
c.setCapital("Yerevan");
18+
c.setCode(374);
19+
c.setName("Armenia");
20+
21+
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
22+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
23+
c.writeExternal(objectOutputStream);
24+
25+
objectOutputStream.flush();
26+
objectOutputStream.close();
27+
fileOutputStream.close();
28+
29+
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
30+
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
31+
32+
Country c2 = new Country();
33+
c2.readExternal(objectInputStream);
34+
35+
objectInputStream.close();
36+
fileInputStream.close();
37+
38+
assertTrue(c2.getCode() == c.getCode());
39+
assertTrue(c2.getName().equals(c.getName()));
40+
}
41+
42+
@Test
43+
public void whenInheritanceSerialization_then_UseExternalizable() throws IOException, ClassNotFoundException {
44+
45+
Region r = new Region();
46+
r.setCapital("Yerevan");
47+
r.setCode(374);
48+
r.setName("Armenia");
49+
r.setClimate("Mediterranean");
50+
r.setPopulation(120.000);
51+
52+
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
53+
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
54+
r.writeExternal(objectOutputStream);
55+
56+
objectOutputStream.flush();
57+
objectOutputStream.close();
58+
fileOutputStream.close();
59+
60+
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
61+
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
62+
63+
Region r2 = new Region();
64+
r2.readExternal(objectInputStream);
65+
66+
objectInputStream.close();
67+
fileInputStream.close();
68+
69+
assertTrue(r2.getPopulation() == null);
70+
}
71+
}

0 commit comments

Comments
 (0)