JAL-3438 spotless for 2.11.2.0
[jalview.git] / test / jalview / datamodel / AllRowsIteratorTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import static org.testng.Assert.assertTrue;
24
25 import jalview.analysis.AlignmentGenerator;
26
27 import java.util.Hashtable;
28 import java.util.NoSuchElementException;
29
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
32
33 public class AllRowsIteratorTest
34 {
35   AlignmentI al;
36
37   Hashtable<SequenceI, SequenceCollectionI> hiddenRepSequences = new Hashtable<>();
38
39   @BeforeClass(alwaysRun = true)
40   public void setup()
41   {
42     // create random alignment
43     AlignmentGenerator gen = new AlignmentGenerator(false);
44     al = gen.generate(20, 15, 123, 5, 5);
45     if (!hiddenRepSequences.isEmpty())
46     {
47       al.getHiddenSequences().showAll(hiddenRepSequences);
48     }
49     hideSequences(2, 4);
50   }
51
52   /*
53    * Test iterator iterates through collection correctly
54    */
55   @Test(groups = { "Functional" })
56   public void testHasNextAndNext()
57   {
58     AllRowsIterator it = new AllRowsIterator(0, 3, al);
59     int count = 0;
60     while (it.hasNext())
61     {
62       it.next();
63       count++;
64     }
65     assertTrue(count == 4, "hasNext() is false after 4 iterations");
66   }
67
68   /*
69    * Test iterator throws NoSuchElementException at end of iteration
70    */
71   @Test(
72     groups =
73     { "Functional" },
74     expectedExceptions =
75     { NoSuchElementException.class })
76   public void testLastNext() throws NoSuchElementException
77   {
78     AllRowsIterator it = new AllRowsIterator(0, 3, al);
79     while (it.hasNext())
80     {
81       it.next();
82     }
83     it.next();
84   }
85
86   /*
87    * Test iterator throws UnsupportedOperationException on call to remove
88    */
89   @Test(
90     groups =
91     { "Functional" },
92     expectedExceptions =
93     { UnsupportedOperationException.class })
94   public void testRemove() throws UnsupportedOperationException
95   {
96     AllRowsIterator it = new AllRowsIterator(0, 3, al);
97     it.remove();
98   }
99
100   /*
101    * Hide sequences between start and end
102    */
103   private void hideSequences(int start, int end)
104   {
105     SequenceI[] allseqs = al.getSequencesArray();
106     SequenceGroup theseSeqs = new SequenceGroup();
107
108     for (int i = start; i <= end; i++)
109     {
110       theseSeqs.addSequence(allseqs[i], false);
111       al.getHiddenSequences().hideSequence(allseqs[i]);
112     }
113
114     hiddenRepSequences.put(allseqs[start], theseSeqs);
115   }
116
117   /*
118    * Test iterator behaves correctly when there is only one element in the collection
119    */
120   @Test(groups = { "Functional" })
121   public void testOneElement()
122   {
123     AllRowsIterator it = new AllRowsIterator(0, 0, al);
124     int count = 0;
125     while (it.hasNext())
126     {
127       it.next();
128       count++;
129     }
130     assertTrue(count == 1, "hasNext() is false after 1 iteration");
131   }
132
133 }