File tree Expand file tree Collapse file tree
core-java-modules/core-java-lang-3/src
main/java/com/baeldung/transientkw
test/java/com/baeldung/transientkw Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .transientkw ;
2+
3+ import java .io .Serializable ;
4+
5+ public class Book implements Serializable {
6+
7+ private static final long serialVersionUID = -2936687026040726549L ;
8+
9+ private String bookName ;
10+ private transient String description ;
11+ private transient int copies ;
12+ private final transient String bookCategory = "Fiction" ;
13+
14+ public String getBookName () {
15+ return bookName ;
16+ }
17+
18+ public void setBookName (String bookName ) {
19+ this .bookName = bookName ;
20+ }
21+
22+ public String getDescription () {
23+ return description ;
24+ }
25+
26+ public void setDescription (String description ) {
27+ this .description = description ;
28+ }
29+
30+ public int getCopies () {
31+ return copies ;
32+ }
33+
34+ public void setCopies (int copies ) {
35+ this .copies = copies ;
36+ }
37+
38+ public String getBookCategory () {
39+ return bookCategory ;
40+ }
41+
42+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .transientkw ;
2+
3+ import java .io .FileInputStream ;
4+ import java .io .FileNotFoundException ;
5+ import java .io .FileOutputStream ;
6+ import java .io .IOException ;
7+ import java .io .ObjectInputStream ;
8+ import java .io .ObjectOutputStream ;
9+
10+ public class BookSerDe {
11+ static String fileName = "book.ser" ;
12+
13+ /**
14+ * Method to serialize Book objects to the file
15+ * @throws FileNotFoundException
16+ */
17+ public static void serialize (Book book ) throws Exception {
18+ FileOutputStream file = new FileOutputStream (fileName );
19+ ObjectOutputStream out = new ObjectOutputStream (file );
20+
21+ out .writeObject (book );
22+
23+ out .close ();
24+ file .close ();
25+ }
26+
27+ /**
28+ * Method to deserialize the person object
29+ * @return book
30+ * @throws IOException, ClassNotFoundException
31+ */
32+ public static Book deserialize () throws Exception {
33+ FileInputStream file = new FileInputStream (fileName );
34+ ObjectInputStream in = new ObjectInputStream (file );
35+
36+ Book book = (Book ) in .readObject ();
37+
38+ in .close ();
39+ file .close ();
40+
41+ return book ;
42+ }
43+
44+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .transientkw ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertNull ;
5+
6+ import org .junit .jupiter .api .Test ;
7+
8+ class TransientUnitTest {
9+
10+ @ Test
11+ void givenTransient_whenSerDe_thenVerifyValues () throws Exception {
12+ Book book = new Book ();
13+ book .setBookName ("Java Reference" );
14+ book .setDescription ("will not be saved" );
15+ book .setCopies (25 );
16+
17+ BookSerDe .serialize (book );
18+ Book book2 = BookSerDe .deserialize ();
19+
20+ assertEquals ("Java Reference" , book2 .getBookName ());
21+ assertNull (book2 .getDescription ());
22+ assertEquals (0 , book2 .getCopies ());
23+ }
24+
25+ @ Test
26+ void givenFinalTransient_whenSerDe_thenValuePersisted () throws Exception {
27+ Book book = new Book ();
28+
29+ BookSerDe .serialize (book );
30+ Book book2 = BookSerDe .deserialize ();
31+
32+ assertEquals ("Fiction" , book2 .getBookCategory ());
33+ }
34+
35+ }
You can’t perform that action at this time.
0 commit comments