View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package test.de.smartics.properties.spi.config.ds;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  import static org.mockito.Mockito.atLeast;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.when;
25  
26  import java.sql.Connection;
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  import java.util.Iterator;
30  import java.util.NoSuchElementException;
31  
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  import de.smartics.properties.api.config.domain.Property;
38  import de.smartics.properties.api.config.domain.PropertyLocation;
39  import de.smartics.properties.spi.config.ds.DsPropertyCollection;
40  import de.smartics.testdoc.annotations.Uut;
41  import de.smartics.testdoc.categories.Technical;
42  
43  /**
44   * Tests {@link DsPropertyCollection}.
45   */
46  @Uut(type = DsPropertyCollection.class)
47  public class DsPropertyCollectionTest
48  {
49    // ********************************* Fields *********************************
50  
51    // --- constants ------------------------------------------------------------
52  
53    private static final String DATA_SOURCE_ID = "test-ds";
54  
55    private static final PropertyLocation TEST_SOURCE = new PropertyLocation(
56        DATA_SOURCE_ID);
57  
58    // --- members --------------------------------------------------------------
59  
60    private Connection connection;
61  
62    // ****************************** Inner Classes *****************************
63  
64    // ********************************* Methods ********************************
65  
66    // --- prepare --------------------------------------------------------------
67  
68    @Before
69    public void setUp()
70    {
71      connection = mock(Connection.class);
72    }
73  
74    @After
75    public void tearDown() throws Exception
76    {
77      verify(connection, atLeast(1)).close();
78    }
79  
80    // --- helper ---------------------------------------------------------------
81  
82    // --- tests ----------------------------------------------------------------
83  
84    @Test(expected = NoSuchElementException.class)
85    public void signalsEmptyIterationIfResultSetProvidesNoHits()
86      throws SQLException
87    {
88      final ResultSet resultSet = mock(ResultSet.class);
89      when(resultSet.next()).thenReturn(false);
90  
91      final DsPropertyCollection uut =
92          new DsPropertyCollection(DATA_SOURCE_ID, connection, resultSet);
93      final Iterator<Property> i = uut.iterator();
94      assertThat(i.hasNext(), is(false));
95      i.next();
96    }
97  
98    @Category(Technical.class)
99    @Test
100   public void allowsToCheckForMoreElementsAnyNumberOfTimes()
101     throws SQLException
102   {
103     final Property expectedProperty = new Property(TEST_SOURCE, "key", "value"); // NOPMD
104 
105     final ResultSet resultSet = mock(ResultSet.class);
106     when(resultSet.next()).thenReturn(true, false);
107     when(resultSet.getString(1)).thenReturn(expectedProperty.getName());
108     when(resultSet.getString(2)).thenReturn(expectedProperty.getValue());
109 
110     final DsPropertyCollection uut =
111         new DsPropertyCollection(DATA_SOURCE_ID, connection, resultSet);
112     final Iterator<Property> i = uut.iterator();
113     assertThat(i.hasNext(), is(true));
114     assertThat(i.hasNext(), is(true));
115 
116     uut.close();
117   }
118 
119   @Category(Technical.class)
120   @Test(expected = NoSuchElementException.class)
121   public void allowsToIterateOverOneProperty() throws SQLException
122   {
123     final Property expectedProperty = new Property(TEST_SOURCE, "key", "value");
124 
125     final ResultSet resultSet = mock(ResultSet.class);
126     when(resultSet.next()).thenReturn(true, false);
127     when(resultSet.getString(1)).thenReturn(expectedProperty.getName());
128     when(resultSet.getString(2)).thenReturn(expectedProperty.getValue());
129 
130     final DsPropertyCollection uut =
131         new DsPropertyCollection(DATA_SOURCE_ID, connection, resultSet);
132     final Iterator<Property> i = uut.iterator();
133     assertThat(i.hasNext(), is(true));
134     final Property property = i.next();
135     assertThat(property, is(equalTo(expectedProperty)));
136 
137     assertThat(i.hasNext(), is(false));
138     i.next();
139   }
140 
141   @Test(expected = NoSuchElementException.class)
142   public void allowsToIterateOverProperties() throws SQLException
143   {
144     final Property expectedProperty1 =
145         new Property(TEST_SOURCE, "key1", "value1");
146     final Property expectedProperty2 =
147         new Property(TEST_SOURCE, "key2", "value2");
148     final Property expectedProperty3 =
149         new Property(TEST_SOURCE, "key3", "value3");
150 
151     final ResultSet resultSet = mock(ResultSet.class);
152     when(resultSet.next()).thenReturn(true, true, true, false);
153     when(resultSet.getString(1)).thenReturn(expectedProperty1.getName(),
154         expectedProperty2.getName(), expectedProperty3.getName());
155     when(resultSet.getString(2)).thenReturn(expectedProperty1.getValue(),
156         expectedProperty2.getValue(), expectedProperty3.getValue());
157 
158     final DsPropertyCollection uut =
159         new DsPropertyCollection(DATA_SOURCE_ID, connection, resultSet);
160     final Iterator<Property> i = uut.iterator();
161     assertThat(i.hasNext(), is(true));
162     assertThat(i.next(), is(equalTo(expectedProperty1)));
163     assertThat(i.hasNext(), is(true));
164     assertThat(i.next(), is(equalTo(expectedProperty2)));
165     assertThat(i.hasNext(), is(true));
166     assertThat(i.next(), is(equalTo(expectedProperty3)));
167 
168     assertThat(i.hasNext(), is(false));
169     i.next();
170   }
171 
172   @Test(expected = UnsupportedOperationException.class)
173   public void removingFromTheIteratorIsNotSupported() throws SQLException
174   {
175     final Property expectedProperty = new Property(TEST_SOURCE, "key", "value");
176 
177     final ResultSet resultSet = mock(ResultSet.class);
178     when(resultSet.next()).thenReturn(true, false);
179     when(resultSet.getString(1)).thenReturn(expectedProperty.getName());
180     when(resultSet.getString(2)).thenReturn(expectedProperty.getValue());
181 
182     final DsPropertyCollection uut =
183         new DsPropertyCollection(DATA_SOURCE_ID, connection, resultSet);
184     try
185     {
186       final Iterator<Property> i = uut.iterator();
187       i.remove();
188     }
189     finally
190     {
191       uut.close();
192     }
193   }
194 
195   @Category(Technical.class)
196   @Test(expected = IllegalStateException.class)
197   public void signalsUnderlyingProblemsWithAnIllegalStateException()
198     throws SQLException
199   {
200     final Property expectedProperty = new Property(TEST_SOURCE, "key", "value");
201 
202     final ResultSet resultSet = mock(ResultSet.class);
203     when(resultSet.next()).thenReturn(true, false);
204     when(resultSet.getString(1)).thenThrow(new SQLException("Saboteur!"));
205     when(resultSet.getString(2)).thenReturn(expectedProperty.getValue());
206 
207     final DsPropertyCollection uut =
208         new DsPropertyCollection(DATA_SOURCE_ID, connection, resultSet);
209     final Iterator<Property> i = uut.iterator();
210     i.hasNext();
211   }
212 }