Search in sources :

Example 1 with TemporaryFolder

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();
}
Also used : TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder)

Example 2 with TemporaryFolder

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();
    }
}
Also used : PluginGenerator(org.talend.sdk.component.runtime.manager.asm.PluginGenerator) UUID(java.util.UUID) Serializer.roundTrip(org.talend.sdk.component.runtime.manager.test.Serializer.roundTrip) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) File(java.io.File) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) DynamicContainerFinder(org.talend.sdk.component.runtime.manager.serialization.DynamicContainerFinder) Thread.sleep(java.lang.Thread.sleep) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) Method(java.lang.reflect.Method) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) Method(java.lang.reflect.Method) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with TemporaryFolder

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());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Collections.list(java.util.Collections.list) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Arrays.asList(java.util.Arrays.asList) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) JarInputStream(java.util.jar.JarInputStream) Collections.singletonMap(java.util.Collections.singletonMap) Comparator.comparing(java.util.Comparator.comparing) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) JarOutputStream(java.util.jar.JarOutputStream) ZipEntry(java.util.zip.ZipEntry) Files(java.nio.file.Files) OS(org.junit.jupiter.api.condition.OS) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors.joining(java.util.stream.Collectors.joining) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) Writer(java.io.Writer) BufferedReader(java.io.BufferedReader) InputStreamReader(java.io.InputStreamReader) JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) FileWriter(java.io.FileWriter) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) JarFile(java.util.jar.JarFile) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Test(org.junit.jupiter.api.Test)

Example 4 with TemporaryFolder

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);
}
Also used : MonoMeecrowaveConfig(org.apache.meecrowave.junit5.MonoMeecrowaveConfig) JarFile(java.util.jar.JarFile) Collections.singletonList(java.util.Collections.singletonList) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) MediaType(javax.ws.rs.core.MediaType) Link(org.talend.sdk.component.server.front.model.Link) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) Map(java.util.Map) ComponentDetailList(org.talend.sdk.component.server.front.model.ComponentDetailList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) WebsocketClient(org.talend.sdk.component.server.test.websocket.WebsocketClient) DependencyDefinition(org.talend.sdk.component.server.front.model.DependencyDefinition) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) GenericType(javax.ws.rs.core.GenericType) Test(org.junit.jupiter.api.Test) Base64(java.util.Base64) List(java.util.List) APPLICATION_OCTET_STREAM_TYPE(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE) ComponentClient(org.talend.sdk.component.server.test.ComponentClient) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) PropertyValidation(org.talend.sdk.component.server.front.model.PropertyValidation) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Dependencies(org.talend.sdk.component.server.front.model.Dependencies) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) Entity.entity(javax.ws.rs.client.Entity.entity) DeploymentException(javax.websocket.DeploymentException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ComponentIndex(org.talend.sdk.component.server.front.model.ComponentIndex) ComponentIndices(org.talend.sdk.component.server.front.model.ComponentIndices) File(java.io.File) Consumer(java.util.function.Consumer) IO(org.apache.ziplock.IO) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) WebTarget(javax.ws.rs.client.WebTarget) Comparator(java.util.Comparator) InputStream(java.io.InputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 5 with TemporaryFolder

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")));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AttributeNotFoundException(javax.management.AttributeNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) PluginGenerator(org.talend.sdk.component.runtime.manager.asm.PluginGenerator) ObjectInputStream(java.io.ObjectInputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ByteArrayInputStream(java.io.ByteArrayInputStream) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) MBeanServer(javax.management.MBeanServer) DynamicContainerFinder(org.talend.sdk.component.runtime.manager.serialization.DynamicContainerFinder) ObjectOutputStream(java.io.ObjectOutputStream) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ManagementFactory(java.lang.management.ManagementFactory) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) Collections.emptyMap(java.util.Collections.emptyMap) Container(org.talend.sdk.component.container.Container) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) IOException(java.io.IOException) UUID(java.util.UUID) ObjectName(javax.management.ObjectName) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Processor(org.talend.sdk.component.runtime.output.Processor) EnhancedObjectInputStream(org.talend.sdk.component.runtime.serialization.EnhancedObjectInputStream) File(java.io.File) Test(org.junit.jupiter.api.Test) MBeanException(javax.management.MBeanException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectOutputStream(java.io.ObjectOutputStream) EnhancedObjectInputStream(org.talend.sdk.component.runtime.serialization.EnhancedObjectInputStream) Stream(java.util.stream.Stream) File(java.io.File) Date(java.util.Date) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Aggregations

TemporaryFolder (org.talend.sdk.component.junit.base.junit5.TemporaryFolder)6 File (java.io.File)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Test (org.junit.jupiter.api.Test)5 WithTemporaryFolder (org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder)5 IOException (java.io.IOException)4 List (java.util.List)4 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)4 FileOutputStream (java.io.FileOutputStream)3 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)3 FileWriter (java.io.FileWriter)2 Writer (java.io.Writer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Files (java.nio.file.Files)2 Collection (java.util.Collection)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.singletonMap (java.util.Collections.singletonMap)2 UUID (java.util.UUID)2 Collectors.toList (java.util.stream.Collectors.toList)2 Assertions.fail (org.junit.jupiter.api.Assertions.fail)2