Skip to content

Commit f325cb3

Browse files
authored
Merge pull request #20545 from jimblye/18191-Create-unit-test-for-schemaGen-script
Create a unit test for schemaGen script
2 parents a650e6b + 5e87c98 commit f325cb3

File tree

1 file changed

+212
-0
lines changed
  • dev/com.ibm.ws.config.schemagen/test/com/ibm/ws/config/schemagen/internal

1 file changed

+212
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package com.ibm.ws.config.schemagen.internal;
2+
/*******************************************************************************
3+
* Copyright (c) 2022 IBM Corporation and others.
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Contributors:
10+
* IBM Corporation - initial API and implementation
11+
*******************************************************************************/
12+
13+
14+
import static org.junit.Assert.assertFalse;
15+
import static org.junit.Assert.assertNull;
16+
import static org.junit.Assert.assertTrue;
17+
18+
import java.io.BufferedReader;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.InputStreamReader;
22+
23+
import org.junit.Test;
24+
25+
/**
26+
* Tests the schemaGen command that exists in the wlp/bin directory.
27+
*/
28+
public class SchemaGenTest {
29+
30+
public static final String WLP_BIN_DIR = "../build.image/wlp/bin";
31+
public static final String OUTPUT_FILE = "schemaGenOutput.xsd";
32+
public static final String HELP_OPTION = "-help";
33+
public static final String SCHEMAGEN_BAT = "./schemaGen.bat";
34+
public static final String SCHEMAGEN_LINUX_SCRIPT = "./schemaGen";
35+
public static final long TIMEOUT = 30_000_000_000L; // 30-second timeout
36+
public static final int MAX_OUTPUT_LINES = 500; // Reasonable limit to output to standard output by schemaGen
37+
public static final boolean IS_WINDOWS = isWindows();
38+
39+
public static boolean isWindows() {
40+
String os = System.getProperty("os.name");
41+
if (os.startsWith("Win")) {
42+
return true;
43+
}
44+
return false;
45+
}
46+
47+
/**
48+
* Test that when no parameters are passed, only basic usage info is displayed
49+
* @throws IOException
50+
*/
51+
@Test
52+
public void testSchemaGenNoParms() throws IOException {
53+
System.out.println("==== testSchemaGenNoParms ====");
54+
55+
ProcessBuilder pb;
56+
Process p = null;
57+
58+
try {
59+
60+
if (IS_WINDOWS) {
61+
pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_BAT);
62+
} else {
63+
pb = new ProcessBuilder(SCHEMAGEN_LINUX_SCRIPT);
64+
}
65+
66+
File dir = new File(WLP_BIN_DIR);
67+
pb.directory(dir);
68+
p = pb.start();
69+
70+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
71+
String line;
72+
73+
boolean usageAppears = false;
74+
boolean encodingAppears = false;
75+
76+
long startTime = System.nanoTime();
77+
int lineCounter = 0;
78+
79+
while ((line = br.readLine()) != null) {
80+
System.out.println(line);
81+
82+
if (line.indexOf("Usage") != -1) {
83+
usageAppears = true;
84+
}
85+
86+
if (line.indexOf("--encoding") != -1) {
87+
encodingAppears = true;
88+
}
89+
90+
// Exit loop if we are getting hung
91+
if (lineCounter++ > MAX_OUTPUT_LINES) {
92+
System.out.println("schemaGen usage info exceeded [ " + MAX_OUTPUT_LINES + " ] lines");
93+
break;
94+
} else if (System.nanoTime() - startTime > TIMEOUT) {
95+
System.out.println("SchemaGen exceeded [ " + TIMEOUT + " ] ns when displaying usage Info");
96+
break;
97+
}
98+
}
99+
100+
assertTrue("'Usage' should appear in command output", usageAppears);
101+
assertFalse("'--encoding' should NOT appear in command output when no arguments passed.", encodingAppears);
102+
System.out.println("PASSED");
103+
104+
} finally {
105+
if ( p!= null) {
106+
p.destroy();
107+
}
108+
}
109+
}
110+
111+
/**
112+
* Test that when -help parameter is passed that help and usage information is displayed.
113+
* @throws IOException
114+
*/
115+
@Test
116+
public void testSchemaGenHelp() throws IOException {
117+
System.out.println("==== testSchemaGenHelp ====");
118+
119+
ProcessBuilder pb;
120+
Process p = null;
121+
try {
122+
123+
if (IS_WINDOWS) {
124+
pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_BAT, HELP_OPTION);
125+
} else {
126+
pb = new ProcessBuilder(SCHEMAGEN_LINUX_SCRIPT, HELP_OPTION);
127+
}
128+
129+
File dir = new File(WLP_BIN_DIR);
130+
pb.directory(dir);
131+
p = pb.start();
132+
133+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
134+
String line;
135+
136+
boolean usageAppears = false;
137+
boolean encodingAppears = false;
138+
139+
long startTime = System.nanoTime();
140+
int lineCounter = 0;
141+
142+
while ((line = br.readLine()) != null) {
143+
System.out.println(line);
144+
145+
if (line.indexOf("Usage") != -1) {
146+
usageAppears = true;
147+
}
148+
149+
if (line.indexOf("--encoding") != -1) {
150+
encodingAppears = true;
151+
}
152+
153+
// Exit loop if we are getting hung
154+
if (lineCounter++ > MAX_OUTPUT_LINES) {
155+
System.out.println("schemaGen help exceeded [ " + MAX_OUTPUT_LINES + " ] lines");
156+
break;
157+
} else if (System.nanoTime() - startTime > TIMEOUT) {
158+
System.out.println("schemaGen exceeded [ " + TIMEOUT + " ] ns when displaying help");
159+
break;
160+
}
161+
}
162+
163+
assertTrue("'Usage' should appear in command output", usageAppears);
164+
assertTrue("'--encoding' should appear in command output when " + HELP_OPTION + " is passed.", encodingAppears);
165+
System.out.println("PASSED");
166+
167+
} finally {
168+
if ( p!= null) {
169+
p.destroy();
170+
}
171+
}
172+
}
173+
174+
/**
175+
* Test that when an output file is specified as parameter that the output file is created.
176+
* @throws IOException
177+
*/
178+
@Test
179+
public void testSchemaGenOutput() throws IOException {
180+
System.out.println("==== testSchemaGenOutput ====");
181+
182+
ProcessBuilder pb;
183+
Process p = null;
184+
try {
185+
if (IS_WINDOWS) {
186+
pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_BAT, OUTPUT_FILE);
187+
} else {
188+
pb = new ProcessBuilder(SCHEMAGEN_LINUX_SCRIPT, OUTPUT_FILE);
189+
}
190+
191+
File dir = new File(WLP_BIN_DIR);
192+
pb.directory(dir);
193+
p = pb.start();
194+
195+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
196+
197+
// The command should not generate any output to stdio or stderr
198+
assertNull("Stream should be null", br.readLine());
199+
200+
// It should, however, generate the output file.
201+
File outputFile = new File(WLP_BIN_DIR + "/" + OUTPUT_FILE);
202+
assertTrue("File [" + outputFile.getName() + "] should exist.", outputFile.exists());
203+
outputFile.delete(); // clean up
204+
System.out.println("PASSED");
205+
206+
} finally {
207+
if ( p!= null) {
208+
p.destroy();
209+
}
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)