Java 8 in IaaS InfoboxCloud in one team. We use Java 8 in tests

    More recently Java 8 was released. This is a big and long-awaited event for all developers on the JVM platform and will certainly affect the code in enterprise projects.

    We at InfoboxCloud really love Java technology and value Java developers. For the release of Java 8, we have written scripts that will allow you to install JRE 8 or JDK 8 in the InfoboxCloud Cloud Infrastructure on any Linux OS in one command . Just select a script, paste it into the console and press Enter. It is much better to spend time writing good code than installing java.
    Java 8 InfoboxCloud IaaS
    On the InfoboxCloud Jelastic Cloud Platform, Java 8 support will appear in the next software stack update pretty soon.

    In this article, having installed Java 8, we will look at how to test your development software with Java 8, building it under Java 7.


    Install Java 8 in one command


    To install the appropriate version of Java, simply log into the InfoboxCloud cloud infrastructure machine and paste the specified command. Of course, there is no vendor-lock in the scripts and they can be used on your server, but they were tested in InfoboxCloud. Tested on machines with CentOS 6, Ubuntu 12.04, Ubuntu 13.10, Debian 7, openSuse 13.1 for i686 and x86_64.

    Jdk 8

    wget repository.jelasticloud.com/scripts/jvm/8/jdk8 && chmod +x jdk8 && ./jdk8 && rm -rf jdk8
    


    JRE 8

    wget repository.jelasticloud.com/scripts/jvm/8/jre8 && chmod +x jre8 && ./jre8 && rm -rf jre8
    


    Install Java 7 in one command


    Of course, we understand that JDK7 and JRE7 will be used for a long time in the production environment, so we prepared installation scripts in one command for Java 7.
    Java 7 and 8 can be installed in parallel: it is recommended to install 7 first, then 8.

    JDK 7 update 51

    wget repository.jelasticloud.com/scripts/jvm/7u51/jdk7u51 && chmod +x jdk7u51 && ./jdk7u51 && rm -rf jdk7u51
    


    JRE 7 update 51

    wget repository.jelasticloud.com/scripts/jvm/7u51/jre7u51 && chmod +x jre7u51 && ./jre7u51 && rm -rf jre7u51
    


    We are writing tests in Java 8, and the application code in Java 7


    Java 8 is completely new. It will take many more updates to finally enterprise became confident in its quality. However, right now you can write tests in java 8, and the application code in java 7.
    Maven Compiler plugin starts in two modes: compile and testCompile. They can be configured individually.

    Add the following section to your pom file:
    1.71.71.81.8

    Now your src / main / java compiles under java 7, and src / main / test compiles under 8.

    If you have a parent pom for the project, you need to reload its configuration:


    This code fragment was loaded with a picture, because habrahabr incorrectly interprets the source tag in maven code. Github Gist is available here .

    Now you can test your project using JDK8. You might want to tell other developers to raise the level of the entire project to that used in your tests.
    Add to the build section:
    org.apache.maven.pluginsmaven-enforcer-plugin1.3.1enforce-javaenforce${maven.compiler.testTarget}

    Note that even compiling the project itself in 7, the compiler does not understand the difference between the APIs in 7 and 8. This means that the project will still compile successfully even if you use API 8 in src / main / java. Let's configure the check, preventing such a situation:
    org.codehaus.mojoanimal-sniffer-maven-plugin1.7signature-checkverifycheckorg.codehaus.mojo.signaturejava171.0

    The project is successfully configured. Let's now use JDK8 in tests:

    JDK 7 source code:

    import java.util.Arrays;
    import java.util.List;
    import java.util.concurrent.Callable;
    public class DoSomething {
       public String execute(Callable call) throws Exception {
          return call.call();
       }
       public List list() {
          return Arrays.asList("a", "b", "c", "d");
       }
    }
    


    Tests with JDK 8:

    import java.util.Optional;
    import org.junit.Assert;
    import org.junit.Test;
    public class DoSomethingTestClase {
       public static final String TEST = "ABCD";
       @Test
       public void shouldReturnString() throws Exception {
          String result = new DoSomething().execute(()-> TEST);
          Assert.assertEquals(TEST, result);
       }
       @Test
       public void shouldFilterResult() throws Exception {
          Optional result = new DoSomething().list()
             .stream()
                .map((a)-> a.toUpperCase())
                .reduce((a, b)->a+b);
          Assert.assertTrue(result.isPresent());
          Assert.assertEquals(TEST, result.get());
       }
    }
    

    Thanks for the idea of ​​testing like this with the JDK8 Aslak Knutsen.

    Successful programming with InfoboxCloud !

    Also popular now: