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
8 package org.opendaylight.mdsal.binding.java.api.generator;
10 import static java.nio.file.Files.newOutputStream;
11 import static org.junit.Assert.assertTrue;
13 import com.google.common.collect.Table;
14 import com.google.common.collect.Table.Cell;
15 import com.google.common.io.Files;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.net.URISyntaxException;
20 import java.nio.charset.StandardCharsets;
21 import java.util.List;
22 import java.util.Optional;
23 import org.junit.BeforeClass;
24 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator;
25 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
26 import org.opendaylight.yangtools.plugin.generator.api.GeneratedFile;
27 import org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath;
28 import org.opendaylight.yangtools.yang.binding.contract.Naming;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 public abstract class BaseCompilationTest {
36 public static void createTestDirs() {
37 if (CompilationTestUtils.TEST_DIR.exists()) {
38 CompilationTestUtils.deleteTestDir(CompilationTestUtils.TEST_DIR);
40 assertTrue(CompilationTestUtils.GENERATOR_OUTPUT_DIR.mkdirs());
41 assertTrue(CompilationTestUtils.COMPILER_OUTPUT_DIR.mkdirs());
44 protected static final void generateTestSources(final List<GeneratedType> types, final File sourcesOutputDir)
46 types.sort((o1, o2) -> o2.getName().compareTo(o1.getName()));
48 final Table<?, GeneratedFilePath, GeneratedFile> generatedFiles = JavaFileGenerator.generateFiles(types, true);
49 for (Cell<?, GeneratedFilePath, GeneratedFile> cell : generatedFiles.cellSet()) {
50 final File target = new File(sourcesOutputDir, cell.getColumnKey().getPath());
51 Files.createParentDirs(target);
53 try (OutputStream os = newOutputStream(target.toPath())) {
54 cell.getValue().writeBody(os);
59 protected static final List<GeneratedType> generateTestSources(final String resourceDirPath,
60 final File sourcesOutputDir) throws IOException, URISyntaxException {
61 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
62 final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
63 final List<GeneratedType> types = new DefaultBindingGenerator().generateTypes(context);
64 generateTestSources(types, sourcesOutputDir);
66 // Also generate YangModuleInfo
67 for (Module module : context.getModules()) {
68 final YangModuleInfoTemplate template = new YangModuleInfoTemplate(module, context,
69 mod -> Optional.of("fake/" + mod.getName()));
71 final File file = new File(new File(sourcesOutputDir,
72 Naming.getRootPackageName(module.getQNameModule()).replace('.', File.separatorChar)),
73 Naming.MODULE_INFO_CLASS_NAME + ".java");
74 Files.createParentDirs(file);
75 Files.asCharSink(file, StandardCharsets.UTF_8).write(template.generate());