2020
2121package org .spine3 .server .storage ;
2222
23+ import com .google .common .base .Throwables ;
2324import com .google .protobuf .Message ;
2425import org .junit .After ;
2526import org .junit .Before ;
@@ -68,9 +69,9 @@ public void tearDownAbstractStorageTest() {
6869 protected abstract I newId ();
6970
7071 /**
71- * Closes the storage.
72+ * Closes the storage and propagates an exception if any occurs .
7273 *
73- * @throws RuntimeException which wraps the primary one if occurs
74+ * @see Throwables#propagate(Throwable)
7475 */
7576 protected void close (AbstractStorage storage ) {
7677 if (storage .isOpen ()) {
@@ -82,6 +83,27 @@ protected void close(AbstractStorage storage) {
8283 }
8384 }
8485
86+ /** Closes the storage and fails the test if any exception occurs. */
87+ protected void closeAndFailIfException (AbstractStorage <I , R > storage ) {
88+ try {
89+ storage .close ();
90+ } catch (Exception e ) {
91+ //noinspection CallToPrintStackTrace
92+ e .printStackTrace ();
93+ fail ("An unexpected exception: " + e .getClass () + "; " + e .getMessage ());
94+ }
95+ }
96+
97+ /** Writes a record, reads it and asserts it is the same as the expected one. */
98+ protected void writeAndReadRecordTest (I id ) {
99+ final R expected = newStorageRecord ();
100+ storage .write (id , expected );
101+
102+ final R actual = storage .read (id );
103+
104+ assertEquals (expected , actual );
105+ }
106+
85107 @ Test
86108 public void return_default_record_instance_if_no_record_with_such_id () {
87109 final R record = storage .read (newId ());
@@ -123,25 +145,11 @@ public void rewrite_record_if_write_by_the_same_id() {
123145 writeAndReadRecordTest (id );
124146 }
125147
126- protected void writeAndReadRecordTest (I id ) {
127- final R expected = newStorageRecord ();
128- storage .write (id , expected );
129-
130- final R actual = storage .read (id );
131-
132- assertEquals (expected , actual );
133- }
134-
135- @ Test
136- public void throw_exception_if_it_is_closed_on_check () throws Exception {
137- storage .close ();
148+ @ Test (expected = IllegalStateException .class )
149+ public void assure_it_is_closed () throws Exception {
150+ closeAndFailIfException (storage );
138151
139- try {
140- storage .checkNotClosed ();
141- fail ("An exception must be thrown." );
142- } catch (IllegalStateException e ) {
143- // is OK because it is closed
144- }
152+ storage .checkNotClosed ();
145153 }
146154
147155 @ Test
@@ -173,27 +181,24 @@ public void return_false_if_it_not_closed() throws Exception {
173181 assertFalse (storage .isClosed ());
174182 }
175183
176- @ Test
177- public void close_itself_and_throw_exception_if_read_then () throws Exception {
178- storage . close ( );
184+ @ Test ( expected = IllegalStateException . class )
185+ public void close_itself_and_throw_exception_if_read_after () throws Exception {
186+ closeAndFailIfException ( storage );
179187
180- try {
181- storage .read (newId ());
182- fail ("An exception must be thrown on attempt to read." );
183- } catch (IllegalStateException e ) {
184- // is OK because storage is closed
185- }
188+ storage .read (newId ());
186189 }
187190
188- @ Test
189- public void close_itself_and_throw_exception_if_write_then () throws Exception {
190- storage . close ( );
191+ @ Test ( expected = IllegalStateException . class )
192+ public void close_itself_and_throw_exception_if_write_after () throws Exception {
193+ closeAndFailIfException ( storage );
191194
192- try {
193- storage .write (newId (), newStorageRecord ());
194- fail ("An exception must be thrown on attempt to write." );
195- } catch (IllegalStateException e ) {
196- // is OK because storage is closed
197- }
195+ storage .write (newId (), newStorageRecord ());
196+ }
197+
198+ @ Test (expected = IllegalStateException .class )
199+ public void throw_exception_if_close_twice () {
200+ closeAndFailIfException (storage );
201+
202+ storage .read (newId ());
198203 }
199204}
0 commit comments