diff --git a/dev/com.ibm.ws.config.schemagen/test/com/ibm/ws/config/schemagen/internal/SchemaGenTest.java b/dev/com.ibm.ws.config.schemagen/test/com/ibm/ws/config/schemagen/internal/SchemaGenTest.java index 89f9f54f6b34..0023f38eceb8 100644 --- a/dev/com.ibm.ws.config.schemagen/test/com/ibm/ws/config/schemagen/internal/SchemaGenTest.java +++ b/dev/com.ibm.ws.config.schemagen/test/com/ibm/ws/config/schemagen/internal/SchemaGenTest.java @@ -1,6 +1,6 @@ package com.ibm.ws.config.schemagen.internal; /******************************************************************************* - * Copyright (c) 2022 IBM Corporation and others. + * Copyright (c) 2022,2024 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -12,15 +12,14 @@ * IBM Corporation - initial API and implementation *******************************************************************************/ - import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.BufferedReader; import java.io.File; -import java.io.IOException; import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; import org.junit.Test; @@ -29,15 +28,35 @@ */ public class SchemaGenTest { - public static final String WLP_BIN_DIR = "../build.image/wlp/bin"; - public static final String OUTPUT_FILE = "schemaGenOutput.xsd"; - public static final String HELP_OPTION = "-help"; - public static final String SCHEMAGEN_BAT = "./schemaGen.bat"; - public static final String SCHEMAGEN_LINUX_SCRIPT = "./schemaGen"; - public static final long TIMEOUT = 30_000_000_000L; // 30-second timeout - public static final int MAX_OUTPUT_LINES = 500; // Reasonable limit to output to standard output by schemaGen - public static final boolean IS_WINDOWS = isWindows(); - + /* + * ==== testSchemaGenOutput ==== + * Environment: + * OS [ os.name ] [ Windows 10 ] isWindows [ true ] + * WLP Bin [ ../build.image/wlp/bin ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin ] Exists [ true ] + * Script [ ./schemaGen.bat ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin\.\schemaGen.bat ] Exists [ true ] + * Output [ schemaGenOutput.xsd ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin\schemaGenOutput.xsd ] Exists [ false ] + * Running [ java.lang.ProcessBuilder@6ecb949e ]: + * Return code [ 1 ] + * + * ==== testSchemaGenNoParms ==== + * Environment: + * OS [ os.name ] [ Windows 10 ] isWindows [ true ] + * WLP Bin [ ../build.image/wlp/bin ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin ] Exists [ true ] + * Script [ ./schemaGen.bat ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin\.\schemaGen.bat ] Exists [ true ] + * Output [ schemaGenOutput.xsd ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin\schemaGenOutput.xsd ] Exists [ false ] + * Running [ java.lang.ProcessBuilder@ba88d78a ]: + * Return code [ 1 ] + * + * ==== testSchemaGenHelp ==== + * Environment: + * OS [ os.name ] [ Windows 10 ] isWindows [ true ] + * WLP Bin [ ../build.image/wlp/bin ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin ] Exists [ true ] + * Script [ ./schemaGen.bat ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin\.\schemaGen.bat ] Exists [ true ] + * Output [ schemaGenOutput.xsd ] [ C:\dev\repos-pub\ol\dev\com.ibm.ws.config.schemagen\..\build.image\wlp\bin\schemaGenOutput.xsd ] Exists [ false ] + * Running [ java.lang.ProcessBuilder@11f5c3bf ]: + * Return code [ 1 ] + */ + public static boolean isWindows() { String os = System.getProperty("os.name"); if (os.startsWith("Win")) { @@ -46,194 +65,224 @@ public static boolean isWindows() { return false; } - /** - * Test that when no parameters are passed, only basic usage info is displayed - * @throws IOException - */ - @Test - public void testSchemaGenNoParms() throws IOException { - System.out.println("==== testSchemaGenNoParms ===="); + public static final boolean IS_WINDOWS = isWindows(); + + public static final String WLP_BIN_DIR_WIN = "..\\build.image\\wlp\\bin"; + public static final String WLP_BIN_DIR_UNIX = "../build.image/wlp/bin"; + public static final String WLP_BIN_DIR = (IS_WINDOWS ? WLP_BIN_DIR_WIN : WLP_BIN_DIR_UNIX); + public static File WLP_BIN = new File(WLP_BIN_DIR); + + public static final String SCHEMAGEN_SCRIPT_WIN = ".\\schemaGen.bat"; + public static final String SCHEMAGEN_SCRIPT_UNIX = "./schemaGen"; + public static final String SCHEMAGEN_SCRIPT = (IS_WINDOWS ? SCHEMAGEN_SCRIPT_WIN : SCHEMAGEN_SCRIPT_UNIX); + + public static final File SCHEMAGEN = new File(WLP_BIN, SCHEMAGEN_SCRIPT); + + public static final String HELP_OPTION = "-help"; + + public static final String OUTPUT_FILE = "schemaGenOutput.xsd"; + public static final File OUTPUT = new File(WLP_BIN, OUTPUT_FILE); + public static final String OUTPUT_PATH = OUTPUT.getAbsolutePath(); + + public static final int MAX_OUTPUT_LINES = 500; // Reasonable limit to output to standard output by schemaGen + public static final long TIMEOUT = 30_000_000_000L; // 30-second timeout + + public static void displayEnv() { + System.out.println("Environment:"); + System.out.println("OS [ os.name ] [ " + System.getProperty("os.name") + " ] isWindows [ " + IS_WINDOWS + " ]"); + System.out.println("WLP Bin [ " + WLP_BIN_DIR + " ] [ " + WLP_BIN.getAbsolutePath() + " ] Exists [ " + WLP_BIN.exists() + " ]"); + System.out.println(" Script [ " + SCHEMAGEN_SCRIPT + " ] [ " + SCHEMAGEN.getAbsolutePath() + " ] Exists [ " + SCHEMAGEN.exists() + " ]"); + System.out.println(" Output [ " + OUTPUT_FILE + " ] [ " + OUTPUT_PATH + " ] Exists [ " + OUTPUT.exists() + " ]"); + } + + public static ProcessBuilder schemaGen() { + return schemaGen(null); + } + + public static ProcessBuilder schemaGen(String option) { + ProcessBuilder pb; + if (IS_WINDOWS) { + if ( option == null ) { + pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_SCRIPT_WIN); + } else { + pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_SCRIPT_WIN, option); + } + } else { + if ( option == null ) { + pb = new ProcessBuilder(SCHEMAGEN_SCRIPT_UNIX); + } else { + pb = new ProcessBuilder(SCHEMAGEN_SCRIPT_UNIX, option); + } + } - ProcessBuilder pb; + pb.directory(WLP_BIN); + pb.redirectErrorStream(true); + + return pb; + } + + public static interface ProcessActor { + int process(String line); + int getHits(); + int getLines(); + } + + public static int apply(ProcessBuilder pBuild, ProcessActor pAct) throws Exception { + System.out.println("Running [ " + pBuild + " ]:"); + + long startTime = System.nanoTime(); + int lines = 0; + Process p = null; - try { - - if (IS_WINDOWS) { - pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_BAT); - } else { - pb = new ProcessBuilder(SCHEMAGEN_LINUX_SCRIPT); - } - - File dir = new File(WLP_BIN_DIR); - pb.directory(dir); - p = pb.start(); + p = pBuild.start(); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line; - - boolean usageAppears = false; - boolean encodingAppears = false; - - long startTime = System.nanoTime(); - int lineCounter = 0; - while ((line = br.readLine()) != null) { + String line; + while (((line = br.readLine()) != null) || p.isAlive()) { + if ( line == null ) { + continue; + } + lines++; System.out.println(line); - - if (line.indexOf("Usage") != -1) { - usageAppears = true; - } - - if (line.indexOf("--encoding") != -1) { - encodingAppears = true; - } - // Exit loop if we are getting hung - if (lineCounter++ > MAX_OUTPUT_LINES) { - System.out.println("schemaGen usage info exceeded [ " + MAX_OUTPUT_LINES + " ] lines"); - break; + if (lines > MAX_OUTPUT_LINES) { + throw new Exception("Exceeded maximum output [ " + MAX_OUTPUT_LINES + " ] (lines)"); } else if (System.nanoTime() - startTime > TIMEOUT) { - System.out.println("SchemaGen exceeded [ " + TIMEOUT + " ] ns when displaying usage Info"); - break; + throw new Exception("Exceeded maximum process time [ " + TIMEOUT + " ] (ns)"); } + + pAct.process(line); } - - assertTrue("'Usage' should appear in command output", usageAppears); - assertFalse("'--encoding' should NOT appear in command output when no arguments passed.", encodingAppears); - System.out.println("PASSED"); - + } finally { - if ( p!= null) { + if (p != null) { p.destroy(); } } + + int rc = ((p == null) ? -1 : p.exitValue()); + System.out.println("Return code [ " + rc + " ]"); + return rc; } - + + public static class TagActor implements ProcessActor { + private final Map tagCounts; + private int numLines; + private int numHits; + + public TagActor(String... tags) { + Map useCounts = new HashMap<>(tags.length); + + for ( String tag : tags ) { + useCounts.put(tag, new int[] { 0 }); + } + + this.tagCounts = useCounts; + } + + public int process(String line) { + numLines++; + + int hits = 0; + for ( String key : tagCounts.keySet() ) { + if ( line.indexOf(key) != -1 ) { + (tagCounts.get(key))[0]++; + hits++; + } + } + + numHits += hits; + + return hits; + } + + public int getCount(String tag) { + int[] counts = tagCounts.get(tag); + return ((counts == null) ? 0 : counts[0]); + } + + @Override + public int getHits() { + return numHits; + } + + @Override + public int getLines() { + return numLines; + } + } + /** - * Test that when -help parameter is passed that help and usage information is displayed. - * @throws IOException + * Test that when no parameters are passed, only basic usage info is displayed. */ @Test - public void testSchemaGenHelp() throws IOException { - System.out.println("==== testSchemaGenHelp ===="); + public void testSchemaGenNoParms() throws Exception { + System.out.println("==== testSchemaGenNoParms ===="); + displayEnv(); + + TagActor pActor = new TagActor("Usage", "--encoding"); + apply( schemaGen(), pActor ); - ProcessBuilder pb; - Process p = null; - try { + int usageCount = pActor.getCount("Usage"); + int encodingCount = pActor.getCount("--encoding"); - if (IS_WINDOWS) { - pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_BAT, HELP_OPTION); - } else { - pb = new ProcessBuilder(SCHEMAGEN_LINUX_SCRIPT, HELP_OPTION); - } + assertTrue("'Usage' should appear", (usageCount > 0)); + assertTrue("'--encoding' should NOT appear", (encodingCount == 0)); - File dir = new File(WLP_BIN_DIR); - pb.directory(dir); - p = pb.start(); + System.out.println("PASSED"); + } + + /** + * Test that when help parameters are passed, help info is displayed. + */ + @Test + public void testSchemaGenHelp() throws Exception { + System.out.println("==== testSchemaGenHelp ===="); + displayEnv(); + + TagActor pActor = new TagActor("Usage", "--encoding"); + apply( schemaGen(HELP_OPTION), pActor ); - BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line; - - boolean usageAppears = false; - boolean encodingAppears = false; + int usageCount = pActor.getCount("Usage"); + int encodingCount = pActor.getCount("--encoding"); - long startTime = System.nanoTime(); - int lineCounter = 0; - - while ((line = br.readLine()) != null) { - System.out.println(line); - - if (line.indexOf("Usage") != -1) { - usageAppears = true; - } - - if (line.indexOf("--encoding") != -1) { - encodingAppears = true; - } + assertTrue("'Usage' should appear", (usageCount > 0)); + assertTrue("'--encoding' should appear", (encodingCount > 0)); - // Exit loop if we are getting hung - if (lineCounter++ > MAX_OUTPUT_LINES) { - System.out.println("schemaGen help exceeded [ " + MAX_OUTPUT_LINES + " ] lines"); - break; - } else if (System.nanoTime() - startTime > TIMEOUT) { - System.out.println("schemaGen exceeded [ " + TIMEOUT + " ] ns when displaying help"); - break; - } - } - - assertTrue("'Usage' should appear in command output", usageAppears); - assertTrue("'--encoding' should appear in command output when " + HELP_OPTION + " is passed.", encodingAppears); - System.out.println("PASSED"); - - } finally { - if ( p!= null) { - p.destroy(); - } - } + System.out.println("PASSED"); } /** * Test that when an output file is specified as parameter that the output file is created, * and that CWWKG0109I "success" message is created. - * - * @throws IOException */ @Test - public void testSchemaGenOutput() throws IOException { + public void testSchemaGenOutput() throws Exception { System.out.println("==== testSchemaGenOutput ===="); + displayEnv(); - ProcessBuilder pb; - Process p = null; - try { - if (IS_WINDOWS) { - pb = new ProcessBuilder("cmd", "/c", SCHEMAGEN_BAT, OUTPUT_FILE); - } else { - pb = new ProcessBuilder(SCHEMAGEN_LINUX_SCRIPT, OUTPUT_FILE); - } - - File dir = new File(WLP_BIN_DIR); - pb.directory(dir); - p = pb.start(); + OUTPUT.delete(); + assertFalse("Output [" + OUTPUT_PATH + "] does not exist", OUTPUT.exists()); - BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line; - - boolean successMsgAppears = false; - - long startTime = System.nanoTime(); - int lineCounter = 0; - - while ((line = br.readLine()) != null) { - System.out.println(line); - - if (line.indexOf("CWWKG0109I") != -1) { - successMsgAppears = true; - } - - // Exit loop if we are getting hung - if (lineCounter++ > MAX_OUTPUT_LINES) { - System.out.println("schemaGen help exceeded [ " + MAX_OUTPUT_LINES + " ] lines"); - break; - } else if (System.nanoTime() - startTime > TIMEOUT) { - System.out.println("schemaGen exceeded [ " + TIMEOUT + " ] ns when displaying help"); - break; - } - } - - assertTrue("'CWWKG0109I' should appear in command output", successMsgAppears); - assertTrue("Should only output 1 message in successful case.", lineCounter==1); + try { + TagActor pActor = new TagActor("CWWKG0109I"); - // It should, however, generate the output file. - File outputFile = new File(WLP_BIN_DIR + "/" + OUTPUT_FILE); - assertTrue("File [" + outputFile.getName() + "] should exist.", outputFile.exists()); - outputFile.delete(); // clean up - System.out.println("PASSED"); + apply( schemaGen(OUTPUT_FILE), pActor); + + int msgCount = pActor.getCount("CWWKG0109I"); + int numLines = pActor.getLines(); + + assertTrue("'CWWKG0109I' should appear once", (msgCount == 1)); + assertTrue("Should have exactly one output line", (numLines == 1)); + assertTrue("Output [" + OUTPUT_PATH + "] exists", OUTPUT.exists()); } finally { - if ( p!= null) { - p.destroy(); - } + OUTPUT.delete(); + assertFalse("Output [" + OUTPUT_PATH + "] does not exist", OUTPUT.exists()); } + + System.out.println("PASSED"); } }