Remove @Deprecated ClassLoaderUtils as it's now in yangtools.util
[mdsal.git] / binding / maven-sal-api-gen-plugin / src / test / java / org / opendaylight / yangtools / yang / unified / doc / generator / maven / retest / DocGenTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.unified.doc.generator.maven.retest;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import java.io.File;
13 import java.io.FileNotFoundException;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Set;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.unified.doc.generator.maven.DocumentationGeneratorImpl;
26 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
27
28 public class DocGenTest {
29     public static final String FS = File.separator;
30     private static final String TEST_PATH = "target" + FS + "test" + FS + "site";
31     private static final File GENERATOR_OUTPUT_DIR = new File(TEST_PATH);
32
33     @Before
34     public void init() {
35         if (GENERATOR_OUTPUT_DIR.exists()) {
36             deleteTestDir(GENERATOR_OUTPUT_DIR);
37         }
38         assertTrue(GENERATOR_OUTPUT_DIR.mkdirs());
39     }
40
41     @After
42     public void cleanUp() {
43         if (GENERATOR_OUTPUT_DIR.exists()) {
44             deleteTestDir(GENERATOR_OUTPUT_DIR);
45         }
46     }
47
48     @Test
49     public void testListGeneration() throws Exception {
50         final List<File> sourceFiles = getSourceFiles("/doc-gen");
51         final SchemaContext context = RetestUtils.parseYangSources(sourceFiles);
52         final Set<Module> modules = context.getModules();
53         final BasicCodeGenerator generator = new DocumentationGeneratorImpl();
54         Collection<File> generatedFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modules);
55         assertEquals(4, generatedFiles.size());
56     }
57
58     private static List<File> getSourceFiles(String path) throws Exception {
59         final URI resPath = DocGenTest.class.getResource(path).toURI();
60         final File sourcesDir = new File(resPath);
61         if (sourcesDir.exists()) {
62             final List<File> sourceFiles = new ArrayList<>();
63             final File[] fileArray = sourcesDir.listFiles();
64             if (fileArray == null) {
65                 throw new IllegalArgumentException("Unable to locate files in " + sourcesDir);
66             }
67             sourceFiles.addAll(Arrays.asList(fileArray));
68             return sourceFiles;
69         } else {
70             throw new FileNotFoundException("Testing files were not found(" + sourcesDir.getName() + ")");
71         }
72     }
73
74     private static void deleteTestDir(File file) {
75         if (file.isDirectory()) {
76             File[] filesToDelete = file.listFiles();
77             if (filesToDelete != null) {
78                 for (File f : filesToDelete) {
79                     deleteTestDir(f);
80                 }
81             }
82         }
83         if (!file.delete()) {
84             throw new RuntimeException("Failed to clean up after test");
85         }
86     }
87
88 }