1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.com.sun.javafx.runtime;
  27 
  28 import com.sun.javafx.runtime.VersionInfo;
  29 import org.junit.Test;
  30 import static org.junit.Assert.*;
  31 
  32 /**
  33  */
  34 public class VersionInfoTest {
  35 
  36     private static class Version {
  37         private String vnum = "";
  38         private String suffix = "";
  39         private String build = "";
  40         private String opt = "";
  41 
  42         // Version number is in the following form:
  43         // $VNUM[-$SUFFIX][+$BUILD[-$OPT]]
  44         private Version(String version) {
  45             int plusIdx = version.indexOf("+");
  46             int firstDashIdx = version.indexOf("-");
  47             if (plusIdx < 0) {
  48                 if (firstDashIdx >= 0) {
  49                     vnum = version.substring(0, firstDashIdx);
  50                     suffix = version.substring(firstDashIdx+1);
  51                 } else {
  52                     vnum = version;
  53                 }
  54             } else {
  55                 if (firstDashIdx < 0) {
  56                     vnum = version.substring(0, plusIdx);
  57                     build = version.substring(plusIdx+1);
  58                 } else {
  59                     if (firstDashIdx < plusIdx) {
  60                         vnum = version.substring(0, firstDashIdx);
  61                         suffix = version.substring(firstDashIdx+1, plusIdx);
  62                         String rest = version.substring(plusIdx+1);
  63                         int nextDashIndex = rest.indexOf("-");
  64                         if (nextDashIndex < 0) {
  65                             build = rest;
  66                         } else {
  67                             build = rest.substring(0, nextDashIndex);
  68                             opt = rest.substring(nextDashIndex+1);
  69                         }
  70                     } else {
  71                         vnum = version.substring(0, plusIdx);
  72                         build = version.substring(plusIdx+1, firstDashIdx);
  73                         opt = version.substring(firstDashIdx+1);
  74                     }
  75                 }
  76             }
  77 
  78 //            System.err.println("version = " + version);
  79 //            System.err.println("    vnum = " + vnum);
  80 //            System.err.println("    suffix = " + suffix);
  81 //            System.err.println("    build = " + build);
  82 //            System.err.println("    opt = " + opt);
  83 //            System.err.println();
  84         }
  85     }
  86 
  87     @Test
  88     public void testMajorVersion() {
  89         String version = VersionInfo.getVersion();
  90         // Need to update major version number when we develop the next
  91         // major release.
  92         assertTrue(version.startsWith("11"));
  93         String runtimeVersion = VersionInfo.getRuntimeVersion();
  94         assertTrue(runtimeVersion.startsWith(version));
  95     }
  96 
  97     @Test
  98     public void testBuildNumber() {
  99         String version = VersionInfo.getVersion();
 100         assertFalse(version.contains("+"));
 101         Version v = new Version(version);
 102         assertEquals("", v.build);
 103         assertEquals("", v.opt);
 104 
 105         String runtimeVersion = VersionInfo.getRuntimeVersion();
 106         assertTrue(runtimeVersion.contains("+"));
 107         v = new Version(runtimeVersion);
 108         assertTrue(v.build.length() > 0);
 109         int buildNum = Integer.parseInt(v.build);
 110         assertTrue(buildNum >= 0);
 111     }
 112 
 113     @Test
 114     public void testNoFcs() {
 115         String version = VersionInfo.getVersion();
 116         assertFalse(version.contains("fcs"));
 117         String runtimeVersion = VersionInfo.getRuntimeVersion();
 118         assertFalse(runtimeVersion.contains("fcs"));
 119     }
 120 
 121     @Test
 122     public void testSuffixOpt() {
 123         String runtimeVersion = VersionInfo.getRuntimeVersion();
 124         int internalIndex = runtimeVersion.indexOf("-internal");
 125         boolean isInternal = internalIndex > 0;
 126         Version v = new Version(runtimeVersion);
 127         if (isInternal) {
 128             assertEquals("internal", v.suffix);
 129             assertTrue(v.opt.length() > 0);
 130         } else {
 131             assertFalse("internal".equals(v.suffix));
 132         }
 133     }
 134 
 135     @Test
 136     public void testNonPublic() {
 137         String runtimeVersion = VersionInfo.getRuntimeVersion();
 138         Version v = new Version(runtimeVersion);
 139         String milestone = VersionInfo.getReleaseMilestone();
 140         String timestamp = VersionInfo.getBuildTimestamp();
 141         String hudsonJob = VersionInfo.getHudsonJobName();
 142         assertEquals(milestone, v.suffix);
 143         if (hudsonJob.length() == 0) {
 144             assertEquals(timestamp, v.opt);
 145             assertEquals("internal", v.suffix);
 146         } else {
 147             assertFalse("internal".equals(v.suffix));
 148         }
 149     }
 150 
 151 }