/
OSGiCommandsITest.java
154 lines (134 loc) · 5.67 KB
/
OSGiCommandsITest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.admin.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.glassfish.main.itest.tools.GlassFishTestEnvironment;
import org.glassfish.main.itest.tools.asadmin.Asadmin;
import org.glassfish.main.itest.tools.asadmin.AsadminResult;
import org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher;
import org.hamcrest.collection.IsEmptyCollection;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher.asadminOK;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author sanjeeb.sahoo@oracle.com
*/
public class OSGiCommandsITest {
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();
@BeforeAll
public static void waitOsgiReady() throws Exception {
for (int i = 0; i < 5; i++) {
AsadminResult result = ASADMIN.exec("osgi", "lb");
if (!result.isError()) {
return;
}
Thread.sleep(1000);
}
}
@Test
public void basicOsgiCmd() {
assertThat(ASADMIN.exec("osgi", "lb"), asadminOK());
}
/**
* Tests functionality of session handling of osgi command.
* It creates sessions, lists them, executes commands against each session and finally stops them.
* @throws Exception
*/
@Test
public void osgiCmdSession() throws Exception {
// Create some sessions
Set<String> sessions = new HashSet<>();
for (int i = 0; i < 3; ++i) {
sessions.add(newCmdSession());
}
// Let's list them to make sure list operation works.
final Set<String> actual = listCmdSessions();
assertEquals(sessions, actual, "listed sessions do not match with created sessions");
// Let's set the same variable in each command session with a different value and make sure
// the variables are scoped to sessions.
for (String sessionId : sessions) {
List<String> result = runCmd("osgi", "--session", "execute", "--session-id", sessionId, "var=" + sessionId);
assertThat(result, IsEmptyCollection.empty());
}
for (String sessionId : sessions) {
List<String> result = runCmd("osgi", "--session", "execute", "--session-id", sessionId, "echo $var");
assertThat(result, not(IsEmptyCollection.empty()));
assertEquals(sessionId, result.get(0));
}
// Let's stop all sessions.
for (String sessionId : sessions) {
List<String> result = runCmd("osgi", "--session", "stop", "--session-id", sessionId);
assertThat(result, IsEmptyCollection.empty());
}
sessions = listCmdSessions();
assertTrue(sessions.isEmpty(), "Not all sessions closed properly: " + sessions);
}
/**
* Test osgi-shell command which is a local command. It takes a file as input. The file contains
* a list of shell commands to be executed.
* @throws IOException
*/
@Test
public void osgiShell() throws IOException {
File cmdFile = File.createTempFile("osgi-commands", ".txt");
cmdFile.deleteOnExit();
try (PrintStream ps = new PrintStream(new FileOutputStream(cmdFile))) {
ps.println("help");
ps.println("lb");
}
AsadminResult result = ASADMIN.exec("osgi-shell", "--file", cmdFile.getAbsolutePath());
assertThat(result.getStdOut(),
stringContainsInOrder("cm:createFactoryConfiguration", "System Bundle", "Apache Felix Gogo Runtime"));
assertThat(result, AsadminResultMatcher.asadminOK());
}
private String newCmdSession() throws Exception {
List<String> value = runCmd("osgi", "--session", "new");
if (value.size() != 1) {
throw new Exception("Unexpected output: \n " + value);
}
return value.get(0);
}
private Set<String> listCmdSessions() throws Exception {
List<String> sessions = runCmd("osgi", "--session", "list");
return new HashSet<>(sessions);
}
private List<String> runCmd(String... cmd) throws Exception {
AsadminResult value = ASADMIN.exec(cmd);
assertThat(value, asadminOK());
List<String> output = new ArrayList<>();
for (String line : value.getStdOut().split("\\n")) {
line = line.trim();
if (line.isEmpty() || line.startsWith(ASADMIN.getCommandName()) || line.startsWith("Command")) {
continue;
}
output.add(line);
}
return output;
}
}