2 * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.stmt.test;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 public class Bug5712Test {
35 public void testTypedefWithNewStatementParser() throws ReactorException, SourceException, FileNotFoundException,
37 SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug5712");
38 assertNotNull(schemaContext);
40 Module badModule = schemaContext.findModuleByName("bad", null);
41 assertNotNull(badModule);
43 checkThing2TypeDef(badModule);
46 private void checkThing2TypeDef(Module badModule) {
47 TypeDefinition<?> thing2 = null;
48 for (TypeDefinition<?> typeDef : badModule.getTypeDefinitions()) {
49 if (typeDef.getQName().getLocalName().equals("thing2")) {
55 assertNotNull(thing2);
56 TypeDefinition<?> baseType = thing2.getBaseType();
57 assertEquals(QName.create("urn:opendaylight:bad", "2016-04-11", "thing"), baseType.getQName());
61 public void testTypedefWithOldParser() throws URISyntaxException, IOException {
62 Set<Module> modules = loadModules(getClass().getResource("/bugs/bug5712").toURI());
63 assertNotNull(modules);
64 assertEquals(1, modules.size());
66 checkThing2TypeDef(modules.iterator().next());
69 public static Set<Module> loadModules(final URI resourceDirectory) throws IOException {
70 return loadSchemaContext(resourceDirectory).getModules();
73 public static SchemaContext loadSchemaContext(final URI resourceDirectory) throws IOException {
74 final YangContextParser parser = new YangParserImpl();
75 final File testDir = new File(resourceDirectory);
76 final String[] fileList = testDir.list();
77 final List<File> testFiles = new ArrayList<>();
78 if (fileList == null) {
79 throw new FileNotFoundException(resourceDirectory.toString());
81 for (String fileName : fileList) {
82 testFiles.add(new File(testDir, fileName));
84 return parser.parseFiles(testFiles);