Skip to content

Commit 2a42834

Browse files
committed
Issue #382: Warning when PEAR contains a JCAS class that is used as a feature range outside the PEAR
- Make sure tests close files so temporary test folders can be deleted
1 parent 57aa798 commit 2a42834

File tree

1 file changed

+43
-22
lines changed
  • uimaj-it-pear-with-typesystem/src/test/java/org/apache/uima/it/pear_with_typesystem

1 file changed

+43
-22
lines changed

uimaj-it-pear-with-typesystem/src/test/java/org/apache/uima/it/pear_with_typesystem/PearIT.java

+43-22
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.apache.uima.cas.CAS;
5555
import org.apache.uima.cas.impl.CASImpl;
5656
import org.apache.uima.internal.util.Class_TCCL;
57+
import org.apache.uima.internal.util.Misc;
5758
import org.apache.uima.internal.util.UIMAClassLoader;
5859
import org.apache.uima.it.pear_with_typesystem.spi.JCasClassProviderForTesting;
5960
import org.apache.uima.it.pear_with_typesystem.type.ComplexAnnotation;
@@ -107,8 +108,8 @@ void setup() throws Exception
107108
}
108109

109110
/**
110-
* PEAR use {@link ComplexAnnotation} directly - we assume it is in the PEAR's local
111-
* classpath and available at compile time.
111+
* PEAR use {@link ComplexAnnotation} directly - we assume it is in the PEAR's local classpath
112+
* and available at compile time.
112113
*/
113114
@Test
114115
void testScenario1(@TempDir File aTemp) throws Exception
@@ -131,6 +132,8 @@ void testScenario1(@TempDir File aTemp) throws Exception
131132
.hasSize(1) //
132133
.allSatisfy(complexAnnotation -> assertThat(complexAnnotation)
133134
.isInstanceOf(ComplexAnnotation.class));
135+
136+
pearAnnotator.getUimaContextAdmin().getResourceManager().destroy();
134137
}
135138

136139
/**
@@ -161,14 +164,16 @@ void testScenario2(@TempDir File aTemp) throws Exception
161164
.hasSize(1) //
162165
.allSatisfy(complexAnnotation -> assertThat(complexAnnotation)
163166
.isInstanceOf(ComplexAnnotation.class));
167+
168+
pearAnnotator.getUimaContextAdmin().getResourceManager().destroy();
164169
}
165170

166171
/**
167172
* PEAR can use {@link ComplexAnnotation} directly - we assume it is in the PEAR's local
168173
* classpath and available at compile time.
169174
* <p>
170-
* However, PEAR does not know about {@link ComplexAnnotationSubtype}. Yet, it will find
171-
* that the CAS contains an annotation of that type.
175+
* However, PEAR does not know about {@link ComplexAnnotationSubtype}. Yet, it will find that
176+
* the CAS contains an annotation of that type.
172177
*/
173178
@Test
174179
void testScenario3(@TempDir File aTemp) throws Exception
@@ -190,6 +195,8 @@ void testScenario3(@TempDir File aTemp) throws Exception
190195
assertThatExceptionOfType(AnalysisEngineProcessException.class) //
191196
.isThrownBy(() -> pearAnnotator.process(cas)) //
192197
.withRootCauseInstanceOf(ClassCastException.class);
198+
199+
pearAnnotator.getUimaContextAdmin().getResourceManager().destroy();
193200
}
194201

195202
private AnalysisEngine installPear(File aTemp,
@@ -245,15 +252,13 @@ public IsolatingResourceManager_impl(Map<String, Object> aResourceMap,
245252
aParameterizedResourceInstanceMap);
246253
}
247254

248-
@SuppressWarnings("resource")
249255
@Override
250256
public synchronized void setExtensionClassPath(String aClasspath, boolean aResolveResource)
251257
throws MalformedURLException
252258
{
253259
var parentCL = Class_TCCL.get_parent_cl();
254-
var uimaCL = new UIMAClassLoader(aClasspath, Class_TCCL.get_parent_cl());
255-
uimaCL = clConfigurer.apply(parentCL,
256-
new IsolatingUIMAClassloader("PEAR Classloader", uimaCL));
260+
var uimaCL = clConfigurer.apply(parentCL, new IsolatingUIMAClassloader(
261+
"PEAR Classloader", parentCL, Misc.classpath2urls(aClasspath)));
257262
setExtensionClassLoader(uimaCL, aResolveResource);
258263
}
259264

@@ -333,6 +338,16 @@ public IsolatingUIMAClassloader(String aName, ClassLoader aParent, URL... aClass
333338
id = aName;
334339
}
335340

341+
@Override
342+
public void close() throws IOException
343+
{
344+
if (getParent() instanceof UIMAClassLoader uimaClassLoader) {
345+
uimaClassLoader.close();
346+
}
347+
348+
super.close();
349+
}
350+
336351
@SafeVarargs
337352
public final <T> IsolatingUIMAClassloader delegatingSPI(ClassLoader aDelegate,
338353
Class<T> aServiceInterface, Class<? extends T>... aServiceProviders)
@@ -513,26 +528,32 @@ protected Class<?> loadClass(String aName, boolean aResolve) throws ClassNotFoun
513528
LOG.debug("[{}] redefining class: {}", id, aName);
514529

515530
String internalName = aName.replace(".", "/") + ".class";
516-
InputStream is = getParent().getResourceAsStream(internalName);
517-
if (is == null) {
531+
URL url = getParent().getResource(internalName);
532+
if (url == null) {
518533
throw new ClassNotFoundException(aName);
519534
}
520535

521536
try {
522-
var buffer = new ByteArrayOutputStream();
523-
is.transferTo(buffer);
524-
byte[] bytes = buffer.toByteArray();
525-
Class<?> cls = defineClass(aName, bytes, 0, bytes.length);
526-
if (cls.getPackage() == null) {
527-
int packageSeparator = aName.lastIndexOf('.');
528-
if (packageSeparator != -1) {
529-
String packageName = aName.substring(0, packageSeparator);
530-
definePackage(packageName, null, null, null, null, null, null,
531-
null);
537+
// Disable JAR cache so JUnit can delete the temporary folder after the test
538+
var urlConnection = url.openConnection();
539+
urlConnection.setDefaultUseCaches(false);
540+
541+
try (InputStream is = urlConnection.getInputStream()) {
542+
var buffer = new ByteArrayOutputStream();
543+
is.transferTo(buffer);
544+
byte[] bytes = buffer.toByteArray();
545+
Class<?> cls = defineClass(aName, bytes, 0, bytes.length);
546+
if (cls.getPackage() == null) {
547+
int packageSeparator = aName.lastIndexOf('.');
548+
if (packageSeparator != -1) {
549+
String packageName = aName.substring(0, packageSeparator);
550+
definePackage(packageName, null, null, null, null, null, null,
551+
null);
552+
}
532553
}
554+
loadedClasses.put(aName, cls);
555+
return cls;
533556
}
534-
loadedClasses.put(aName, cls);
535-
return cls;
536557
}
537558
catch (IOException ex) {
538559
throw new ClassNotFoundException(

0 commit comments

Comments
 (0)