99 ctx.usePackageForFolder(p.getParent(), pkg);
100 ctx.addSource(p);
101 ctx.parse();
102 return ctx.collectClasses(pkg);
103 }
104
105 private InMemoryFileManager<StandardJavaFileManager> compileJavaCode() {
106 JavaCompiler cl = ToolProvider.getSystemJavaCompiler();
107 StandardJavaFileManager sfm = cl.getStandardFileManager(null, null, null);
108 InMemoryFileManager<StandardJavaFileManager> fm = new InMemoryFileManager<>(sfm);
109 JavaCompiler.CompilationTask task = cl.getTask(null, fm, null, null,
110 null, sfm.getJavaFileObjects(javaSrcFiles));
111 task.call();
112 return fm;
113 }
114
115 @Test
116 public void testJarManifest() throws IOException {
117 // Get the jar
118 ByteArrayOutputStream bos = new ByteArrayOutputStream();
119 ctx.collectJarFile(new JarOutputStream(bos), pkg);
120
121 System.out.println("Jar built, verifying...");
122 JarInputStream jis = new JarInputStream(new ByteArrayInputStream(bos.toByteArray()));
123
124 // List all classes in the jar
125 Set<String> files = new HashSet<>();
126 for (JarEntry e = jis.getNextJarEntry(); e != null; e = jis.getNextJarEntry()) {
127 if (e.isDirectory()) {
128 continue;
129 }
130 String name = e.getName();
131 if (! name.endsWith(".class")) {
132 // Should not have file not class files
133 System.err.println("Warning: unexpected file " + name);
134 }
135 name = name.substring(0, name.length() - 6);
136 files.add(name.replace(File.separatorChar, '.'));
137 }
138
139 assertEquals(files, mfm.listClasses());
140 }
141
142 private void verifyNativeLocation(NativeLocation actual, NativeLocation expected) {
143 // Only check the filename, not full path
144 assertNotNull(actual);
145 assertNotNull(expected);
146 assertEquals(Paths.get(actual.file()).getFileName(),
147 Paths.get(expected.file()).getFileName());
148 assertEquals(actual.line(), expected.line());
149 assertEquals(actual.column(), expected.column());
150 }
151
152 private void verifyMethodAnnotation(Method actual, Method expected) {
153 Annotation[] aa = actual.getAnnotations();
154 Annotation[] ea = expected.getAnnotations();
155
156 for (Annotation a: ea) {
157 if (a instanceof NativeLocation) {
|
99 ctx.usePackageForFolder(p.getParent(), pkg);
100 ctx.addSource(p);
101 ctx.parse();
102 return ctx.collectClasses(pkg);
103 }
104
105 private InMemoryFileManager<StandardJavaFileManager> compileJavaCode() {
106 JavaCompiler cl = ToolProvider.getSystemJavaCompiler();
107 StandardJavaFileManager sfm = cl.getStandardFileManager(null, null, null);
108 InMemoryFileManager<StandardJavaFileManager> fm = new InMemoryFileManager<>(sfm);
109 JavaCompiler.CompilationTask task = cl.getTask(null, fm, null, null,
110 null, sfm.getJavaFileObjects(javaSrcFiles));
111 task.call();
112 return fm;
113 }
114
115 @Test
116 public void testJarManifest() throws IOException {
117 // Get the jar
118 ByteArrayOutputStream bos = new ByteArrayOutputStream();
119 ctx.collectJarFile(new JarOutputStream(bos), new String[0], pkg);
120
121 System.out.println("Jar built, verifying...");
122 JarInputStream jis = new JarInputStream(new ByteArrayInputStream(bos.toByteArray()));
123
124 // List all classes in the jar
125 Set<String> files = new HashSet<>();
126 for (JarEntry e = jis.getNextJarEntry(); e != null; e = jis.getNextJarEntry()) {
127 if (e.isDirectory()) {
128 continue;
129 }
130 String name = e.getName();
131 if (! name.endsWith(".properties")) {
132 if (! name.endsWith(".class")) {
133 // Should not have file not class files
134 System.err.println("Warning: unexpected file " + name);
135 }
136 name = name.substring(0, name.length() - 6);
137 files.add(name.replace(File.separatorChar, '.'));
138 }
139 }
140
141 assertEquals(files, mfm.listClasses());
142 }
143
144 private void verifyNativeLocation(NativeLocation actual, NativeLocation expected) {
145 // Only check the filename, not full path
146 assertNotNull(actual);
147 assertNotNull(expected);
148 assertEquals(Paths.get(actual.file()).getFileName(),
149 Paths.get(expected.file()).getFileName());
150 assertEquals(actual.line(), expected.line());
151 assertEquals(actual.column(), expected.column());
152 }
153
154 private void verifyMethodAnnotation(Method actual, Method expected) {
155 Annotation[] aa = actual.getAnnotations();
156 Annotation[] ea = expected.getAnnotations();
157
158 for (Annotation a: ea) {
159 if (a instanceof NativeLocation) {
|