GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestStructAccessor.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.gluegen;
30
31import java.io.File;
32import java.io.IOException;
33import java.io.OutputStreamWriter;
34import java.lang.reflect.InvocationTargetException;
35import java.util.Arrays;
36import java.util.List;
37import java.util.Locale;
38
39import javax.tools.Diagnostic;
40import javax.tools.DiagnosticCollector;
41import javax.tools.JavaCompiler;
42import javax.tools.JavaFileObject;
43import javax.tools.StandardJavaFileManager;
44import javax.tools.ToolProvider;
45
46import org.junit.AfterClass;
47import org.junit.Assert;
48import org.junit.Test;
49
50import static java.lang.System.*;
51import static com.jogamp.gluegen.BuildUtil.*;
52
53/**
54 *
55 * @author Michael Bien
56 */
57public class TestStructAccessor {
58
59 @Test
60 public void generateStruct() {
61 generate("struct");
62 }
63
64 @Test
65 public void compileStructJava() {
66 compileJava();
67 }
68
69 @Test
70 public void compileStructNatives() {
71 // this will only copy gluegen-rt to the right place
72 compileNatives();
73 }
74
75 @Test
76 public void validateGeneratedStructs() throws IOException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException {
77
78 // compile testcase
79 final String source = gluegenRoot + "/test/junit/com/jogamp/gluegen/StructValidator.java";
80 compile(new File(source), testOutput + "/build/classes");
81
82 // invoke test
83 final Class<?> test = Class.forName("com.jogamp.gluegen.StructValidator");
84 test.getDeclaredMethod("validate").invoke(null);
85 }
86
87 private void compile(final File file, final String dest) throws IOException {
88 compile(new File[] {file}, dest);
89 }
90
91 // yeah, java 6 has even a compiler api...
92 private void compile(final File[] files, final String destination) throws IOException {
93
94 out.println("compiling files:\n " + Arrays.asList(files));
95
96 final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
97 final DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>();
98 final StandardJavaFileManager fileManager = compiler.getStandardFileManager(collector, null, null);
99
100 final Iterable<? extends JavaFileObject> fileObj = fileManager.getJavaFileObjects(files);
101
102 final boolean success = compiler.getTask( new OutputStreamWriter(out),
103 fileManager,
104 collector,
105 Arrays.asList("-d", destination/*, "-verbose"*/),
106 null,
107 fileObj ).call();
108
109 fileManager.close();
110
111 final List<Diagnostic<? extends JavaFileObject>> list = collector.getDiagnostics();
112 if(!list.isEmpty() || !success) {
113 for (final Diagnostic<? extends JavaFileObject> d : list) {
114 out.println("Error on line "+ d.getLineNumber());
115 out.println("Compiler Message:\n"+d.getMessage(Locale.ENGLISH));
116 }
117 Assert.fail("compilation failed");
118 }
119
120 out.println("done");
121
122 }
123
124 @AfterClass
125 public static void tearDown() {
126// cleanGeneratedFiles();
127 }
128
129}