1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.ui.common.jvm;
  34 
  35 import static org.junit.Assert.assertEquals;
  36 
  37 import org.junit.Test;
  38 
  39 import org.openjdk.jmc.ui.common.jvm.JVMCommandLineToolkit;
  40 
  41 public class JVMCommandLineToolkitTest {
  42 
  43         // Tests for getMainClassOrJar
  44 
  45         @Test
  46         public void testEclipseJar() {
  47                 assertEquals(
  48                                 "D:\\eclipse\\install\\eclipse-rcp-helios-win32-x86_64\\eclipse\\plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar",
  49                                 JVMCommandLineToolkit.getMainClassOrJar(
  50                                                 "D:\\eclipse\\install\\eclipse-rcp-helios-win32-x86_64\\eclipse\\plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar -os win32 -ws win32 -arch x86_64 -showsplash -launcher D:\\eclipse\\install\\eclipse-rcp-helios-win32-x86_64\\eclipse\\eclipse.exe -name Eclipse --launcher.library D:\\eclipse\\install\\eclipse-rcp-helios-win32-x86_64\\eclipse\\plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.0.v20100503\\eclipse_1307.dll -startup D:\\eclipse\\install\\eclipse-rcp-helios-win32-x86_64\\eclipse\\plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar -exitdata 1528_5c -product org.eclipse.epp.package.rcp.product -showlocation -vm d:/jrockits/R28.0.1_R28.0.1-21_1.6.0/bin/javaw.exe -vmargs -Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx512m -jar D:\\eclipse\\install\\eclipse-rcp-helios-win32-x86_64\\eclipse\\plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar"));
  51         }
  52 
  53         @Test
  54         public void testSimpleClass() {
  55                 assertEquals("org.openjdk.jmc.test.Runner1",
  56                                 JVMCommandLineToolkit.getMainClassOrJar("org.openjdk.jmc.test.Runner1"));
  57         }
  58 
  59         @Test
  60         public void testJarAndMoreFlags() {
  61                 assertEquals("/path1/path2/mc.jar", JVMCommandLineToolkit
  62                                 .getMainClassOrJar("/path1/path2/mc.jar -consoleLog -data /work/path3"));
  63         }
  64 
  65         @Test
  66         public void testSlashClassAndFlags() {
  67                 assertEquals("org/netbeans/Main", JVMCommandLineToolkit.getMainClassOrJar("org/netbeans/Main --branding nb"));
  68         }
  69 
  70         @Test
  71         public void testEclipsClassAndFlOags() {
  72                 assertEquals("org.eclipse.equinox.launcher.Main", JVMCommandLineToolkit.getMainClassOrJar(
  73                                 "org.eclipse.equinox.launcher.Main -launcher C:\\path1\\path2\\eclipse\\eclipse.exe -name Eclipse -showsplash 600 -product org.openjdk.jmc.rcp.application.product -data C:\\workspaces\\mcmain/../jmc_rcp -configuration file:C:/workspaces/mcmain/.metadata/.plugins/org.eclipse.pde.core/JMC RCP/ -dev file:C:/workspaces/mcmain/.metadata/.plugins/org.eclipse.pde.core/JMC RCP/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog"));
  74         }
  75 
  76         @Test
  77         public void testEmptyString() {
  78                 assertEquals("", JVMCommandLineToolkit.getMainClassOrJar(""));
  79         }
  80 
  81         @Test
  82         public void testNull() {
  83                 assertEquals(null, JVMCommandLineToolkit.getMainClassOrJar(null));
  84         }
  85 
  86         // Tests for getJavaCommandLine
  87 
  88         @Test
  89         public void testCpAndClass() {
  90                 assertEquals("org.openjdk.jmc.test.Runner2",
  91                                 JVMCommandLineToolkit.getJavaCommandLine("-cp application.jar org.openjdk.jmc.test.Runner2"));
  92         }
  93 
  94         @Test
  95         public void testCpWithSpaceDashAndClass() {
  96                 assertEquals("org.openjdk.jmc.test.Runner2", JVMCommandLineToolkit
  97                                 .getJavaCommandLine("-cp \"foo -bar/application.jar\" org.openjdk.jmc.test.Runner2"));
  98         }
  99 
 100         @Test
 101         public void testCpPropClassAndArg() {
 102                 assertEquals("org.openjdk.jmc.test.Runner3 arg", JVMCommandLineToolkit
 103                                 .getJavaCommandLine("-cp application.jar -Darg1=foo org.openjdk.jmc.test.Runner3 arg"));
 104         }
 105 
 106         @Test
 107         public void testJarFlag() {
 108                 assertEquals("C:/path1/path2/mc.jar -consoleLog -data C:/path3/path4",
 109                                 JVMCommandLineToolkit.getJavaCommandLine(
 110                                                 "-jar C:/path1/path2/mc.jar -consoleLog -data C:/path3/path4"));
 111         }
 112 
 113         @Test
 114         public void testClassPathJarAndFlags() {
 115                 // not really a valid cmdline
 116                 assertEquals("C:/path1/path2/mc.jar -consoleLog -data C:/path3/path4",
 117                                 JVMCommandLineToolkit.getJavaCommandLine(
 118                                                 "-classpath application.jar -jar C:/path1/path2/mc.jar -consoleLog -data C:/path3/path4"));
 119         }
 120 
 121         @Test
 122         public void testEmptyString2() {
 123                 assertEquals("", JVMCommandLineToolkit.getJavaCommandLine(""));
 124         }
 125 
 126         @Test
 127         public void testNull2() {
 128                 assertEquals(null, JVMCommandLineToolkit.getJavaCommandLine(null));
 129         }
 130 }