2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.util.List;
15 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
16 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
17 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
18 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
19 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
22 public class SupportTestUtil {
24 public static void containsMethods(final GeneratedType genType, final NameTypePattern... searchedSignsWhat) {
25 final List<MethodSignature> searchedSignsIn = genType.getMethodDefinitions();
26 containsMethods(searchedSignsIn, searchedSignsWhat);
29 public static void containsMethods(final List<MethodSignature> searchedSignsIn,
30 final NameTypePattern... searchedSignsWhat) {
31 if (searchedSignsIn == null) {
32 throw new IllegalArgumentException("List of method signatures in which should be searched can't be null");
34 if (searchedSignsWhat == null) {
35 throw new IllegalArgumentException("Array of method signatures which should be searched can't be null");
38 for (NameTypePattern searchedSignWhat : searchedSignsWhat) {
39 boolean nameMatchFound = false;
40 String typeNameFound = "";
41 for (MethodSignature searchedSignIn : searchedSignsIn) {
42 if (searchedSignWhat.getName().equals(searchedSignIn.getName())) {
43 nameMatchFound = true;
44 typeNameFound = resolveFullNameOfReturnType(searchedSignIn.getReturnType());
45 if (searchedSignWhat.getType().equals(typeNameFound)) {
50 assertTrue("Method " + searchedSignWhat.getName() + " wasn't found.", nameMatchFound);
51 assertEquals("Return type in method " + searchedSignWhat.getName() + " doesn't match expected type ",
52 searchedSignWhat.getType(), typeNameFound);
57 public static void containsAttributes(final GeneratedTransferObject genTO, boolean equal, boolean hash,
58 boolean toString, final NameTypePattern... searchedSignsWhat) {
59 List<GeneratedProperty> searchedPropertiesIn = genTO.getProperties();
60 containsAttributes(searchedPropertiesIn, "", searchedSignsWhat);
62 searchedPropertiesIn = genTO.getEqualsIdentifiers();
63 containsAttributes(searchedPropertiesIn, "equal", searchedSignsWhat);
66 searchedPropertiesIn = genTO.getHashCodeIdentifiers();
67 containsAttributes(searchedPropertiesIn, "hash", searchedSignsWhat);
70 searchedPropertiesIn = genTO.getToStringIdentifiers();
71 containsAttributes(searchedPropertiesIn, "toString", searchedSignsWhat);
76 public static void containsAttributes(final List<GeneratedProperty> searchedPropertiesIn, final String listType,
77 final NameTypePattern... searchedPropertiesWhat) {
79 for (NameTypePattern searchedPropertyWhat : searchedPropertiesWhat) {
80 boolean nameMatchFound = false;
81 String typeNameFound = "";
82 for (GeneratedProperty searchedPropertyIn : searchedPropertiesIn) {
83 if (searchedPropertyWhat.getName().equals(searchedPropertyIn.getName())) {
84 nameMatchFound = true;
85 typeNameFound = resolveFullNameOfReturnType(searchedPropertyIn.getReturnType());
86 if (searchedPropertyWhat.getType().equals(typeNameFound)) {
91 assertTrue("Property " + searchedPropertyWhat.getName() + " wasn't found in " + listType
92 + " property list.", nameMatchFound);
93 assertEquals("The type of property " + searchedPropertyWhat.getName() + " in " + listType
94 + " property list doesn't match expected type.", searchedPropertyWhat.getType(), typeNameFound);
99 public static String resolveFullNameOfReturnType(final Type type) {
100 final StringBuilder nameBuilder = new StringBuilder();
101 if (type instanceof ParameterizedType) {
102 nameBuilder.append(type.getName() + "<");
103 ParameterizedType parametrizedTypes = (ParameterizedType) type;
104 for (Type parametrizedType : parametrizedTypes.getActualTypeArguments()) {
105 nameBuilder.append(parametrizedType.getName() + ",");
107 if (nameBuilder.charAt(nameBuilder.length() - 1) == ',') {
108 nameBuilder.deleteCharAt(nameBuilder.length() - 1);
110 nameBuilder.append(">");
112 nameBuilder.append(type.getName());
114 return nameBuilder.toString();
117 public static void containsInterface(String interfaceNameSearched, GeneratedType genType) {
118 List<Type> caseCImplements = genType.getImplements();
119 boolean interfaceFound = false;
120 for (Type caseCImplement : caseCImplements) {
121 String interfaceName = resolveFullNameOfReturnType(caseCImplement);
122 if (interfaceName.equals(interfaceNameSearched)) {
123 interfaceFound = true;
127 assertTrue("Generated type " + genType.getName() + " doesn't implement interface " + interfaceNameSearched,