下面列出了怎么用org.testng.collections.Sets的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testConcatenateForkNodes() {
DagNode<String> dagNode1 = new DagNode<>("val1");
DagNode<String> dagNode2 = new DagNode<>("val2");
DagNode<String> dagNode3 = new DagNode<>("val3");
dagNode2.addParentNode(dagNode1);
dagNode3.addParentNode(dagNode1);
Dag<String> dag1 = new Dag<>(Lists.newArrayList(dagNode1, dagNode2, dagNode3));
DagNode<String> dagNode4 = new DagNode<>("val4");
Dag<String> dag2 = new Dag<>(Lists.newArrayList(dagNode4));
Set<DagNode<String>> forkNodes = Sets.newHashSet();
forkNodes.add(dagNode3);
Dag<String> dagNew = dag1.concatenate(dag2, forkNodes);
Assert.assertEquals(dagNew.getChildren(dagNode2).size(), 1);
Assert.assertEquals(dagNew.getChildren(dagNode2).get(0), dagNode4);
Assert.assertEquals(dagNew.getParents(dagNode4).size(), 1);
Assert.assertEquals(dagNew.getParents(dagNode4).get(0), dagNode2);
Assert.assertEquals(dagNew.getEndNodes().size(), 2);
Assert.assertEquals(dagNew.getEndNodes().get(0).getValue(), "val4");
Assert.assertEquals(dagNew.getEndNodes().get(1).getValue(), "val3");
Assert.assertEquals(dagNew.getChildren(dagNode3).size(), 0);
}
private void assertHCC1143TestTrimmedGeneListFile(final File outputGeneList) throws IOException {
Assert.assertTrue(outputGeneList.exists());
// Test gene list
final Set<String> gtGenesToSee = Sets.newHashSet(Arrays.asList("CCDC40", "ENAH", "NPB", "PCSK5", "PCYT2"));
try (final TableReader<LinkedHashMap<String, String>> outputReader = createLinkedHashMapListTableReader(outputGeneList)) {
// Check that the ordering of the column is correct and that the values are all correct.
final List<LinkedHashMap<String, String>> geneListRecords = outputReader.toList();
// 5 genes, but one breakpoint in the middle of one of the genes, so a total of 6 entries.
Assert.assertEquals(geneListRecords.size(), 6);
final Set<String> genesSeen = geneListRecords.stream().map(r -> r.get("gene")).collect(Collectors.toSet());
Assert.assertEquals(genesSeen, gtGenesToSee);
}
}
@Test
public void defaultToEmptyCollectionsOnNullValueTest() throws Throwable {
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(List.class, null).isEmpty());
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(List.class, Lists.newArrayList("1")).contains("1"));
Set<Object> set = Sets.newHashSet();
set.add(1);
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Set.class, set).contains(1));
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Set.class, null).isEmpty());
Map<Object, Object> map = Maps.newHashMap();
map.put(1, 2);
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Map.class, map).containsKey(1));
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Map.class, map).containsValue(2));
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Map.class, null).isEmpty());
Object[] arr = set.toArray(new Object[set.size()]);
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Object[].class, null).length == 0);
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Object[].class, arr).length == 1);
Properties prop = new Properties();
prop.put("1", "2");
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Properties.class, prop).containsKey("1"));
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Properties.class, prop).containsValue("2"));
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Properties.class, null).isEmpty());
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Collection.class, null).isEmpty());
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(Collection.class, set).contains(1));
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(String.class, null) == null);
assertTrue(Util.defaultToEmptyCollectionsOnNullValue(String.class, "123").contentEquals("123"));
}
/**
* Extracts the unique list of <code>ITestNGMethod</code>s.
*/
public static List<ITestNGMethod> uniqueMethodList(Collection<List<ITestNGMethod>> methods) {
Set<ITestNGMethod> resultSet = Sets.newHashSet();
for (List<ITestNGMethod> l : methods) {
resultSet.addAll(l);
}
return Lists.newArrayList(resultSet);
}
/** A Method that returns all the Methods that are annotated with @DataProvider
* in a given package. Should be moved to htsjdk and used from there
*
* @param packageName the package under which to look for classes and methods
* @return an iterator to collection of Object[]'s consisting of {Method method, Class clazz} pair.
* where method has the @DataProviderAnnotation and is a member of clazz.
*/
public static Iterator<Object[]> getDataProviders(final String packageName) {
List<Object[]> data = new ArrayList<>();
final ClassFinder classFinder = new ClassFinder();
classFinder.find(packageName, Object.class);
for (final Class<?> testClass : classFinder.getClasses()) {
final int modifiers;
final Method[] declaredMethods;
final Method[] methods;
try {
modifiers = testClass.getModifiers();
declaredMethods = testClass.getDeclaredMethods();
methods = testClass.getMethods();
} catch (final NoClassDefFoundError e){
continue;
}
if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers))
continue;
Set<Method> methodSet = Sets.newHashSet();
methodSet.addAll(Arrays.asList(declaredMethods));
methodSet.addAll(Arrays.asList(methods));
for (final Method method : methodSet) {
if (method.isAnnotationPresent(DataProvider.class)) {
data.add(new Object[]{method, testClass});
}
}
}
return data.iterator();
}
@Test
public void basicTest() throws IOException {
final Set<String> headersOfInterest = Sets.newHashSet(Arrays.asList("name", "learning_SAMPLE_0"));
final List<AnnotatedInterval> annotatedIntervals =
AnnotatedIntervalCollection.create(TEST_FILE.toPath(), headersOfInterest).getRecords();
Assert.assertEquals(annotatedIntervals.size(), 15);
Assert.assertTrue(annotatedIntervals.stream()
.mapToInt(s -> s.getAnnotations().entrySet().size())
.allMatch(i -> i == headersOfInterest.size()));
Assert.assertTrue(annotatedIntervals.stream().allMatch(s -> s.getAnnotations().keySet().containsAll(headersOfInterest)));
// Grab the first 15 and test values
List<AnnotatedInterval> gtRegions = Arrays.asList(
new AnnotatedInterval(new SimpleInterval("1", 30365, 30503), ImmutableSortedMap.of("name", "target_1_None", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 69088, 70010), ImmutableSortedMap.of("name", "target_2_None", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 367656, 368599), ImmutableSortedMap.of("name", "target_3_None", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 621093, 622036), ImmutableSortedMap.of("name", "target_4_None", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 861319, 861395), ImmutableSortedMap.of("name", "target_5_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 865532, 865718), ImmutableSortedMap.of("name", "target_6_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 866416, 866471), ImmutableSortedMap.of("name", "target_7_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 871149, 871278), ImmutableSortedMap.of("name", "target_8_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 874417, 874511), ImmutableSortedMap.of("name", "target_9_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 874652, 874842), ImmutableSortedMap.of("name", "target_10_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 876521, 876688), ImmutableSortedMap.of("name", "target_11_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 877513, 877633), ImmutableSortedMap.of("name", "target_12_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 877787, 877870), ImmutableSortedMap.of("name", "target_13_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 877936, 878440), ImmutableSortedMap.of("name", "target_14_SAMD11", "learning_SAMPLE_0", "2")),
new AnnotatedInterval(new SimpleInterval("1", 878630, 878759), ImmutableSortedMap.of("name", "target_15_SAMD11", "learning_SAMPLE_0", "2"))
);
Assert.assertEquals(annotatedIntervals.subList(0, gtRegions.size()), gtRegions);
}
private void initListeners() {
//
// Find all the listener factories and collect all the listeners requested in a
// @Listeners annotation.
//
Set<Class<? extends ITestNGListener>> listenerClasses = Sets.newHashSet();
Class<? extends ITestNGListenerFactory> listenerFactoryClass = null;
for (IClass cls : getTestClasses()) {
Class<?> realClass = cls.getRealClass();
ListenerHolder listenerHolder = findAllListeners(realClass);
if (listenerFactoryClass == null) {
listenerFactoryClass = listenerHolder.listenerFactoryClass;
}
listenerClasses.addAll(listenerHolder.listenerClasses);
}
//
// Now we have all the listeners collected from @Listeners and at most one
// listener factory collected from a class implementing ITestNGListenerFactory.
// Instantiate all the requested listeners.
//
ITestNGListenerFactory listenerFactory = null;
// If we found a test listener factory, instantiate it.
try {
if (m_testClassFinder != null) {
IClass ic = m_testClassFinder.getIClass(listenerFactoryClass);
if (ic != null) {
listenerFactory = (ITestNGListenerFactory) ic.getInstances(false)[0];
}
}
if (listenerFactory == null) {
listenerFactory = listenerFactoryClass != null ? listenerFactoryClass.newInstance() : null;
}
}
catch(Exception ex) {
throw new TestNGException("Couldn't instantiate the ITestNGListenerFactory: "
+ ex);
}
// Instantiate all the listeners
for (Class<? extends ITestNGListener> c : listenerClasses) {
if (IClassListener.class.isAssignableFrom(c) && m_classListeners.containsKey(c)) {
continue;
}
ITestNGListener listener = listenerFactory != null ? listenerFactory.createListener(c) : null;
if (listener == null) {
listener = ClassHelper.newInstance(c);
}
addListener(listener);
}
}