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.test;
10 import static org.junit.Assert.assertTrue;
12 import com.google.common.collect.ImmutableSet;
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.net.URISyntaxException;
19 import java.nio.charset.StandardCharsets;
20 import java.util.List;
21 import java.util.Optional;
22 import java.util.function.Supplier;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
26 import org.opendaylight.mdsal.binding.generator.impl.BindingGeneratorImpl;
27 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
28 import org.opendaylight.mdsal.binding.java.api.generator.YangModuleInfoTemplate;
29 import org.opendaylight.mdsal.binding.model.api.Type;
30 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 public abstract class BaseCompilationTest {
37 protected BindingGenerator bindingGenerator;
40 public static void createTestDirs() {
41 if (CompilationTestUtils.TEST_DIR.exists()) {
42 CompilationTestUtils.deleteTestDir(CompilationTestUtils.TEST_DIR);
44 assertTrue(CompilationTestUtils.GENERATOR_OUTPUT_DIR.mkdirs());
45 assertTrue(CompilationTestUtils.COMPILER_OUTPUT_DIR.mkdirs());
50 bindingGenerator = new BindingGeneratorImpl();
53 protected static final void generateTestSources(final List<Type> types, final File sourcesOutputDir)
55 types.sort((o1, o2) -> o2.getName().compareTo(o1.getName()));
57 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
58 final Table<?, String, Supplier<String>> generatedFiles = generator.generateFileContent(true);
59 for (Cell<?, String, Supplier<String>> cell : generatedFiles.cellSet()) {
60 final File target = new File(sourcesOutputDir, cell.getColumnKey());
61 Files.createParentDirs(target);
62 Files.asCharSink(target, StandardCharsets.UTF_8).write(cell.getValue().get());
66 protected final List<Type> generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
67 throws IOException, URISyntaxException {
68 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
69 final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
70 final List<Type> types = bindingGenerator.generateTypes(context);
71 generateTestSources(types, sourcesOutputDir);
73 // Also generate YangModuleInfo
74 for (Module module : context.getModules()) {
75 final YangModuleInfoTemplate template = new YangModuleInfoTemplate(module, context,
76 mod -> Optional.of("fake/" + mod.getName()));
78 final File file = new File(GeneratorJavaFile.packageToDirectory(sourcesOutputDir,
79 BindingMapping.getRootPackageName(module.getQNameModule())),
80 BindingMapping.MODULE_INFO_CLASS_NAME + ".java");
81 Files.asCharSink(file, StandardCharsets.UTF_8).write(template.generate());