1+ package com .urise .webapp .storage ;
2+
3+ import com .urise .webapp .exception .ExistStorageException ;
4+ import com .urise .webapp .exception .NotExistStorageException ;
5+ import com .urise .webapp .exception .StorageException ;
6+ import com .urise .webapp .model .Resume ;
7+ import org .junit .Before ;
8+ import org .junit .Test ;
9+
10+
11+ import static com .urise .webapp .storage .AbstractArrayStorage .MAX_SIZE ;
12+ import static org .junit .Assert .*;
13+
14+ public abstract class AbstractStorageTest {
15+ private static final String UUID_1 = "UUID_1" ;
16+ private static final String UUID_2 = "UUID_2" ;
17+ private static final String UUID_3 = "UUID_3" ;
18+ private static final String UUID_4 = "UUID_4" ;
19+ private static final Resume R_1 = new Resume (UUID_1 );
20+ private static final Resume R_2 = new Resume (UUID_2 );
21+ private static final Resume R_4 = new Resume (UUID_4 );
22+ protected Storage storage ;
23+ private static final Resume R_3 = new Resume (UUID_3 );
24+
25+ protected AbstractStorageTest (Storage storage ) {
26+ this .storage = storage ;
27+ }
28+
29+ /*создается перед каждым методом*/
30+ @ Before
31+ public void setUp () {
32+ storage .clear ();
33+ storage .save (R_1 );
34+ storage .save (R_2 );
35+ storage .save (R_3 );
36+ }
37+
38+ @ Test
39+ public void clear () {
40+ storage .clear ();
41+ assertEquals (0 , storage .size ());
42+ }
43+
44+ @ Test (expected = ExistStorageException .class )
45+ public void saveExist () {
46+ storage .save (R_3 );
47+ }
48+
49+ @ Test
50+ public void saveNotExist () {
51+ int sizeBefore = storage .size ();
52+ storage .save (R_4 );
53+ assertEquals (storage .size (), sizeBefore + 1 );
54+ assertEquals (R_4 , storage .get (UUID_4 ));
55+ }
56+
57+ @ Test
58+ public void getExist () {
59+ assertEquals (storage .get (UUID_1 ), R_1 );
60+ assertEquals (storage .get (UUID_2 ), R_2 );
61+ assertEquals (storage .get (UUID_3 ), R_3 );
62+ }
63+
64+ @ Test (expected = NotExistStorageException .class )
65+ public void getNotExist () {
66+ storage .get ("notExistableUUID" );
67+ }
68+
69+ @ Test (expected = NotExistStorageException .class )
70+ public void deleteExist () {
71+ int sizeBefore = storage .size ();
72+ storage .delete (UUID_1 );
73+ assertEquals (storage .size (), sizeBefore - 1 );
74+ storage .get (UUID_1 );
75+
76+ }
77+
78+ @ Test (expected = NotExistStorageException .class )
79+ public void deleteNotExist () {
80+ storage .delete ("notExistableUUID" );
81+ }
82+
83+ @ Test
84+ public void getAll () {
85+ assertArrayEquals ( new Resume []{R_1 , R_2 , R_3 },storage .getAll ());
86+ }
87+
88+ @ Test
89+ public void size () {
90+ assertEquals (storage .size (), 3 );
91+ }
92+
93+ @ Test (expected = AssertionError .class )
94+ public void sizeOverLoaded () {
95+ try {
96+ if (!(storage instanceof AbstractArrayStorage )) {
97+ throw new StorageException ("Is not instance of AbstractArrayStorage" ,null );
98+ }
99+ for (int i = 4 ; i <= MAX_SIZE ; i ++) {
100+ storage .save (new Resume ());
101+ }
102+ storage .save (new Resume ());
103+ } catch (StorageException e ) {
104+ fail ();
105+ }
106+
107+ }
108+
109+ @ Test
110+ public void updateExist () {
111+ Resume rBefore = storage .get (UUID_1 );
112+ Resume rAfter = new Resume (UUID_1 );
113+ storage .update (rAfter );
114+ assertNotSame (storage .get (UUID_1 ), rBefore );
115+ }
116+
117+ @ Test (expected = NotExistStorageException .class )
118+ public void updateNotExist () {
119+ Resume rAfter = new Resume ("test" );
120+ storage .update (rAfter );
121+ }
122+ }
0 commit comments