View Javadoc

1   // Copyright (C) 2004 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.engine.agent;
23  
24  import static java.util.Arrays.asList;
25  import static net.grinder.testutility.AssertUtilities.assertContainsPattern;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertFalse;
28  import static org.junit.Assert.assertNotNull;
29  import static org.junit.Assert.assertNull;
30  import static org.junit.Assert.assertTrue;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Properties;
36  
37  import net.grinder.common.GrinderProperties;
38  import net.grinder.testutility.AbstractJUnit4FileTestCase;
39  import net.grinder.util.Directory;
40  
41  import org.junit.Test;
42  
43  
44  /**
45   *  Unit tests for <code>WorkerProcessCommandLine</code>.
46   *
47   * @author Philip Aston
48   */
49  public class TestWorkerProcessCommandLine extends AbstractJUnit4FileTestCase {
50  
51    @Test public void testConstructorWithEmptyProperties() throws Exception {
52  
53      final WorkerProcessCommandLine workerProcessCommandLine =
54        new WorkerProcessCommandLine(new GrinderProperties(),
55                                     new Properties(),
56                                     null,
57                                     new Directory());
58  
59      assertEquals(
60        "java net.grinder.engine.process.WorkerProcessEntryPoint",
61        workerProcessCommandLine.toString());
62    }
63  
64    @Test public void testConstructorWithProperties() throws Exception {
65  
66      final GrinderProperties grinderProperties = new GrinderProperties() {{
67        setProperty("grinder.jvm.arguments", "-server -Xmx1024M");
68        setProperty("grinder.jvm.classpath", "abc;def");
69      }};
70  
71      final Properties overrideProperties = new Properties();
72  
73      final WorkerProcessCommandLine workerProcessCommandLine =
74        new WorkerProcessCommandLine(grinderProperties,
75                                     overrideProperties,
76                                     grinderProperties.getProperty("grinder.jvm.arguments"),
77                                     new Directory());
78  
79      assertEquals("java -server '-Xmx1024M' -classpath 'abc;def' net.grinder.engine.process.WorkerProcessEntryPoint",
80                   workerProcessCommandLine.toString());
81  
82      // Should work twice.
83      assertEquals("java -server '-Xmx1024M' -classpath 'abc;def' net.grinder.engine.process.WorkerProcessEntryPoint",
84                   workerProcessCommandLine.toString());
85    }
86  
87    @Test public void testConstructorWithAgent() throws Exception {
88  
89      final GrinderProperties grinderProperties = new GrinderProperties();
90  
91      final Properties overrideProperties = new Properties();
92  
93      final File agentFile = new File(getDirectory(), "grinder-dcr-agent-1.jar");
94      final File someJar = new File(getDirectory(), "some.jar");
95      overrideProperties.put("java.class.path", someJar.getAbsolutePath());
96      agentFile.createNewFile();
97  
98      final WorkerProcessCommandLine workerProcessCommandLine =
99        new WorkerProcessCommandLine(grinderProperties,
100                                    overrideProperties,
101                                    grinderProperties.getProperty("grinder.jvm.arguments"),
102                                    new Directory());
103 
104     assertContainsPattern(
105       workerProcessCommandLine.toString(),
106       "^java '-javaagent:[^\\s]*" + agentFile.getName() +
107       "' -classpath '[^\\s]*" + someJar.getName() +
108       "' net.grinder.engine.process.WorkerProcessEntryPoint$");
109   }
110 
111   @Test public void testWithSystemProperties() throws Exception {
112 
113     final GrinderProperties grinderProperties = new GrinderProperties() {{
114       setProperty("grinder.jvm.arguments", "-Xmx1024M");
115       setProperty("grinder.jvm.classpath", "abc;def");
116     }};
117 
118     final Properties systemProperties = new Properties() {{
119       setProperty("grinder.processes", "99"); // Ignored
120       setProperty("java.class.path", "jvd;vg;nc");
121     }};
122 
123     final WorkerProcessCommandLine workerProcessCommandLine =
124       new WorkerProcessCommandLine(grinderProperties,
125                                    systemProperties,
126                                    grinderProperties.getProperty("grinder.jvm.arguments"),
127                                    new Directory());
128 
129     String commandLine = workerProcessCommandLine.toString();
130 
131     final String expectedPrefix = "java '-Xmx1024M' ";
132 
133     assertTrue(commandLine, commandLine.startsWith(expectedPrefix));
134 
135     commandLine = commandLine.substring(expectedPrefix.length());
136 
137     final String expectedSuffix =
138       "-classpath 'abc;def" + File.pathSeparatorChar + "jvd;vg;nc' " +
139       "net.grinder.engine.process.WorkerProcessEntryPoint";
140 
141     assertEquals(expectedSuffix, commandLine);
142   }
143 
144   private static List<File> path(final String... strings) {
145     final List<File> result = new ArrayList<File>(strings.length);
146 
147     for (final String s : strings) {
148       result.add(new File(s));
149     }
150 
151     return result;
152   }
153 
154   @Test public void testIsAgentJarValidNames() {
155 
156     for (final String s : asList("grinder-dcr-agent-1.jar",
157                                  "grinder-dcr-agent-3.11.jar",
158                                  "grinder-dcr-agent-3.11-SNAPSHOT.jar")) {
159       assertTrue(s, WorkerProcessCommandLine.isAgentJar(s));
160     }
161   }
162 
163   @Test public void testIsAgentJarInvalidNames() {
164 
165     for (final String s : asList("grinder-dcr-agent.jar",
166                                  "grinder-dcr-agent-3.11.jar.asc",
167                                  "grinder-dcr-agent-3.11-sources.jar",
168                                  "xgrinder-dcr-agent-3.11.jar")) {
169       assertFalse(s, WorkerProcessCommandLine.isAgentJar(s));
170     }
171   }
172 
173   @Test public void testFindAgentJarFile() throws Exception {
174     assertNull(WorkerProcessCommandLine.findAgentJarFile(path("foo.jar")));
175 
176     assertNull(WorkerProcessCommandLine.findAgentJarFile(path("/foo.jar")));
177 
178     assertNull(
179       WorkerProcessCommandLine.findAgentJarFile(path("/notthere/foo.jar")));
180 
181     assertNull(
182       WorkerProcessCommandLine.findAgentJarFile(
183         path("somewhere ", "somewhereelse")));
184 
185     final File directories = new File(getDirectory(), "a/b");
186     directories.mkdirs();
187 
188     assertNull(
189       WorkerProcessCommandLine.findAgentJarFile(path(directories.getPath()
190                                                      + "/c.jar")));
191 
192     final File f = new File(directories, "grinder-dcr-agent-1.20.jar");
193     f.createNewFile();
194 
195     assertNotNull(
196       WorkerProcessCommandLine.findAgentJarFile(path(f.getAbsolutePath())));
197 
198     assertNull(
199       WorkerProcessCommandLine.findAgentJarFile(
200         asList(new File(getDirectory().getAbsoluteFile(), "c.jar"))));
201 
202     // I'd like also to test with relative paths, but this is impossible to
203     // do in a platform independent manner.
204   }
205 }