View Javadoc
1   package de.japrost.jabudget.serialization;
2   
3   import static org.assertj.core.api.Assertions.assertThat;
4   
5   import java.io.File;
6   
7   import org.junit.After;
8   import org.junit.Before;
9   import org.junit.Test;
10  
11  /**
12   * Test the {@link LazyFileOutputStream}.
13   */
14  // TODO more tests when moving to io package.
15  public class LazyFileOutputStreamTest {
16  
17  	private LazyFileOutputStream cut;
18  	private LazyFileInputStream helper;
19  
20  	/**
21  	 * Set cut and helping reader with a file.
22  	 */
23  	@Before
24  	public void initCut() {
25  		final File file = new File("target/LazyFile.txt");
26  		cut = new LazyFileOutputStream(file);
27  		helper = new LazyFileInputStream(file);
28  	}
29  
30  	/**
31  	 * Be kind and close before exit.
32  	 *
33  	 * @throws Exception never.
34  	 */
35  	@After
36  	public void closeCut() throws Exception {
37  		cut.close();
38  		helper.close();
39  	}
40  
41  	/**
42  	 * Test that write() opens the delegate and delegates.
43  	 *
44  	 * @throws Exception never.
45  	 */
46  	@Test
47  	public void testWrite() throws Exception {
48  		// given
49  		assertThat(cut.isOpen()).isFalse();
50  		// when
51  		cut.write(97);
52  		// then
53  		assertThat(cut.isOpen()).isTrue();
54  		assertThat(helper.read()).isEqualTo(97);
55  		assertThat(helper.read()).isEqualTo(-1);
56  	}
57  
58  	/**
59  	 * Test that write() opens the delegate and delegates even two times.
60  	 *
61  	 * @throws Exception never.
62  	 */
63  	@Test
64  	public void testWrite_twice() throws Exception {
65  		// given
66  		assertThat(cut.isOpen()).isFalse();
67  		// when
68  		cut.write(97);
69  		cut.write(32);
70  		// then
71  		assertThat(cut.isOpen()).isTrue();
72  		assertThat(helper.read()).isEqualTo(97);
73  		assertThat(helper.read()).isEqualTo(32);
74  		assertThat(helper.read()).isEqualTo(-1);
75  	}
76  
77  }