Search in sources :

Example 1 with Deployer

use of org.glassfish.embeddable.Deployer in project Payara by payara.

the class EmbeddedTest method testEjb.

@Test
public void testEjb() throws GlassFishException {
    Deployer deployer = glassfish.getDeployer();
    URL source = SampleEjb.class.getClassLoader().getResource("org/glassfish/distributions/test/ejb/SampleEjb.class");
    String p = source.getPath().substring(0, source.getPath().length() - "org/glassfish/distributions/test/ejb/SimpleEjb.class".length());
    String appName = deployer.deploy(new File(p).toURI(), "--name=sample");
    Assert.assertNotNull("AppName is null from deployer of type " + deployer.getClass().getName(), appName);
    // ok now let's look up the EJB...
    try {
        InitialContext ic = new InitialContext();
        SampleEjb ejb = (SampleEjb) ic.lookup("java:global/sample/SampleEjb");
        if (ejb != null) {
            try {
                System.out.println(ejb.saySomething());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (NamingException e) {
        // To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
    }
    deployer.undeploy(appName);
    System.out.println("Done with EJB");
}
Also used : NamingException(javax.naming.NamingException) SampleEjb(org.glassfish.distributions.test.ejb.SampleEjb) File(java.io.File) Deployer(org.glassfish.embeddable.Deployer) URL(java.net.URL) InitialContext(javax.naming.InitialContext) GlassFishException(org.glassfish.embeddable.GlassFishException) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 2 with Deployer

use of org.glassfish.embeddable.Deployer in project Payara by payara.

the class EmbeddedTest method testWeb.

@Test
public void testWeb() throws GlassFishException {
    System.out.println("Starting testWeb " + glassfish);
    Deployer deployer = glassfish.getDeployer();
    URL source = SampleEjb.class.getClassLoader().getResource("org/glassfish/distributions/test/web/WebHello.class");
    String p = source.getPath().substring(0, source.getPath().length() - "org/glassfish/distributions/test/web/WebHello.class".length());
    File path = new File(p).getParentFile().getParentFile();
    String name = null;
    if (path.getName().lastIndexOf('.') != -1) {
        name = path.getName().substring(0, path.getName().lastIndexOf('.'));
    } else {
        name = path.getName();
    }
    System.out.println("Deploying " + path + ", name = " + name);
    String appName = deployer.deploy(path.toURI(), "--name=" + name);
    System.out.println("Deployed " + appName);
    Assert.assertTrue(appName != null);
    try {
        URL servlet = new URL("http://localhost:8080/test-classes/hello");
        URLConnection yc = servlet.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine = in.readLine();
        if (inputLine != null) {
            System.out.println(inputLine);
        }
        Assert.assertNotNull(inputLine);
        Assert.assertEquals(inputLine.trim(), "Hello World !");
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    // do not throw the exception for now, because this may break the build if,
    // for example, another instance of glassfish is running on 8080
    // throw e;
    }
    if (appName != null) {
        deployer.undeploy(appName);
        System.out.println("Undeployed " + appName);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SampleEjb(org.glassfish.distributions.test.ejb.SampleEjb) File(java.io.File) Deployer(org.glassfish.embeddable.Deployer) URL(java.net.URL) URLConnection(java.net.URLConnection) GlassFishException(org.glassfish.embeddable.GlassFishException) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 3 with Deployer

use of org.glassfish.embeddable.Deployer in project Payara by payara.

the class CustomDBNameTest method test.

@Test
public void test() throws Exception {
    // 1. Bootstrap GlassFish DAS in embedded mode.
    GlassFishProperties glassFishProperties = new GlassFishProperties();
    glassFishProperties.setInstanceRoot(System.getenv("S1AS_HOME") + "/domains/domain1");
    glassFishProperties.setConfigFileReadOnly(false);
    GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(glassFishProperties);
    PrintStream sysout = System.out;
    glassfish.start();
    System.setOut(sysout);
    // 2. Deploy the PaaS application.
    File archive = new File(System.getProperty("basedir") + // TODO :: use mvn
    "/target/custom_db_name_paas_sample.war");
    // apis to get
    // the archive
    // location.
    Assert.assertTrue(archive.exists());
    Deployer deployer = null;
    String appName = null;
    try {
        deployer = glassfish.getDeployer();
        appName = deployer.deploy(archive);
        System.err.println("Deployed [" + appName + "]");
        Assert.assertNotNull(appName);
        CommandRunner commandRunner = glassfish.getCommandRunner();
        CommandResult result = commandRunner.run("list-services");
        System.out.println("\nlist-services command output [ " + result.getOutput() + "]");
        // 3. Access the app to make sure PaaS app is correctly provisioned.
        String HTTP_PORT = (System.getProperty("http.port") != null) ? System.getProperty("http.port") : "28080";
        String instanceIP = getLBIPAddress(glassfish);
        get("http://" + instanceIP + ":" + HTTP_PORT + "/custom_db_name_paas_sample/CustomDBNameServlet", "Customer ID");
    // 4. Undeploy the PaaS application .
    } finally {
        if (appName != null) {
            deployer.undeploy(appName);
            System.out.println("Destroying the resources created");
            System.err.println("Undeployed [" + appName + "]");
        }
    }
}
Also used : PrintStream(java.io.PrintStream) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) CommandRunner(org.glassfish.embeddable.CommandRunner) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) CommandResult(org.glassfish.embeddable.CommandResult) Test(org.junit.Test)

Example 4 with Deployer

use of org.glassfish.embeddable.Deployer in project Payara by payara.

the class HelloWorldTest method runTests.

private void runTests(GlassFish glassfish) throws Exception {
    // 2. Deploy the PaaS application.
    File archive = new File(System.getProperty("basedir") + "/target/helloworld.war");
    Assert.assertTrue(archive.exists());
    Deployer deployer = null;
    String appName = null;
    try {
        deployer = glassfish.getDeployer();
        appName = deployer.deploy(archive);
        System.err.println("Deployed [" + appName + "]");
        Assert.assertNotNull(appName);
        CommandRunner commandRunner = glassfish.getCommandRunner();
        CommandResult result = commandRunner.run("list-services");
        System.out.println("\nlist-services command output [ " + result.getOutput() + "]");
        // 3. Access the app to make sure PaaS app is correctly provisioned.
        String HTTP_PORT = (System.getProperty("http.port") != null) ? System.getProperty("http.port") : "28080";
        List<String> ips = getLBIPAddress(glassfish);
        for (String ip : ips) {
            get("http://" + ip + ":" + HTTP_PORT + "/helloworld/hi.jsp", "PaaS says Hello World!");
        }
    // 4. Undeploy the PaaS application.
    } finally {
        if (appName != null) {
            deployer.undeploy(appName);
            System.err.println("Undeployed [" + appName + "]");
            try {
                boolean undeployClean = false;
                CommandResult commandResult = glassfish.getCommandRunner().run("list-services");
                if (commandResult.getOutput().contains("Nothing to list.")) {
                    undeployClean = true;
                }
                Assert.assertTrue(undeployClean);
            } catch (Exception e) {
                System.err.println("Couldn't verify whether undeploy succeeded");
            }
        }
    }
}
Also used : File(java.io.File) CommandRunner(org.glassfish.embeddable.CommandRunner) Deployer(org.glassfish.embeddable.Deployer) CommandResult(org.glassfish.embeddable.CommandResult)

Example 5 with Deployer

use of org.glassfish.embeddable.Deployer in project Payara by payara.

the class CoffeeTest method test.

@Test
public void test() throws Exception {
    // 1. Bootstrap GlassFish DAS in embedded mode.
    GlassFishProperties glassFishProperties = new GlassFishProperties();
    glassFishProperties.setInstanceRoot(System.getenv("S1AS_HOME") + "/domains/domain1");
    glassFishProperties.setConfigFileReadOnly(false);
    GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(glassFishProperties);
    PrintStream sysout = System.out;
    glassfish.start();
    System.setOut(sysout);
    // 2. Deploy the PaaS application.
    File archive = new File(System.getProperty("basedir") + "/target/external_db_service_test.war");
    // TODO :: use mvn apis to get the archive location.
    Assert.assertTrue(archive.exists());
    Deployer deployer = null;
    String appName = null;
    CommandRunner commandRunner = glassfish.getCommandRunner();
    try {
        // 2.1. Generate and run Derby VM
        CommandResult createDerbyVMResult = commandRunner.run("create-derby-vm", "--servicecharacteristics", "service-type=Database");
        System.out.println("\ncreate-derby-vm command output [ " + createDerbyVMResult.getOutput() + "]");
        String derbyIP = (createDerbyVMResult.getOutput().split("\n"))[1];
        System.out.println("Derby IP = " + derbyIP);
        // 2.2. Create the shared DB service
        CommandResult createExternalServiceResult = commandRunner.run("create-external-service", "--configuration", "ip-address=" + derbyIP + ":databasename=foo:connectionAttributes=;'create=true':port=1527:user=APP:password=APP:host=" + derbyIP + ":classname=org.apache.derby.jdbc.ClientXADataSource:resourcetype=javax.sql.XADataSource", "--servicetype", "Database", "coffee-service");
        System.out.println("\ncreate-external-service command output [ " + createExternalServiceResult.getOutput() + "]");
        // 2.3. List services to check for the shared service
        CommandResult listSharedServicesResult = commandRunner.run("list-services");
        System.out.println("\nlist-services command output [ " + listSharedServicesResult.getOutput() + "]");
        // 2.4. Deploy app
        deployer = glassfish.getDeployer();
        appName = deployer.deploy(archive);
        System.err.println("Deployed [" + appName + "]");
        Assert.assertNotNull(appName);
        CommandResult result = commandRunner.run("list-services");
        System.out.println("\nlist-services command output [ " + result.getOutput() + "]");
        // 3. Access the app to make sure PaaS app is correctly provisioned.
        String HTTP_PORT = (System.getProperty("http.port") != null) ? System.getProperty("http.port") : "28080";
        String instanceIP = getLBIPAddress(glassfish);
        get("http://" + instanceIP + ":" + HTTP_PORT + "/external_db_service_test/CoffeeServlet", "Coffee ID");
    // 4. Undeploy the PaaS application .
    } finally {
        if (appName != null) {
            deployer.undeploy(appName);
            System.err.println("Undeployed [" + appName + "]");
            System.out.println("Destroying the resources created");
            // 4.1. Delete External DB Service.
            CommandResult deleteResult = commandRunner.run("delete-external-service", "coffee-service");
            System.out.println("\ndelete-external-service command output [" + deleteResult.getOutput() + "]");
            // 4.2. Delete generated derby VM
            CommandResult deleteDerbyVMResult = commandRunner.run("delete-derby-vm");
            System.out.println("\ndelete-derby-vm command output [ " + deleteDerbyVMResult.getOutput() + "]");
        }
    }
}
Also used : PrintStream(java.io.PrintStream) GlassFish(org.glassfish.embeddable.GlassFish) JarFile(java.util.jar.JarFile) File(java.io.File) CommandRunner(org.glassfish.embeddable.CommandRunner) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Deployer(org.glassfish.embeddable.Deployer) CommandResult(org.glassfish.embeddable.CommandResult) Test(org.junit.Test)

Aggregations

Deployer (org.glassfish.embeddable.Deployer)33 File (java.io.File)32 GlassFish (org.glassfish.embeddable.GlassFish)24 Test (org.junit.Test)24 CommandRunner (org.glassfish.embeddable.CommandRunner)22 CommandResult (org.glassfish.embeddable.CommandResult)21 GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)21 PrintStream (java.io.PrintStream)16 JarFile (java.util.jar.JarFile)13 URI (java.net.URI)6 IOException (java.io.IOException)4 InitialContext (javax.naming.InitialContext)4 GlassFishException (org.glassfish.embeddable.GlassFishException)4 URL (java.net.URL)3 ScatteredArchive (org.glassfish.embeddable.archive.ScatteredArchive)3 HttpListener (org.glassfish.embeddable.web.HttpListener)3 WebContainer (org.glassfish.embeddable.web.WebContainer)3 NamingException (javax.naming.NamingException)2 SampleEjb (org.glassfish.distributions.test.ejb.SampleEjb)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2