Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbridgha committed Sep 20, 2024
1 parent d940e40 commit 69a529c
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,23 @@
*******************************************************************************/
package net.wasdev.wlp.test.feature.it;

import java.io.BufferedReader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import io.openliberty.tools.common.plugins.util.InstallFeatureUtil;

import static org.junit.Assert.assertEquals;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

import org.apache.commons.io.input.ReversedLinesFileReader;
import org.junit.Before;

import io.openliberty.tools.common.plugins.util.InstallFeatureUtil;

public class BaseInstallFeature {

static File logFile;
static File logErrorFile;

protected File[] features;
protected File[] features;

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -69,7 +63,10 @@ protected boolean existsInFeaturesDirectory(String feature) {
boolean found = false;
for (File file : features) {
if ((file.getName().equals("com.ibm.websphere.appserver." + feature + ".mf")) ||
(file.getName().equals("io.openliberty.versionless." + feature + ".mf"))) {
(file.getName().equals("io.openliberty.versionless." + feature + ".mf")) ||
(file.getName().equals("io.openliberty.jakarta." + feature + ".mf")) ||
(file.getName().equals("io.openliberty." + feature + ".mf")))
{
found = true;
break;
}
Expand All @@ -82,130 +79,22 @@ protected String getFeatureInfo() throws Exception {
return InstallFeatureUtil.productInfo(installDirectory, "featureInfo");
}

/**
* Count number of lines that contain the given string
*/
protected static int countOccurrences(String str, File file) throws FileNotFoundException, IOException {
int occurrences = 0;
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
try {
while (line != null) {
if (line.contains(str)) {
occurrences++;
}
line = br.readLine();
}
} finally {
br.close();
}
return occurrences;
}

protected static boolean readFile(String str, File file) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
try {
while (line != null) {
if (line.contains(str)) {
return true;
}
line = br.readLine();
}
} finally {
br.close();
}
return false;
}

protected static boolean verifyLogMessageDoesNotExist(String message, int timeout)
throws InterruptedException, FileNotFoundException, IOException {
return verifyLogMessageDoesNotExist(message, timeout, logFile);
}

protected static boolean verifyLogMessageDoesNotExist(String message, int timeout, File log)
throws InterruptedException, FileNotFoundException, IOException {
int waited = 0;
int sleep = 10;
while (waited <= timeout) {
Thread.sleep(sleep);
waited += sleep;
if (countOccurrences(message, log) > 0) {
return false;
}
}
return true;
}

protected static boolean verifyLogMessageExists(String message, int timeout)
throws InterruptedException, FileNotFoundException, IOException {
return verifyLogMessageExists(message, timeout, logFile);
}

protected static boolean verifyLogMessageExists(String message, int timeout, File log)
throws InterruptedException, FileNotFoundException, IOException {
int waited = 0;
int sleep = 10;
while (waited <= timeout) {
Thread.sleep(sleep);
waited += sleep;
if (readFile(message, log)) {
return true;
}
}
return false;
}

protected static boolean verifyLogMessageExists(String message, int timeout, File log, int occurrences)
throws InterruptedException, FileNotFoundException, IOException {
int waited = 0;
int sleep = 10;
while (waited <= timeout) {
Thread.sleep(sleep);
waited += sleep;
if (countOccurrences(message, log) == occurrences) {
return true;
}
}
return false;
}

protected static boolean verifyLogMessageExists(String message, int timeout, int occurrences)
throws InterruptedException, FileNotFoundException, IOException {
return verifyLogMessageExists(message, timeout, logFile, occurrences);
}

protected static String getLogTail() throws IOException {
return getLogTail(logFile);
}

protected static String getLogTail(File log) throws IOException {
int numLines = 100;
ReversedLinesFileReader object = null;
try {
object = new ReversedLinesFileReader(log, StandardCharsets.UTF_8);
List<String> reversedLines = new ArrayList<String>();
public boolean buildLogCheck(String msg) throws Exception {
File buildLog = new File("../build.log");
assertTrue(buildLog.exists());

for (int i = 0; i < numLines; i++) {
String line = object.readLine();
if (line == null) {
break;
try (InputStream buildOutput = new FileInputStream(buildLog); InputStreamReader in = new InputStreamReader(buildOutput); Scanner s = new Scanner(in);) {
while (s.hasNextLine()) {
String line = s.nextLine();
if(line.contains(msg)) {
return true;
}
}
reversedLines.add(line);
}
StringBuilder result = new StringBuilder();
for (int i = reversedLines.size() - 1; i >=0; i--) {
result.append(reversedLines.get(i) + "\n");
}
return "Last "+numLines+" lines of log at "+log.getAbsolutePath()+":\n" +
"===================== START =======================\n" +
result.toString() +
"====================== END ========================\n";
} finally {
if (object != null) {
object.close();
}
}
}
} catch (Exception e) {
System.out.println("Error checking build.log " + e.getMessage());
}

return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# The expected result of the build, possible values are "success" (default) and "failure"
# can be indexed
invoker.buildResult = failure
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.openliberty.tools.it</groupId>
<artifactId>kernel-install-feature-tests</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>install-features-versionless-bad-platform-server-it</artifactId>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>@pom.version@</version>
<configuration>
<assemblyArtifact>
<groupId>${runtimeGroupId}</groupId>
<artifactId>${runtimeKernelId}</artifactId>
<version>${runtimeVersion}</version>
<type>zip</type>
</assemblyArtifact>
<serverName>test</serverName>
<serverXmlFile>src/test/resources/server.xml</serverXmlFile>
</configuration>
<executions>
<execution>
<id>install-liberty-server</id>
<phase>compile</phase>
<goals>
<goal>install-server</goal>
</goals>
</execution>
<execution>
<id>create-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>install-server-features</id>
<phase>pre-integration-test</phase>
<goals>
<goal>install-feature</goal>
</goals>
<configuration>
<features>
<acceptLicense>true</acceptLicense>
</features>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* (c) Copyright IBM Corporation 2018.
* (c) Copyright IBM Corporation 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@
import org.junit.Test;
import java.io.File;

public class InstallFeaturesVersionlessServerTest extends BaseInstallFeature {
public class InstallFeaturesVersionlessBadPlatformServerTest extends BaseInstallFeature {

@Test
public void testInstalledFeatures() throws Exception {
Expand All @@ -32,7 +32,7 @@ public void testInstalledFeatures() throws Exception {
assertNotInstalled("couchdb-1.0");
assertNotInstalled("distributedMap-1.0");

assertTrue(getLogTail(), verifyLogMessageExists("CWWKF1516E: The platform could not be determined. The following versionless features cannot be installed: [servlet]", 30000));
assertTrue(buildLogCheck("CWWKF1515E: The badname platform could not be found."));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<server description="default server">
<featureManager>
<feature>appSecurityClient-1.0</feature>
<feature>servlet</feature>
<platform>badname</platform>
</featureManager>
</server>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* (c) Copyright IBM Corporation 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package net.wasdev.wlp.test.feature.it;

import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;

public class InstallFeaturesVersionlessNoPlatformServerTest extends BaseInstallFeature {

@Test
public void testInstalledFeatures() throws Exception {


assertInstalled("appSecurityClient-1.0");
assertNotInstalled("servlet");
assertNotInstalled("servlet-4.0");
assertNotInstalled("beanValidation-2.0");
assertNotInstalled("couchdb-1.0");
assertNotInstalled("distributedMap-1.0");

assertTrue(buildLogCheck("CWWKF1516E: The platform could not be determined. The following versionless features cannot be installed: [servlet]"));
}

}
Loading

0 comments on commit 69a529c

Please sign in to comment.