Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8be1f8cee5e5553bc3bb50ceeaba1668802b174c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.type;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.MockitoAnnotations;

/**
 * Test Case for {@link ResultSetIterable}
 * 
 * @author Roberto E. Escobar
 */
public class ResultSetIterableTest {

   @Rule
   public ExpectedException thrown = ExpectedException.none();

   private ResultSet<String> result;
   private Set<String> data;

   @Before
   public void init() {
      MockitoAnnotations.initMocks(this);

      data = new LinkedHashSet<>();
      result = new ResultSetIterable<>(data);
   }

   @Test
   public void testSizeAndEmpty() {
      data.add("a");
      data.add("b");
      data.add("c");

      assertFalse(result.isEmpty());
      assertEquals(3, result.size());

      data.clear();
      assertTrue(result.isEmpty());
      assertEquals(0, result.size());

      result.iterator();
   }

   @Test
   public void testGetExactlyOneMultipleException() throws OseeCoreException {
      data.add("a");
      data.add("b");

      thrown.expect(MultipleItemsExist.class);
      thrown.expectMessage("Multiple items found - total [2]");
      result.getExactlyOne();
   }

   @Test
   public void testGetExactlyOneNoneExistException() throws OseeCoreException {
      thrown.expect(ItemDoesNotExist.class);
      thrown.expectMessage("No item found");
      result.getExactlyOne();
   }

   @Test
   public void testGetExactlyOne() throws OseeCoreException {
      data.add("c");
      assertEquals("c", result.getExactlyOne());
   }

   @Test
   public void testGetOneOrNull() throws OseeCoreException {
      Assert.assertNull(result.getOneOrNull());

      data.add("a");
      data.add("b");
      data.add("c");
      assertEquals("a", result.getOneOrNull());
   }

   @Test
   public void testGetAtMostOneOrNullExceptionMoreThanOne() throws OseeCoreException {
      assertNull(result.getAtMostOneOrNull());

      data.add("a");
      data.add("b");
      data.add("c");
      thrown.expect(MultipleItemsExist.class);
      thrown.expectMessage("Multiple items found - total [3]");
      assertEquals("a", result.getAtMostOneOrNull());
   }

   @Test
   public void testGetAtMostOneOrNull() throws OseeCoreException {
      assertNull(result.getAtMostOneOrNull());

      data.add("a");
      assertEquals("a", result.getAtMostOneOrNull());
   }

   @Test
   public void testIterator() {
      data.add("a");
      data.add("b");
      data.add("c");

      Iterator<String> iterator = result.iterator();
      assertEquals("a", iterator.next());
      assertEquals("b", iterator.next());
      assertEquals("c", iterator.next());
   }

}

Back to the top