diff --git a/smack-core/build.gradle b/smack-core/build.gradle index 5db1a5d5b..e7f4c43ff 100644 --- a/smack-core/build.gradle +++ b/smack-core/build.gradle @@ -16,6 +16,7 @@ dependencies { testCompile 'org.powermock:powermock-module-junit4:1.5.5' testCompile 'org.powermock:powermock-api-mockito:1.5.5' testCompile 'com.jamesmurty.utils:java-xmlbuilder:0.6' + testCompile 'net.iharder:base64:2.3.8' } task compressionJar(type: Jar) { diff --git a/smack-core/src/test/java/org/jivesoftware/smack/test/util/SmackTestSuite.java b/smack-core/src/test/java/org/jivesoftware/smack/test/util/SmackTestSuite.java new file mode 100644 index 000000000..a7d1144d1 --- /dev/null +++ b/smack-core/src/test/java/org/jivesoftware/smack/test/util/SmackTestSuite.java @@ -0,0 +1,97 @@ +/** + * + * Copyright © 2014 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.io.IOException; +import java.io.UnsupportedEncodingException; + +import net.iharder.Base64; + +import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smack.util.stringencoder.Base64.Encoder; +import org.junit.runners.Suite; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.RunnerBuilder; + +/** + * The SmackTestSuite takes care of initializing Smack for the unit tests. For example the Base64 + * encoder is configured. + */ +public class SmackTestSuite extends Suite { + + public SmackTestSuite(Class klass, RunnerBuilder builder) throws InitializationError { + super(klass, builder); + + init(); + } + + public static void init() { + org.jivesoftware.smack.util.stringencoder.Base64.setEncoder(new Encoder() { + + @Override + public byte[] decode(String string) { + try { + return Base64.decode(string); + } + catch (IllegalArgumentException e) { + // Expected by e.g. the unit test. + // " Base64-encoded string must have at least four characters, but length specified was 1", + // should not cause an exception, but instead null should be returned. Maybe + // this should be changed in a later Smack release, so that the actual exception + // is handled. + return null; + } + catch (IOException e) { + throw new IllegalStateException(e); + } + } + + @Override + public byte[] decode(byte[] input, int offset, int len) { + try { + return Base64.decode(input, offset, len, 0); + } + catch (IllegalArgumentException e) { + // Expected by e.g. the unit test. + // " Base64-encoded string must have at least four characters, but length specified was 1", + // should not cause an exception, but instead null should be returned. Maybe + // this should be changed in a later Smack release, so that the actual exception + // is handled. + return null; + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + @Override + public String encodeToString(byte[] input, int offset, int len) { + return Base64.encodeBytes(input, offset, len); + } + + @Override + public byte[] encode(byte[] input, int offset, int len) { + String string = encodeToString(input, offset, len); + try { + return string.getBytes(StringUtils.USASCII); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(e); + } + } + + }); + } +} diff --git a/smack-extensions/build.gradle b/smack-extensions/build.gradle index b926a05f9..67a5a09fd 100644 --- a/smack-extensions/build.gradle +++ b/smack-extensions/build.gradle @@ -8,5 +8,4 @@ Classes and methods that implement support for the various XMPP XEPs dependencies { compile project(':smack-core') testCompile project(':smack-core').sourceSets.test.runtimeClasspath - testCompile project(':smack-java7') } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/VCardUnitTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/VCardUnitTest.java index b82ba6827..3ced33d48 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/VCardUnitTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/VCardUnitTest.java @@ -20,13 +20,20 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; +import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smackx.vcardtemp.packet.VCard; import org.jivesoftware.smackx.vcardtemp.provider.VCardProvider; +import org.junit.Before; import org.junit.Test; public class VCardUnitTest extends InitExtensions { + @Before + public void initSmackTestSuite() { + SmackTestSuite.init(); + } + @Test public void testNoWorkHomeSpecifier_EMAIL() throws Throwable { VCard card = VCardProvider.createVCardFromXML("foo@fee.www.bar"); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/IBBTestsSuite.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/IBBTestsSuite.java index 86b2d366d..2a58b0179 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/IBBTestsSuite.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/IBBTestsSuite.java @@ -16,6 +16,7 @@ */ package org.jivesoftware.smackx.bytestreams.ibb; +import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smackx.bytestreams.ibb.packet.CloseTest; import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtensionTest; import org.jivesoftware.smackx.bytestreams.ibb.packet.DataTest; @@ -24,7 +25,7 @@ import org.jivesoftware.smackx.bytestreams.ibb.provider.OpenIQProviderTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; -@RunWith(Suite.class) +@RunWith(SmackTestSuite.class) @Suite.SuiteClasses( { CloseTest.class, DataPacketExtensionTest.class, DataTest.class, OpenTest.class, OpenIQProviderTest.class, CloseListenerTest.class, DataListenerTest.class, InBandBytestreamManagerTest.class,