Skip to content

Commit

Permalink
Introduce test fixtures
Browse files Browse the repository at this point in the history
This also removes the powermock dependency. Although powermock is a
fine library, it currently prevents dropping Junit4. And since we only
use the Whitebox API of powermock, this simply replaced powermock's
Whitebox with our own.
  • Loading branch information
Flowdalic committed Apr 11, 2020
1 parent 4a99f72 commit b5f9d4d
Show file tree
Hide file tree
Showing 51 changed files with 123 additions and 80 deletions.
13 changes: 8 additions & 5 deletions build.gradle
Expand Up @@ -24,6 +24,7 @@ apply from: 'version.gradle'

allprojects {
apply plugin: 'java-library'
apply plugin: 'java-test-fixtures'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jacoco'
Expand Down Expand Up @@ -130,7 +131,6 @@ allprojects {
':smack-openpgp',
].collect { project(it) }
junitVersion = '5.6.0'
powerMockVersion = '2.0.2'
commonsIoVersion = '2.6'
if (project.hasProperty("useSonatype")) {
useSonatype = project.getProperty("useSonatype").toBoolean()
Expand Down Expand Up @@ -266,10 +266,16 @@ allprojects {
}

dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testFixturesApi "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"

// The smack-extensions subproject uses mockito in its fest
// fixtures, and we want to have mockito also available in
// test, so we use API here.
testFixturesApi "org.mockito:mockito-core:3.3.3"
testImplementation 'com.jamesmurty.utils:java-xmlbuilder:1.2'

errorprone 'com.google.errorprone:error_prone_core:2.3.3'
errorproneJavac('com.google.errorprone:javac:9+181-r4173-1')
}
Expand All @@ -282,9 +288,6 @@ allprojects {
configure (junit4Projects) {
dependencies {
testImplementation "org.junit.vintage:junit-vintage-engine:$junitVersion"
testImplementation "org.powermock:powermock-module-junit4:$powerMockVersion"
testImplementation "org.powermock:powermock-module-junit4-rule:$powerMockVersion"
testImplementation "org.powermock:powermock-api-mockito2:$powerMockVersion"
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/checkstyle/suppressions.xml
Expand Up @@ -9,5 +9,5 @@ it disabled until gradle uses a checkstyle version where this is fixed
<suppress checks="GenericWhitespace"
files="Protocol.java" />
<!-- Suppress JavadocPackage in the test packages -->
<suppress checks="JavadocPackage" files="[\\/]test[\\/]"/>
<suppress checks="JavadocPackage" files="[\\/]test(Fixtures)?[\\/]"/>
</suppressions>
26 changes: 16 additions & 10 deletions smack-core/build.gradle
Expand Up @@ -10,23 +10,29 @@ dependencies {
compile "org.jxmpp:jxmpp-core:$jxmppVersion"
compile "org.jxmpp:jxmpp-jid:$jxmppVersion"
compile "org.minidns:minidns-core:$miniDnsVersion"
testCompile project(':smack-xmlparser-stax')
testCompile project(':smack-xmlparser-xpp3')
testCompile "org.jxmpp:jxmpp-jid:$jxmppVersion:tests"
testCompile "org.xmlunit:xmlunit-core:$xmlUnitVersion"

testFixturesImplementation project(':smack-xmlparser-stax')
testFixturesImplementation project(':smack-xmlparser-xpp3')

// Bouncy Castle is setup by SmackTestSuite. We deliberately use
// 'implementation' here since there is no need to shadow it
// outside of the fixtures compilation classpath. That is, no test
// should ever setup Bouncy Castle as security provider explicitly.
testFixturesImplementation 'org.bouncycastle:bcprov-jdk15on:1.64'
testFixturesImplementation 'org.apache.commons:commons-lang3:3.10'

testFixturesApi "org.jxmpp:jxmpp-jid:$jxmppVersion:tests"
testFixturesApi "org.xmlunit:xmlunit-core:$xmlUnitVersion"
// Explictily add assertj-core which is a dependency of
// xmlunit-assertj, but gradle fails to resolves it with:
// Execution failed for task ':smack-core:compileTestJava'.
// > Could not resolve all files for configuration ':smack-core:testCompileClasspath'.
// > Could not find org.assertj:assertj-core:.
// Required by:
// project :smack-core > org.xmlunit:xmlunit-assertj:2.6.2
testCompile "org.assertj:assertj-core:3.11.1"
testCompile "org.xmlunit:xmlunit-assertj:$xmlUnitVersion"
testCompile 'com.jamesmurty.utils:java-xmlbuilder:1.2'
testCompile 'org.bouncycastle:bcprov-jdk15on:1.64'
testCompile 'com.google.guava:guava:28.2-jre'
testCompile 'org.jgrapht:jgrapht-io:1.3.1'
testFixturesApi "org.assertj:assertj-core:3.11.1"
testFixturesApi "org.xmlunit:xmlunit-assertj:$xmlUnitVersion"
testFixturesApi 'org.hamcrest:hamcrest-library:2.2'
}

class CreateFileTask extends DefaultTask {
Expand Down
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jivesoftware.smack;

import java.io.IOException;
Expand Down
@@ -1,6 +1,6 @@
/**
*
* Copyright © 2014 Florian Schmaus
* Copyright © 2014-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@
package org.jivesoftware.smack.test.util;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

Expand All @@ -40,7 +39,6 @@ protected boolean matchesSafely(CharSequence item) {
return charSequenceString.equals(itemString);
}

@Factory
public static Matcher<CharSequence> equalsCharSequence(CharSequence charSequence) {
return new CharSequenceEquals(charSequence);
}
Expand Down
@@ -1,6 +1,6 @@
/**
*
* Copyright 2019 Florian Schmaus
* Copyright 2019-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,9 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.test.util;

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.lang.ref.PhantomReference;
Expand Down Expand Up @@ -129,7 +129,7 @@ private static void assertReferencesQueueSize(ReferenceQueue<?> referenceQueue,
}

Reference<?> reference = referenceQueue.poll();
assertNull("Reference queue is not empty when it should be", reference);
assertNull(reference, "Reference queue is not empty when it should be");
}

private static void assertReferencesQueueIsEmpty(ReferenceQueue<?> referenceQueue) {
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.util;
package org.jivesoftware.smack.test.util;

import java.io.IOException;
import java.net.BindException;
Expand Down
@@ -0,0 +1,44 @@
/**
*
* Copyright 2020 Florian Schmaus
*
* 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.jivesoftware.smack.test.util;

import java.lang.reflect.Field;

import org.apache.commons.lang3.reflect.FieldUtils;

public class Whitebox {

public static <T> T getInternalState(Object object, String fieldName, Class<T> fieldType) {
Class<?> objectClass = object.getClass();

Field field = FieldUtils.getField(objectClass, fieldName, true);

Object res;
try {
res = field.get(object);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new AssertionError(e);
}

if (!fieldType.isInstance(res)) {
throw new AssertionError(res + " is not a (sub)type of " + fieldType);
}

return fieldType.cast(res);
}

}
1 change: 0 additions & 1 deletion smack-debug-slf4j/build.gradle
Expand Up @@ -6,5 +6,4 @@ Connect your favourite slf4j backend of choice to get output inside of it"""
dependencies {
compile project(':smack-core')
compile 'org.slf4j:slf4j-api:[1.7,1.8)'
testCompile project(path: ":smack-core", configuration: "testRuntime")
}
1 change: 0 additions & 1 deletion smack-debug/build.gradle
Expand Up @@ -4,5 +4,4 @@ Inspect the exchanged XMPP stanzas."""

dependencies {
compile project(':smack-core')
testCompile project(path: ":smack-core", configuration: "testRuntime")
}
4 changes: 2 additions & 2 deletions smack-experimental/build.gradle
Expand Up @@ -7,8 +7,8 @@ between patch versions."""
dependencies {
compile project(':smack-core')
compile project(':smack-extensions')
testCompile project(path: ":smack-core", configuration: "testRuntime")
testCompile project(path: ":smack-extensions", configuration: "testRuntime")

compile "org.hsluv:hsluv:0.2"

testFixturesApi(testFixtures(project(":smack-extensions")))
}
Expand Up @@ -18,12 +18,9 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.security.Security;

import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.StringUtils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -33,10 +30,6 @@
*/
public class HashTest extends SmackTestSuite {

static {
Security.addProvider(new BouncyCastleProvider());
}

private static final String testString = "Hello World!";
private static final String md5sum = "ed076287532e86365e841e92bfc50d8c";
private static final String sha1sum = "2ef7bde608ce5404e97d5f042f95f89f1c232871";
Expand Down
3 changes: 2 additions & 1 deletion smack-extensions/build.gradle
Expand Up @@ -10,5 +10,6 @@ dependencies {
// Some implementations need APIs provided by smack-im,
// e.g. message delivery receipts the roster
compile project(':smack-im')
testCompile project(path: ":smack-core", configuration: "testRuntime")
testFixturesApi(testFixtures(project(":smack-core")))
//testCompile project(path: ":smack-core", configuration: "testRuntime")
}
Expand Up @@ -23,6 +23,7 @@
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.test.util.Whitebox;

import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Close;
Expand All @@ -31,7 +32,6 @@
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.JidTestUtil;
import org.mockito.ArgumentCaptor;
import org.powermock.reflect.Whitebox;

/**
* Test for the CloseListener class.
Expand Down Expand Up @@ -59,7 +59,7 @@ public void shouldReplyErrorIfSessionIsUnknown() throws Exception {
InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);

// get the CloseListener from InBandByteStreamManager
CloseListener closeListener = Whitebox.getInternalState(byteStreamManager,
CloseListener closeListener = Whitebox.getInternalState(byteStreamManager, "closeListener",
CloseListener.class);

Close close = new Close("unknownSessionId");
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.test.util.Whitebox;

import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
Expand All @@ -32,7 +33,6 @@
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.JidTestUtil;
import org.mockito.ArgumentCaptor;
import org.powermock.reflect.Whitebox;

/**
* Test for the CloseListener class.
Expand Down Expand Up @@ -60,7 +60,7 @@ public void shouldReplyErrorIfSessionIsUnknown() throws Exception {
InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);

// get the DataListener from InBandByteStreamManager
DataListener dataListener = Whitebox.getInternalState(byteStreamManager,
DataListener dataListener = Whitebox.getInternalState(byteStreamManager, "dataListener",
DataListener.class);

DataPacketExtension dpe = new DataPacketExtension("unknownSessionID", 0, "Data");
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.jivesoftware.smack.test.util.Whitebox;
import org.jivesoftware.smack.util.stringencoder.Base64;

import org.jivesoftware.smackx.InitExtensions;
Expand All @@ -46,7 +47,6 @@
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.EntityFullJid;
import org.jxmpp.jid.JidTestUtil;
import org.powermock.reflect.Whitebox;

/**
* Test for InBandBytestreamSession.
Expand Down Expand Up @@ -262,7 +262,7 @@ public void shouldSendCloseRequestIfInvalidSequenceReceived() throws Exception {
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
initiatorJID);
InputStream inputStream = session.getInputStream();
StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);

// build invalid packet with out of order sequence
String base64Data = Base64.encode("Data");
Expand Down Expand Up @@ -303,7 +303,7 @@ public void shouldReadAllReceivedData1() throws Exception {
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
initiatorJID);
InputStream inputStream = session.getInputStream();
StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);

// verify data packet and notify listener
for (int i = 0; i < controlData.length / blockSize; i++) {
Expand Down Expand Up @@ -349,7 +349,7 @@ public void shouldReadAllReceivedData2() throws Exception {
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
initiatorJID);
InputStream inputStream = session.getInputStream();
StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);

// verify data packet and notify listener
for (int i = 0; i < controlData.length / blockSize; i++) {
Expand Down

0 comments on commit b5f9d4d

Please sign in to comment.