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;
14 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
21 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
22 import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28 public class SupportTestUtil {
30 public static SchemaContext resolveSchemaContextFromFiles(final URI... yangFiles) throws IOException {
31 final YangContextParser parser = new YangParserImpl();
33 final List<File> inputFiles = new ArrayList<File>();
34 for (int i = 0; i < yangFiles.length; ++i) {
35 inputFiles.add(new File(yangFiles[i]));
38 return parser.parseFiles(inputFiles);
41 public static void containsMethods(final GeneratedType genType, final NameTypePattern... searchedSignsWhat) {
42 final List<MethodSignature> searchedSignsIn = genType.getMethodDefinitions();
43 containsMethods(searchedSignsIn, searchedSignsWhat);
46 public static void containsMethods(final List<MethodSignature> searchedSignsIn,
47 final NameTypePattern... searchedSignsWhat) {
48 if (searchedSignsIn == null) {
49 throw new IllegalArgumentException("List of method signatures in which should be searched can't be null");
51 if (searchedSignsWhat == null) {
52 throw new IllegalArgumentException("Array of method signatures which should be searched can't be null");
55 for (NameTypePattern searchedSignWhat : searchedSignsWhat) {
56 boolean nameMatchFound = false;
57 String typeNameFound = "";
58 for (MethodSignature searchedSignIn : searchedSignsIn) {
59 if (searchedSignWhat.getName().equals(searchedSignIn.getName())) {
60 nameMatchFound = true;
61 typeNameFound = resolveFullNameOfReturnType(searchedSignIn.getReturnType());
62 if (searchedSignWhat.getType().equals(typeNameFound)) {
67 assertTrue("Method " + searchedSignWhat.getName() + " wasn't found.", nameMatchFound);
68 assertEquals("Return type in method " + searchedSignWhat.getName() + " doesn't match expected type ",
69 searchedSignWhat.getType(), typeNameFound);
74 public static void containsAttributes(final GeneratedTransferObject genTO, boolean equal, boolean hash,
75 boolean toString, final NameTypePattern... searchedSignsWhat) {
76 List<GeneratedProperty> searchedPropertiesIn = genTO.getProperties();
77 containsAttributes(searchedPropertiesIn, "", searchedSignsWhat);
79 searchedPropertiesIn = genTO.getEqualsIdentifiers();
80 containsAttributes(searchedPropertiesIn, "equal", searchedSignsWhat);
83 searchedPropertiesIn = genTO.getHashCodeIdentifiers();
84 containsAttributes(searchedPropertiesIn, "hash", searchedSignsWhat);
87 searchedPropertiesIn = genTO.getToStringIdentifiers();
88 containsAttributes(searchedPropertiesIn, "toString", searchedSignsWhat);
93 public static void containsAttributes(final List<GeneratedProperty> searchedPropertiesIn, final String listType,
94 final NameTypePattern... searchedPropertiesWhat) {
96 for (NameTypePattern searchedPropertyWhat : searchedPropertiesWhat) {
97 boolean nameMatchFound = false;
98 String typeNameFound = "";
99 for (GeneratedProperty searchedPropertyIn : searchedPropertiesIn) {
100 if (searchedPropertyWhat.getName().equals(searchedPropertyIn.getName())) {
101 nameMatchFound = true;
102 typeNameFound = resolveFullNameOfReturnType(searchedPropertyIn.getReturnType());
103 if (searchedPropertyWhat.getType().equals(typeNameFound)) {
108 assertTrue("Property " + searchedPropertyWhat.getName() + " wasn't found in " + listType
109 + " property list.", nameMatchFound);
110 assertEquals("The type of property " + searchedPropertyWhat.getName() + " in " + listType
111 + " property list doesn't match expected type.", searchedPropertyWhat.getType(), typeNameFound);
116 public static String resolveFullNameOfReturnType(final Type type) {
117 final StringBuilder nameBuilder = new StringBuilder();
118 if (type instanceof ParameterizedType) {
119 nameBuilder.append(type.getName() + "<");
120 ParameterizedType parametrizedTypes = (ParameterizedType) type;
121 for (Type parametrizedType : parametrizedTypes.getActualTypeArguments()) {
122 nameBuilder.append(parametrizedType.getName() + ",");
124 if (nameBuilder.charAt(nameBuilder.length() - 1) == ',') {
125 nameBuilder.deleteCharAt(nameBuilder.length() - 1);
127 nameBuilder.append(">");
129 nameBuilder.append(type.getName());
131 return nameBuilder.toString();
134 public static void containsInterface(String interfaceNameSearched, GeneratedType genType) {
135 List<Type> caseCImplements = genType.getImplements();
136 boolean interfaceFound = false;
137 for (Type caseCImplement : caseCImplements) {
138 String interfaceName = resolveFullNameOfReturnType(caseCImplement);
139 if (interfaceName.equals(interfaceNameSearched)) {
140 interfaceFound = true;
144 assertTrue("Generated type " + genType.getName() + " doesn't implement interface " + interfaceNameSearched,