Skip to content
Permalink
Browse files

Improved: Turn ‘UtilObjectTests’ into a unit test class

(OFBIZ-11067)

Adapt ‘UtilObjectUnitTest’ test class to avoid side effects when
testing allowed serialization classes.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1869022 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 26, 2019
1 parent af2c4b4 commit e5933c677bcd26f169dfcff5a0d5fd69d7dad4d4
@@ -307,7 +307,7 @@ checkstyle {
// the sum of errors that were present before introducing the
// ‘checkstyle’ tool present in the framework and in the official
// plugins.
tasks.checkstyleMain.maxErrors = 37858
tasks.checkstyleMain.maxErrors = 37830
tasks.checkstyleTest.maxErrors = 176
// Currently there are a lot of errors so we need to temporarily
// hide them to avoid polluting the terminal output.

This file was deleted.

@@ -1,4 +1,4 @@
/*******************************************************************************
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -15,8 +15,15 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.apache.ofbiz.base.util.test;
*/
package org.apache.ofbiz.base.util;

import static org.apache.ofbiz.base.util.UtilMisc.toSet;
import static org.apache.ofbiz.base.util.UtilObject.getObjectFromFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
@@ -30,30 +37,10 @@

import org.apache.ofbiz.base.lang.Factory;
import org.apache.ofbiz.base.lang.SourceMonitored;
import org.apache.ofbiz.base.test.GenericTestCaseBase;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GroovyUtil;
import org.apache.ofbiz.base.util.UtilObject;
import org.junit.Test;

@SourceMonitored
public class UtilObjectTests extends GenericTestCaseBase {
public UtilObjectTests(String name) {
super(name);
}

@Override
protected void setUp() throws Exception {
super.setUp();
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
}

public void testStaticHelperClass() throws Exception {
assertStaticHelperClass(UtilObject.class);
}
public class UtilObjectTests {

public static final class ErrorInjector extends FilterInputStream {
private int after;
@@ -115,8 +102,9 @@ public void close() throws IOException {
}
}

@Test
public void testErrorInjector() throws Exception {
byte[] source = new byte[] { 0, 1, 2, 3, 4, 5, 6 };
byte[] source = new byte[] {0, 1, 2, 3, 4, 5, 6};
InputStream in = new ErrorInjector(new ByteArrayInputStream(source), true);
byte[] result = new byte[source.length];
int r = in.read();
@@ -183,7 +171,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
}
}

public void testGetBytes_Object() {
@Test
public void testGetBytesObject() {
assertNotNull("long", UtilObject.getBytes(0L));
assertNotNull("injector good", UtilObject.getBytes(new SerializationInjector(false, false)));
boolean errorOn = Debug.isOn(Debug.ERROR);
@@ -196,6 +185,7 @@ public void testGetBytes_Object() {
}
}

@Test
public void testGetObject() {
Long one = 1L;
byte[] oneBytes = UtilObject.getBytes(one);
@@ -207,7 +197,9 @@ public void testGetObject() {
assertNull("parse empty array", UtilObject.getObject(new byte[0]));

// simulate a ClassNotFoundException
Object groovySerializable = GroovyUtil.eval("class foo implements java.io.Serializable { }; return new foo()", new HashMap<String, Object>());
Object groovySerializable = GroovyUtil.eval(
"class foo implements java.io.Serializable { }; return new foo()",
new HashMap<String, Object>());
byte[] groovySerializableBytes = UtilObject.getBytes(groovySerializable);
assertNotNull("groovySerializableBytes", groovySerializableBytes);
assertNull("groovyDeserializable", UtilObject.getObject(groovySerializableBytes));
@@ -223,6 +215,7 @@ public void testGetObject() {
}
}

@Test
public void testGetByteCount() throws Exception {
assertNotSame("long", 0, UtilObject.getByteCount(0L));
Exception caught = null;
@@ -235,59 +228,77 @@ public void testGetByteCount() throws Exception {
}
}

@Test
public void testDoHashCode() throws Exception {
UtilObject.doHashCode(this);
UtilObject.doHashCode(null);
UtilObject.doHashCode(0);
UtilObject.doHashCode(new Object[] { this, Object.class });
UtilObject.doHashCode(new Object[] { null, Object.class });
UtilObject.doHashCode(new int[] { 1, 3 });
UtilObject.doHashCode(new Object[] {this, Object.class});
UtilObject.doHashCode(new Object[] {null, Object.class});
UtilObject.doHashCode(new int[] {1, 3});
}

public interface TestFactoryIntf extends Factory<Object, Set<String>> {
}

public static class FirstTestFactory implements TestFactoryIntf {
public static final class FirstTestFactory implements TestFactoryIntf {
@Override
public Object getInstance(Set<String> set) {
if (!set.contains("first")) return null;
if (set.contains("one")) return "ONE";
if (set.contains("two")) return "TWO";
if (set.contains("three")) return "THREE";
if (!set.contains("first")) {
return null;
}
if (set.contains("one")) {
return "ONE";
}
if (set.contains("two")) {
return "TWO";
}
if (set.contains("three")) {
return "THREE";
}
return null;
}
}

public static class SecondTestFactory implements TestFactoryIntf {
public static final class SecondTestFactory implements TestFactoryIntf {
@Override
public Object getInstance(Set<String> set) {
if (!set.contains("second")) return null;
if (set.contains("ONE")) return "1";
if (set.contains("TWO")) return "2";
if (set.contains("THREE")) return "3";
if (!set.contains("second")) {
return null;
}
if (set.contains("ONE")) {
return "1";
}
if (set.contains("TWO")) {
return "2";
}
if (set.contains("THREE")) {
return "3";
}
return null;
}
}

@Test
public void testGetObjectFromFactory() throws Exception {
assertEquals("first one", "ONE", UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("first", "one")));
assertEquals("first two", "TWO", UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("first", "two")));
assertEquals("first three", "THREE", UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("first", "three")));
assertEquals("first null", "1", UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("first", "second", "ONE")));
assertEquals("second one", "1", UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("second", "ONE")));
assertEquals("second two", "2", UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("second", "TWO")));
assertEquals("second three", "3", UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("second", "THREE")));
assertEquals("first one", "ONE", getObjectFromFactory(TestFactoryIntf.class, toSet("first", "one")));
assertEquals("first two", "TWO", getObjectFromFactory(TestFactoryIntf.class, toSet("first", "two")));
assertEquals("first three", "THREE", getObjectFromFactory(TestFactoryIntf.class, toSet("first", "three")));
assertEquals("first null", "1", getObjectFromFactory(TestFactoryIntf.class, toSet("first", "second", "ONE")));
assertEquals("second one", "1", getObjectFromFactory(TestFactoryIntf.class, toSet("second", "ONE")));
assertEquals("second two", "2", getObjectFromFactory(TestFactoryIntf.class, toSet("second", "TWO")));
assertEquals("second three", "3", getObjectFromFactory(TestFactoryIntf.class, toSet("second", "THREE")));
Exception caught = null;
try {
UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("first"));
getObjectFromFactory(TestFactoryIntf.class, toSet("first"));
} catch (ClassNotFoundException e) {
caught = e;
} finally {
assertNotNull("nothing found first", caught);
}
caught = null;
try {
UtilObject.getObjectFromFactory(TestFactoryIntf.class, set("second"));
getObjectFromFactory(TestFactoryIntf.class, toSet("second"));
} catch (ClassNotFoundException e) {
caught = e;
} finally {
@@ -28,9 +28,16 @@
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.Test;

public class UtilObjectUnitTest {
@After
public void cleanUp() {
// Ensure that the default value of allowed deserialization classes is used.
UtilProperties.setPropertyValueInMemory("SafeObjectInputStream", "ListOfSafeObjectsForInputStream", "");
}

// Test reading a basic list of string object.
@Test
public void testGetObjectExceptionSafe() throws IOException, ClassNotFoundException {
@@ -0,0 +1,2 @@
org.apache.ofbiz.base.util.UtilObjectTests$FirstTestFactory
org.apache.ofbiz.base.util.UtilObjectTests$SecondTestFactory

0 comments on commit e5933c6

Please sign in to comment.
You can’t perform that action at this time.