use of org.talend.sdk.component.junit.base.junit5.TemporaryFolder in project component-runtime by Talend.
the class TemporaryFolderExtension method beforeAll.
@Override
public void beforeAll(final ExtensionContext context) throws Exception {
final TemporaryFolder temporaryFolder = new TemporaryFolder();
context.getStore(NAMESPACE).put(TemporaryFolder.class.getName(), temporaryFolder);
temporaryFolder.create();
}
use of org.talend.sdk.component.junit.base.junit5.TemporaryFolder in project component-runtime by Talend.
the class InterceptorTest method run.
@Test
void run(final TemporaryFolder temporaryFolder) throws Exception {
final File pluginFolder = new File(temporaryFolder.getRoot(), "test-plugins_" + UUID.randomUUID().toString());
pluginFolder.mkdirs();
final File plugin = pluginGenerator.createChainPlugin(pluginFolder, "plugin.jar");
try (final ComponentManager manager = new ComponentManager(new File("target/test-dependencies"), "META-INF/test/dependencies", "org.talend.test:type=plugin,value=%s")) {
manager.addPlugin(plugin.getAbsolutePath());
final List<Object> collect = manager.find(c -> c.get(ComponentManager.AllServices.class).getServices().values().stream()).filter(c -> c.getClass().getName().endsWith("SuperService$$TalendServiceProxy")).collect(toList());
assertEquals(1, collect.size());
final Object instance = collect.iterator().next();
final Method method = instance.getClass().getMethod("canBeLong", int.class);
assertEquals("exec_1/1", method.invoke(instance, 1));
assertEquals("exec_1/1", method.invoke(instance, 1));
sleep(450);
assertEquals("exec_1/2", method.invoke(instance, 1));
assertEquals("exec_3/3", method.invoke(instance, 3));
assertEquals("exec_4/4", method.invoke(instance, 4));
final LightContainer container = manager.find(c -> Stream.of(c.get(LightContainer.class))).findFirst().get();
DynamicContainerFinder.LOADERS.put("plugin", container.classloader());
DynamicContainerFinder.SERVICES.put(instance.getClass().getSuperclass(), instance);
final Object roundTrip = roundTrip(instance);
assertEquals(roundTrip, instance);
} finally {
// clean temp files
DynamicContainerFinder.LOADERS.clear();
DynamicContainerFinder.SERVICES.clear();
}
}
use of org.talend.sdk.component.junit.base.junit5.TemporaryFolder in project component-runtime by Talend.
the class CarBundlerTest method bundle.
@Test
void bundle(final TemporaryFolder temporaryFolder) throws NoSuchMethodException, IOException, InterruptedException {
final File m2 = temporaryFolder.newFolder();
final File dep = new File(m2, "foo/bar/dummy/1.2/dummy-1.2.jar");
dep.getParentFile().mkdirs();
try (final JarOutputStream jos = new JarOutputStream(new FileOutputStream(dep))) {
jos.putNextEntry(new JarEntry("test.txt"));
jos.write("test".getBytes(StandardCharsets.UTF_8));
jos.closeEntry();
}
final CarBundler.Configuration configuration = new CarBundler.Configuration();
configuration.setVersion("1.2.3");
configuration.setArtifacts(singletonMap("foo.bar:dummy:1.2", dep));
configuration.setOutput(new File(temporaryFolder.getRoot(), "output.jar"));
configuration.setMainGav("foo.bar:dummy:1.2");
new CarBundler(configuration, new ReflectiveLog(log)).run();
assertTrue(configuration.getOutput().exists());
try (final JarFile jar = new JarFile(configuration.getOutput())) {
final List<JarEntry> entries = list(jar.entries()).stream().sorted(comparing(ZipEntry::getName)).collect(toList());
final List<String> paths = entries.stream().map(ZipEntry::getName).collect(toList());
assertEquals(asList("MAVEN-INF/", "MAVEN-INF/repository/", "MAVEN-INF/repository/foo/", "MAVEN-INF/repository/foo/bar/", "MAVEN-INF/repository/foo/bar/dummy/", "MAVEN-INF/repository/foo/bar/dummy/1.2/", "MAVEN-INF/repository/foo/bar/dummy/1.2/dummy-1.2.jar", "META-INF/", "META-INF/MANIFEST.MF", "TALEND-INF/", "TALEND-INF/metadata.properties", "org/", "org/talend/", "org/talend/sdk/", "org/talend/sdk/component/", "org/talend/sdk/component/tools/", "org/talend/sdk/component/tools/exec/", "org/talend/sdk/component/tools/exec/CarMain.class"), paths);
entries.stream().filter(e -> e.getName().endsWith("/")).forEach(e -> assertTrue(e.isDirectory()));
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(jar.getInputStream(jar.getEntry("TALEND-INF/metadata.properties"))))) {
final String meta = reader.lines().collect(joining("\n"));
assertTrue(meta.contains("version=1.2.3"));
assertTrue(meta.contains("date="));
assertTrue(meta.contains("component_coordinates=foo.bar\\:dummy\\:1.2"));
}
try (final JarInputStream depJar = new JarInputStream(jar.getInputStream(jar.getEntry("MAVEN-INF/repository/foo/bar/dummy/1.2/dummy-1.2.jar")))) {
final JarEntry nextJarEntry = depJar.getNextJarEntry();
assertNotNull(nextJarEntry);
assertEquals("test.txt", nextJarEntry.getName());
assertEquals("test", new BufferedReader(new InputStreamReader(depJar)).lines().collect(joining("\n")));
assertNull(depJar.getNextJarEntry());
}
}
// try to execute the main now in a fake studio
final File fakeStudio = temporaryFolder.newFolder();
final File fakeConfig = new File(fakeStudio, "configuration/config.ini");
fakeConfig.getParentFile().mkdirs();
try (final Writer writer = new FileWriter(fakeConfig)) {
// no-op, just create the file
}
final File fakeM2 = new File(fakeStudio, "configuration/.m2/repository/");
fakeM2.mkdirs();
assertEquals(0, new ProcessBuilder(new File(System.getProperty("java.home"), "/bin/java" + (OS.WINDOWS.isCurrentOs() ? ".exe" : "")).getAbsolutePath(), "-jar", configuration.getOutput().getAbsolutePath(), "studio-deploy", fakeStudio.getAbsolutePath()).inheritIO().start().waitFor());
// asserts the jar was installed and the component registered
assertTrue(new File(fakeM2, "foo/bar/dummy/1.2/dummy-1.2.jar").exists());
assertEquals("component.java.coordinates = foo.bar:dummy:1.2", Files.readAllLines(fakeConfig.toPath()).stream().collect(joining("\n")).trim());
}
use of org.talend.sdk.component.junit.base.junit5.TemporaryFolder in project component-runtime by Talend.
the class ComponentResourceTest method getDependency.
@Test
void getDependency(final TestInfo info, final TemporaryFolder folder) {
final Function<String, File> download = id -> {
final InputStream stream = base.path("component/dependency/{id}").resolveTemplate("id", id).request(APPLICATION_OCTET_STREAM_TYPE).get(InputStream.class);
final File file = new File(folder.getRoot(), info.getTestMethod().get().getName() + ".jar");
try (final OutputStream outputStream = new FileOutputStream(file)) {
IO.copy(stream, outputStream);
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return file;
};
final Consumer<File> jarValidator = file -> {
assertTrue(file.exists());
try (final JarFile jar = new JarFile(file)) {
assertTrue(jar.entries().hasMoreElements());
} catch (final IOException e) {
fail(e.getMessage());
}
};
final File zipLock = download.apply("org.apache.tomee:ziplock:jar:7.0.4");
jarValidator.accept(zipLock);
final File component = download.apply(client.getJdbcId());
jarValidator.accept(component);
}
use of org.talend.sdk.component.junit.base.junit5.TemporaryFolder in project component-runtime by Talend.
the class ComponentManagerTest method run.
@Test
void run(final TemporaryFolder temporaryFolder) throws Exception {
final File pluginFolder = new File(temporaryFolder.getRoot(), "test-plugins_" + UUID.randomUUID().toString());
pluginFolder.mkdirs();
// just some jars with classes we can scan
final File plugin1 = pluginGenerator.createPlugin(pluginFolder, "plugin1.jar", "org.apache.tomee:openejb-itests-beans:jar:7.0.4:runtime");
final File plugin2 = pluginGenerator.createPlugin(pluginFolder, "plugin2.jar", "org.apache.tomee:arquillian-tomee-codi-tests:jar:7.0.4:runtime");
// ensure jmx value is free and we don't get a test luck
final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
assertFalse(mBeanServer.isRegistered(new ObjectName("org.talend.test:type=plugin,value=plugin1")));
try (final ComponentManager manager = new ComponentManager(new File("target/test-dependencies"), "META-INF/test/dependencies", "org.talend.test:type=plugin,value=%s")) {
doCheckRegistry(plugin1, plugin2, manager);
final Date plugin1CreatedDate = doCheckJmx(mBeanServer);
// now remove a plugin and check the meta disappeared
manager.removePlugin(plugin1.getName().replace(".jar", ""));
assertEquals(1, manager.find(Stream::of).count());
assertEquals("plugin2", manager.find(Stream::of).findFirst().get().getId());
manager.addPlugin(plugin1.getAbsolutePath());
final Date plugin1RecreatedDate = doCheckJmx(mBeanServer);
assertTrue(plugin1CreatedDate.getTime() <= plugin1RecreatedDate.getTime());
} finally {
// clean temp files
DynamicContainerFinder.LOADERS.clear();
Stream.of(pluginFolder.listFiles()).forEach(File::delete);
if (ofNullable(pluginFolder.listFiles()).map(f -> f.length == 0).orElse(true)) {
pluginFolder.delete();
}
}
// unregistered
assertFalse(mBeanServer.isRegistered(new ObjectName("org.talend.test:type=plugin,value=plugin1")));
}
Aggregations