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.yang.unified.doc.generator.maven;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.io.FileNotFoundException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.List;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
24 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
26 public class DocGenTest {
27 public static final String FS = File.separator;
28 private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
29 private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
30 private YangParserImpl parser;
34 if (GENERATOR_OUTPUT_DIR.exists()) {
35 deleteTestDir(GENERATOR_OUTPUT_DIR);
37 assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
38 parser = new YangParserImpl();
42 public void cleanUp() {
43 if (GENERATOR_OUTPUT_DIR.exists()) {
44 deleteTestDir(GENERATOR_OUTPUT_DIR);
49 public void testListGeneration() throws Exception {
50 final List<File> sourceFiles = getSourceFiles("/doc-gen");
51 final SchemaContext context = parser.parseFiles(sourceFiles);
52 final BasicCodeGenerator generator = new DocumentationGeneratorImpl();
53 Collection<File> generatedFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, context.getModules());
54 assertEquals(4, generatedFiles.size());
57 private static List<File> getSourceFiles(final String path) throws Exception {
58 final URI resPath = DocGenTest.class.getResource(path).toURI();
59 final File sourcesDir = new File(resPath);
60 if (sourcesDir.exists()) {
61 final List<File> sourceFiles = new ArrayList<>();
62 final File[] fileArray = sourcesDir.listFiles();
63 if (fileArray == null) {
64 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
66 sourceFiles.addAll(Arrays.asList(fileArray));
69 throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
73 private static void deleteTestDir(final File file) {
74 if (file.isDirectory()) {
75 File[] filesToDelete = file.listFiles();
76 if (filesToDelete != null) {
77 for (File f : filesToDelete) {
83 throw new RuntimeException("Failed to clean up after test");