Added generation of Transfer Objects from Type Definitions.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / test / java / org / opendaylight / controller / sal / java / api / generator / test / GeneratorJavaFileTest.java
1 /*
2  * Copyright (c) 2013 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.controller.sal.java.api.generator.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.File;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20
21 import javax.tools.JavaCompiler;
22 import javax.tools.JavaFileObject;
23 import javax.tools.StandardJavaFileManager;
24 import javax.tools.ToolProvider;
25
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
30 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
31 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
32 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
33 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
34 import org.opendaylight.controller.sal.binding.model.api.Type;
35 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
36 import org.opendaylight.controller.sal.java.api.generator.GeneratorJavaFile;
37 import org.opendaylight.controller.sal.java.api.generator.InterfaceGenerator;
38 import org.opendaylight.controller.yang.model.api.Module;
39 import org.opendaylight.controller.yang.model.api.SchemaContext;
40 import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;
41
42 public class GeneratorJavaFileTest {
43     private static final String FS = File.separator;
44     private static final String PATH = "test-dir";
45     private final File testDir = new File(PATH);
46
47     private static final String GENERATOR_OUTPUT_PATH = "src/test/resources/src";
48     private static final File GENERATOR_OUTPUT = new File(GENERATOR_OUTPUT_PATH);
49     private static final String COMPILER_OUTPUT_PATH = "src/test/resources/bin";
50     private static final File COMPILER_OUTPUT = new File(COMPILER_OUTPUT_PATH);
51
52     @Before
53     public void init() {
54         assertTrue(testDir.mkdir());
55         assertTrue(COMPILER_OUTPUT.mkdirs());
56         assertTrue(GENERATOR_OUTPUT.mkdirs());
57     }
58
59     @After
60     public void cleanUp() {
61         deleteTestDir(testDir);
62         deleteTestDir(COMPILER_OUTPUT);
63         deleteTestDir(GENERATOR_OUTPUT);
64     }
65
66     @Test
67     public void test() {
68         final Set<GeneratedType> types = new HashSet<GeneratedType>();
69         GeneratedType t1 = createGeneratedType(
70                 "org.opendaylight.controller.gen", "Type1");
71         GeneratedType t2 = createGeneratedType(
72                 "org.opendaylight.controller.gen", "Type2");
73         GeneratedType t3 = createGeneratedType(
74                 "org.opendaylight.controller.gen", "Type3");
75         types.add(t1);
76         types.add(t2);
77         types.add(t3);
78         GeneratorJavaFile generator = new GeneratorJavaFile(
79                 new InterfaceGenerator(), types);
80         generator.generateToFile(PATH);
81
82         // path: test-dir/com/cisco/yang
83         String[] files = new File(PATH + FS + "org" + FS + "opendaylight" + FS + "controller" + FS + "gen").list();
84         List<String> filesList = Arrays.asList(files);
85
86         assertEquals(3, files.length);
87         assertTrue(filesList.contains("Type1.java"));
88         assertTrue(filesList.contains("Type2.java"));
89         assertTrue(filesList.contains("Type3.java"));
90     }
91     
92     @Test
93     public void compilationTest() throws Exception {
94         final YangModelParserImpl parser = new YangModelParserImpl();
95         final BindingGenerator bindingGenerator = new BindingGeneratorImpl();
96
97         final File sourcesDir = new File("src/test/resources/yang");
98         final List<File> sourceFiles = new ArrayList<File>();
99         final File[] fileArray = sourcesDir.listFiles();
100         
101         for (int i = 0; i < fileArray.length; ++i) {
102             sourceFiles.add(fileArray[i]);
103         }
104         
105 //        String[] sourcesDirPaths = new String[sourceFiles.length];
106 //        for (int i = 0; i < sourceFiles.length; i++) {
107 //            sourcesDirPaths[i] = sourceFiles[i].getAbsolutePath();
108 //        }
109         final Set<Module> modulesToBuild = parser
110                 .parseYangModels(sourceFiles);
111
112         final SchemaContext context = parser
113                 .resolveSchemaContext(modulesToBuild);
114         final List<Type> types = bindingGenerator.generateTypes(context);
115         final Set<GeneratedType> typesToGenerate = new HashSet<GeneratedType>();
116         final Set<GeneratedTransferObject> tosToGenerate = new HashSet<GeneratedTransferObject>();
117         for (Type type : types) {
118             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
119                 typesToGenerate.add((GeneratedType) type);
120             }
121
122             if (type instanceof GeneratedTransferObject) {
123                 tosToGenerate.add((GeneratedTransferObject) type);
124             }
125         }
126
127         final GeneratorJavaFile generator = new GeneratorJavaFile(
128                 typesToGenerate, tosToGenerate);
129         generator.generateToFile(GENERATOR_OUTPUT_PATH);
130
131         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
132         StandardJavaFileManager fileManager = compiler.getStandardFileManager(
133                 null, null, null);
134
135         List<File> filesList = getJavaFiles(new File(GENERATOR_OUTPUT_PATH));
136
137         Iterable<? extends JavaFileObject> compilationUnits = fileManager
138                 .getJavaFileObjectsFromFiles(filesList);
139         Iterable<String> options = Arrays.asList(new String[] { "-d",
140                 COMPILER_OUTPUT_PATH });
141         boolean compiled = compiler.getTask(null, null, null, options, null,
142                 compilationUnits).call();
143         assertTrue(compiled);
144     }
145
146     private GeneratedType createGeneratedType(String pkgName, String name) {
147         GeneratedTypeBuilder builder = new GeneratedTypeBuilderImpl(pkgName,
148                 name);
149         return builder.toInstance();
150     }
151
152     private void deleteTestDir(File file) {
153         if (file.isDirectory()) {
154             for (File f : file.listFiles()) {
155                 deleteTestDir(f);
156             }
157         }
158         if (!file.delete()) {
159             throw new RuntimeException("Failed to clean up after test");
160         }
161     }
162
163     /**
164      * Search recursively given directory for *.java files.
165      *
166      * @param directory
167      *            directory to search
168      * @return List of java files found
169      */
170     private List<File> getJavaFiles(File directory) {
171         List<File> result = new ArrayList<File>();
172         for (File file : directory.listFiles()) {
173             if (file.isDirectory()) {
174                 result.addAll(getJavaFiles(file));
175             } else {
176                 String absPath = file.getAbsolutePath();
177                 if (absPath.endsWith(".java")) {
178                     result.add(file);
179                 }
180             }
181         }
182         return result;
183     }
184
185 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.