Commit 5fe5b717 authored by David Trachtenherz's avatar David Trachtenherz
Browse files

JUnit test for SubComponentIterator removed

refs 779
parent d7b7238a
/*--------------------------------------------------------------------------+
$Id: BasicAutomatonSimulatorTest.java 1640 2011-10-16 16:40:10Z hoelzl $
| |
| Copyright 2011 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package test.org.fortiss.af3.component.common;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.component.utils.ComponentArchitectureUtils;
import org.fortiss.af3.component.utils.ComponentArchitectureUtils.SubComponentIterator;
import org.junit.Assert;
import org.junit.Test;
import test.org.fortiss.af3.component.model.SubComponentIteratorTestModel;
import test.org.fortiss.af3.testbase.ModelTestBase;
/**
* Test class for {@link SubComponentIterator}.
*
* @author trachtenherz
* @version $Rev: 1640 $
* @ConQAT.Rating YELLOW Hash: C98CBE7AB97F2B48CF1FF940608BD647
*/
public class SubComponentIteratorAndFindingTest extends ModelTestBase<SubComponentIteratorTestModel> {
/** Constructor. */
public SubComponentIteratorAndFindingTest() {
super(new SubComponentIteratorTestModel());
}
/**
* Tests the sub-component iterator.
*/
@Test
public void testSubComponentIterator() {
printCompNames(new SubComponentIterator(testModel.rootComponent));
printCompNames(new SubComponentIterator(testModel.rootComponent, true));
Iterator<Component> subCompIterator = new SubComponentIterator(testModel.rootComponent);
assertTrue(checkIfAllSubComponentsIterated(subCompIterator));
Iterator<Component> subCompIteratorWithRoot =
new SubComponentIterator(testModel.rootComponent, true);
Assert.assertEquals(testModel.rootComponent, subCompIteratorWithRoot.next());
assertTrue(checkIfAllSubComponentsIterated(subCompIteratorWithRoot));
}
/**
* Tests finding sub-components recursively.
*/
@Test
public void testFindSubComponentsRecursively() {
Iterator<Component> subCompIterator = new SubComponentIterator(testModel.rootComponent);
Collection<String> toBeFound = new ArrayList<String>();
while(subCompIterator.hasNext()) {
Component c = subCompIterator.next();
// Randomly select about a half of all sub-components
if(Math.random() < 0.5) {
toBeFound.add(c.getName());
}
}
Collection<String> notFoundCompNames = new ArrayList<String>();
Map<String, Component> foundMap =
ComponentArchitectureUtils.findSubComponentsRecursively(testModel.rootComponent,
toBeFound, notFoundCompNames);
// All sub-components have been found
assertTrue(foundMap.size() == toBeFound.size());
assertTrue(notFoundCompNames.isEmpty());
// Test handling not found sub-components
Collection<String> notFoundCompNames2 = new ArrayList<String>();
Collection<String> toBeFound2 = new ArrayList<String>(toBeFound);
String[] nonExistingComps = new String[] {"SomeComp1", "SomeComp2", "SomeComp3"};
toBeFound2.addAll(Arrays.asList(nonExistingComps));
Map<String, Component> foundMap2 =
ComponentArchitectureUtils.findSubComponentsRecursively(testModel.rootComponent,
toBeFound2, notFoundCompNames2);
// All existing sub-components found again
assertTrue(foundMap2.size() == foundMap.size());
// All non-existing components not found
for(String name : nonExistingComps) {
assertTrue(notFoundCompNames2.contains(name));
}
}
/**
* Checks if all sub-components in the model have been returned by the iterator.
*/
public boolean checkIfAllSubComponentsIterated(Iterator<Component> iterator) {
Set<Component> subComps = new HashSet<Component>(testModel.subComponents);
while(iterator.hasNext()) {
Component c = iterator.next();
if(subComps.contains(c)) {
subComps.remove(c);
} else {
Assert.fail("Component " + c.getName() +
" returned more than once or not a sub-component.");
}
}
if(subComps.isEmpty()) {
// All sub-components have been returned by the iterator
return true;
}
return false;
}
/**
* Prints the names of all component returned by the {@code iterator}.
*/
public void printCompNames(Iterator<Component> iterator) {
while(iterator.hasNext()) {
Component c = iterator.next();
System.out.println(c.getName());
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment