Commit 8ed50148 authored by Ate Douma's avatar Ate Douma

TRIVIAL cleanup master

parent 27eba2c0
*.iml
.idea/
.settings/
target/
*.log
This diff is collapsed.
Hippo CMS Test Utils
Copyright 2017-2018 Hippo B.V. (http://www.onehippo.com)
This product includes software developed by:
Hippo B.V., Amsterdam, The Netherlands (http://www.onehippo.com/);
The Apache Software Foundation (http://www.apache.org/).
NOTICE: Only our own original work is licensed under the terms of the
Apache License Version 2.0. The licenses of some libraries might impose
different redistribution or general licensing terms than those stated in the
Apache License. Users and redistributors are hereby requested to verify these
conditions and agree upon them.
# This is not the branch you're looking for...
BloomReach only provides the git trees for the release tags of Hippo CMS, as explained on https://www.onehippo.org/about/open-source-release-policy.html
To checkout the code for a specific release tag, after cloning this repository, use the following:
## to show the available tags
git tag
## to checkout a specific tag
git checkout <tag name>
## to modify a project
If you want to make modifications to a project, for example to create a patch, create a new fork branch from the specific tag like this:
git checkout -b forked-<tag name> <tag name>
For the latter, also see the **Build from Source** documentation at https://www.onehippo.org/library/development/build-hippo-cms-from-scratch.html
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2017-2018 Hippo B.V. (http://www.onehippo.com)
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.
-->
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-cms7-project</artifactId>
<version>29.4-SNAPSHOT</version>
</parent>
<name>Hippo CMS Test Utils</name>
<description>Hippo CMS Test Utils</description>
<packaging>jar</packaging>
<artifactId>hippo-cms-testutils</artifactId>
<version>1.3.0-SNAPSHOT</version>
<inceptionYear>2017</inceptionYear>
<properties>
<commons-lang3.version>3.5</commons-lang3.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<scm>
<connection>scm:git:https://code.onehippo.org/cms-community/hippo-cms-testutils.git</connection>
<developerConnection>scm:git:git@code.onehippo.org:cms-community/hippo-cms-testutils.git</developerConnection>
<url>https://code.onehippo.org/cms-community/hippo-cms-testutils</url>
</scm>
<repositories>
<repository>
<id>hippo</id>
<name>Hippo Maven 2</name>
<url>https://maven.onehippo.com/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</repository>
</repositories>
</project>
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.testutils.jcr.event;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventListener;
import static org.junit.Assert.fail;
public class EventCollector {
private final String MAGIC_NODE_NAME = "magic-node-name";
private Session session;
private Node root;
private EventListener eventListener;
private List<EventPojo> collectedEvents = new ArrayList<>();
private boolean stopped = false;
public EventCollector(final Session session, final Node root) {
this.session = session;
this.root = root;
}
public void start() throws RepositoryException {
eventListener = (events) -> {
try {
while (events.hasNext()) {
final Event event = events.nextEvent();
if (event.getPath().contains(MAGIC_NODE_NAME)) {
stopped = true;
} else {
collectedEvents.add(EventPojo.from(event));
}
}
} catch (RepositoryException e) {
fail("Unexpected exception: " + e);
}
};
session.getWorkspace().getObservationManager().addEventListener(
eventListener,
EventUtils.ALL_EVENTS,
root.getPath(),
true,
null,
null,
false);
}
public List<EventPojo> stop() throws RepositoryException {
root.addNode(MAGIC_NODE_NAME);
session.save();
int waitMs = 10;
while (!stopped && waitMs < 1000) {
try {
Thread.sleep(waitMs);
} catch (InterruptedException e) {
fail("Unexpected exception: " + e);
}
waitMs *= 2;
}
if (!stopped) {
fail("Event listener did not receive magic event");
}
root.getNode(MAGIC_NODE_NAME).remove();
session.save();
session.getWorkspace().getObservationManager().removeEventListener(eventListener);
return collectedEvents;
}
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.testutils.jcr.event;
import javax.jcr.RepositoryException;
import javax.jcr.observation.Event;
import org.apache.commons.lang3.StringUtils;
public class EventPojo {
private final int eventType;
private final String path;
private final String srcChildRelPath;
private final String destChildRelPath;
EventPojo(final int eventType, final String path) {
this(eventType, path, null ,null);
}
EventPojo(final int eventType, final String path, final String srcChildRelPath, final String destChildRelPath) {
this.eventType = eventType;
this.path = path;
this.srcChildRelPath = srcChildRelPath;
this.destChildRelPath = destChildRelPath;
}
static EventPojo from(Event jcrEvent) throws RepositoryException {
return new EventPojo(
jcrEvent.getType(),
jcrEvent.getPath(),
(String) jcrEvent.getInfo().get("srcChildRelPath"),
(String) jcrEvent.getInfo().get("destChildRelPath"));
}
public int getEventType() {
return eventType;
}
public String getPath() {
return path;
}
public String getSrcChildRelPath() {
return srcChildRelPath;
}
public String getDestChildRelPath() {
return destChildRelPath;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof EventPojo)) return false;
final EventPojo other = (EventPojo) object;
return eventType == other.eventType
&& StringUtils.equals(path, other.path)
&& StringUtils.equals(srcChildRelPath, other.srcChildRelPath)
&& StringUtils.equals(destChildRelPath, other.destChildRelPath);
}
@Override
public String toString() {
String string = "Event: path=" + path;
switch (eventType) {
case Event.NODE_ADDED:
string += " type:NODE_ADDED";
break;
case Event.NODE_MOVED:
string += " type:NODE_MOVED";
break;
case Event.NODE_REMOVED:
string += " type:NODE_REMOVED";
break;
case Event.PROPERTY_ADDED:
string += " type:PROPERTY_ADDED";
break;
case Event.PROPERTY_CHANGED:
string += " type:PROPERTY_CHANGED";
break;
case Event.PROPERTY_REMOVED:
string += " type:PROPERTY_REMOVED";
break;
}
if (srcChildRelPath != null) {
string += " srcChildRelPath:" + srcChildRelPath;
}
if (destChildRelPath != null) {
string += " destChildRelPath:" + destChildRelPath;
}
return string;
}
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.testutils.jcr.event;
import static javax.jcr.observation.Event.NODE_ADDED;
import static javax.jcr.observation.Event.NODE_MOVED;
import static javax.jcr.observation.Event.NODE_REMOVED;
import static javax.jcr.observation.Event.PERSIST;
import static javax.jcr.observation.Event.PROPERTY_ADDED;
import static javax.jcr.observation.Event.PROPERTY_CHANGED;
import static javax.jcr.observation.Event.PROPERTY_REMOVED;
public class EventUtils {
public static final int ALL_EVENTS = NODE_ADDED | NODE_REMOVED | NODE_MOVED
| PROPERTY_ADDED | PROPERTY_CHANGED | PROPERTY_REMOVED | PERSIST;
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.testutils.jcr.event;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.observation.Event;
import static org.junit.Assert.fail;
public class ExpectedEvents {
private final List<EventPojo> expectedEvents = new ArrayList<>();
public ExpectedEvents expectNodeAdded(final String path) throws RepositoryException {
expectedEvents.add(new EventPojo(Event.NODE_ADDED, path));
return this;
}
public ExpectedEvents expectNodeAdded(final String path, final String additionalProperty) throws RepositoryException {
expectedEvents.add(new EventPojo(Event.NODE_ADDED, path));
return expectPropertyAdded(path + "/" + additionalProperty);
}
public ExpectedEvents expectNodeReordered(final String parent, final String srcChildRelPath, final String destChildRelPath) throws RepositoryException {
expectedEvents.add(new EventPojo(Event.NODE_MOVED, parent, srcChildRelPath, destChildRelPath));
return this;
}
public ExpectedEvents expectNodeRemoved(final String path) throws RepositoryException {
expectedEvents.add(new EventPojo(Event.NODE_REMOVED, path));
return this;
}
public ExpectedEvents expectPropertyAdded(final String path) throws RepositoryException {
expectedEvents.add(new EventPojo(Event.PROPERTY_ADDED, path));
return this;
}
public ExpectedEvents expectPropertyChanged(final String path) throws RepositoryException {
expectedEvents.add(new EventPojo(Event.PROPERTY_CHANGED, path));
return this;
}
public ExpectedEvents expectPropertyRemoved(final String path) throws RepositoryException {
expectedEvents.add(new EventPojo(Event.PROPERTY_REMOVED, path));
return this;
}
public void check(final List<EventPojo> actualEvents) throws RepositoryException {
final StringBuilder message = new StringBuilder();
final List<EventPojo> unseenEvents = new ArrayList<>(expectedEvents.size());
unseenEvents.addAll(expectedEvents);
for (EventPojo event : actualEvents) {
if (unseenEvents.contains(event)) {
unseenEvents.remove(event);
} else {
message.append("unexpected event: ").append(event.toString()).append("\n");
}
}
for (EventPojo unseen : unseenEvents) {
message.append("did not see the following event: ").append(unseen.toString()).append("\n");
}
if (message.length() > 0) {
fail(message.toString());
}
}
}
/*
* Copyright 2013-2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.testutils.log4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.filter.AbstractFilter;
/**
* This is a very simple filter based on string matching.
*
* <p>The filter reads lines from a called <code>log4j-filters.txt</code>
* that is expected to be on the classpath. Each line that does not start with # represents a string to
* to match the log message to filter against. If the log message contains one
* of the configured strings it is filtered out. Lines may also be regular expressions.
* Lines that start with # are treated as comments and ignored</p>
* <p>
* Note: this class originally was provided through the hippo-repository-testutils module,
* org.onehippo.repository.testutils.log4j.StringMatchFilter which was log4j1 based.
* </p>
* <p>
* For log4j2 the class needed to be rewritten as log4j2 plugin, and now is more conveniently provided
* through the hippo-cms-testutils module (which hippo-repository-testutils depends upon).
* </p>
*/
@Plugin(name = "StringMatchFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
public class StringMatchFilter extends AbstractFilter {
private final List<String> stringsToMatch = new ArrayList<String>();
private final List<Pattern> patternsToMatch = new ArrayList<>();
public StringMatchFilter() {
super(Result.DENY, Result.NEUTRAL);
final InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("log4j-filters.txt");
if (resourceAsStream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceAsStream))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.isEmpty()) {
continue;
}
if (line.startsWith("#")) {
continue;
}
stringsToMatch.add(line);
patternsToMatch.add(Pattern.compile(line));
}
} catch (PatternSyntaxException ignored) {
} catch (IOException e) {
System.err.println("Error while initializing log4j StringMatchFilter: " + e);
}
} else {
System.err.println("StringMatchFilter: Could not find log4j-filters.txt");
}
}
@Override
public Result filter(final LogEvent event) {
String msg = event.getMessage().getFormattedMessage();
if(msg == null || stringsToMatch.isEmpty()) {
return Result.NEUTRAL;
}
if (matches(msg)) {
return Result.DENY;
}
return Result.NEUTRAL;
}
private boolean matches(final String msg) {
for (String s : stringsToMatch) {
if (msg.contains(s)) {
return true;
}
}
for (Pattern pattern : patternsToMatch) {
if (pattern.matcher(msg).matches()) {
return true;
}
}
return false;
}
@PluginFactory
public static StringMatchFilter createFilter() {
return new StringMatchFilter();
}
}
/*
* Copyright 2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.testutils.log4j;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Log4jInterceptorTest {
static final Logger log = LoggerFactory.getLogger(Log4jInterceptorTest.class);
static final Logger logPackage = LoggerFactory.getLogger(Log4jInterceptorTest.class.getPackage().getName());
static final Logger logOrgTestDebug = LoggerFactory.getLogger("org.test.debug");
static final Logger logOrgTestInfo = LoggerFactory.getLogger("org.test.info");
static final Logger logComTestInfo = LoggerFactory.getLogger("com.test.info");
@Test
public void testCanTrapDebugMessageEvenIfLogLevelIsInfo() {
try (Log4jInterceptor interceptor = Log4jInterceptor.onDebug().trap().build()) {
// log level is on info however the following debug message still will be trapped!
log.debug("Log4jInterceptorTest");
Assert.assertEquals(1, interceptor.getEvents().size());
Assert.assertTrue(interceptor.messages().anyMatch(m->m.equals("Log4jInterceptorTest")));
}
}
@Test
public void testWontTrapDebugMessageOnInfoLevel() {
try (Log4jInterceptor interceptor = Log4jInterceptor.onInfo().trap().build()) {
log.debug("Log4jInterceptorTest");
Assert.assertTrue(interceptor.getEvents().isEmpty());
}
}
@Test
public void testDenyAll() {
try (Log4jInterceptor interceptor = Log4jInterceptor.onAll().deny().build()) {
log.debug("Log4jInterceptorTest");
logPackage.error("org.onehippo.testutils.log4j");
logOrgTestDebug.debug("org.test.debug");
logOrgTestInfo.info("org.test.info");
logComTestInfo.info("com.test.info");
Assert.assertTrue(interceptor.getEvents().isEmpty());
}
}
@Test
public void testTrapAll() {
try (Log4jInterceptor interceptor = Log4jInterceptor.onAll().trap().build()) {
log.debug("Log4jInterceptorTest");
logPackage.error("org.onehippo.testutils.log4j");
logOrgTestDebug.debug("org.test.debug");
logComTestInfo.info("com.test.info");
logOrgTestInfo.info("org.test.info");
Assert.assertEquals(5, interceptor.getEvents().size());
}
}
@Test
public void testTrapAllDenied() {
try (Log4jInterceptor interceptor = Log4jInterceptor.onAll().deny().trap().build()) {
log.debug("Log4jInterceptorTest");
logPackage.error("org.onehippo.testutils.log4j");
logOrgTestDebug.debug("org.test.debug");
logComTestInfo.info("com.test.info");
Assert.assertEquals(4, interceptor.getEvents().size());
}
}
@Test
public void testTrapSomeDenyAll() {
try (Log4jInterceptor interceptor = Log4jInterceptor.onAll().trap("org.onehippo").deny("org.test", "com.test").build()) {
log.debug("Log4jInterceptorTest");
logPackage.debug("org.onehippo.testutils.log4j");
logOrgTestDebug.debug("org.test.debug");
logOrgTestInfo.info("org.test.info");
logComTestInfo.info("com.test.info");
Assert.assertEquals(2, interceptor.getEvents().size());
List<String> messages = interceptor.messages().collect(Collectors.toList());
Assert.assertTrue(messages.contains("Log4jInterceptorTest") && messages.contains("org.onehippo.testutils.log4j"));
}
}
@Test
public void testTrapLoggerHierarchy() {
try (Log4jInterceptor interceptor = Log4jInterceptor.onAll().trap("org.onehippo").build()) {
log.debug("Log4jInterceptorTest");
logPackage.error("org.onehippo.testutils.log4j");
Assert.assertEquals(2, interceptor.getEvents().size());
List<String> messages = interceptor.messages().collect(Collectors.toList());
Assert.assertTrue(messages.contains("Log4jInterceptorTest") && messages.contains("org.onehippo.testutils.log4j"));
}
}
}
/*
* Copyright 2012-2017 Hippo B.V. (http://www.onehippo.com)
*
* 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 org.onehippo.testutils.log4j;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;