1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

Init Base64 test encoder for unit-tests

The previous approach with

testCompile project("smack-java7")

was flawed. Instead we setup a Base64 encoder especially for the unit
tests now.
This commit is contained in:
Florian Schmaus 2014-09-07 17:22:55 +02:00
parent 982b49106a
commit 184604bba2
5 changed files with 107 additions and 2 deletions

View file

@ -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) {

View file

@ -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);
}
}
});
}
}

View file

@ -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')
}

View file

@ -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("<vcard><EMAIL><USERID>foo@fee.www.bar</USERID></EMAIL></vcard>");

View file

@ -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,