hlship 2004/06/09 07:55:32
Added: framework/src/test/org/apache/hivemind/util
TestClasspathResource.java home.png
TestToStringBuilder.java home_fr.png
TestShutdownCoordinator.java
TestLocalizedNameGenerator.java
TestEventListenerList.java TestStringUtils.java
TestBodyBuilder.java TestPropertyUtils.java
Log:
Refactor some tests into a properly named package.
Revision Changes Path
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestClasspathResource.java
Index: TestClasspathResource.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import hivemind.test.FrameworkTestCase;
import java.util.Locale;
import org.apache.hivemind.util.ClasspathResource;
import org.apache.hivemind.Resource;
/**
* Tests for [EMAIL PROTECTED] org.apache.hivemind.util.ClasspathResource}.
*
* @author Howard Lewis Ship
*/
public class TestClasspathResource extends FrameworkTestCase
{
public void testClasspathEquals()
{
Resource l1 = new ClasspathResource(_resolver,
"/org/apache/hivemind/util/home.png");
Resource l2 = new ClasspathResource(_resolver,
"/org/apache/hivemind/util/home.png");
assertEquals("Object equality.", l1, l2);
assertEquals("Hash code", l1.hashCode(), l2.hashCode());
}
public void testClasspathRelativeSameResource()
{
Resource l1 = new ClasspathResource(_resolver, "/foo/bar/Baz");
Resource l2 = l1.getRelativeResource("Baz");
assertSame(l1, l2);
}
public void testClasspathRelativeSameFolder()
{
Resource l1 = new ClasspathResource(_resolver, "/foo/bar/Baz");
Resource expected = new ClasspathResource(_resolver,
"/foo/bar/Fubar");
Resource actual = l1.getRelativeResource("Fubar");
assertEquals(expected, actual);
}
public void testClasspathRelative()
{
Resource l1 = new ClasspathResource(_resolver, "/foo/bar/Baz");
Resource expected = new ClasspathResource(_resolver,
"/foo/bar/gloop/Yup");
Resource actual = l1.getRelativeResource("gloop/Yup");
assertEquals(expected, actual);
}
public void testClasspathAbsolute()
{
Resource l1 = new ClasspathResource(_resolver, "/foo/bar/Baz");
Resource expected = new ClasspathResource(_resolver, "/bip/bop/Boop");
Resource actual = l1.getRelativeResource("/bip/bop/Boop");
assertEquals(expected, actual);
}
public void testClasspathLocalize()
{
Resource l1 = new ClasspathResource(_resolver,
"/org/apache/hivemind/util/home.png");
Resource expected = new ClasspathResource(_resolver,
"/org/apache/hivemind/util/home_fr.png");
Resource actual = l1.getLocalization(Locale.FRANCE);
assertEquals(expected, actual);
}
public void testClasspathLocalizeMissing()
{
Resource l1 = new ClasspathResource(_resolver, "/foo/bar/Baz.zap");
Resource l2 = l1.getLocalization(Locale.ENGLISH);
assertNull(l2);
}
public void testClasspathLocalizeDefault()
{
Resource l1 = new ClasspathResource(_resolver,
"/org/apache/hivemind/util/home.png");
Resource actual = l1.getLocalization(Locale.KOREAN);
assertSame(l1, actual);
}
public void testClasspathLocalizeNull()
{
Resource l1 = new ClasspathResource(_resolver,
"/org/apache/hivemind/util/home.png");
Resource actual = l1.getLocalization(null);
assertSame(l1, actual);
}
public void testEqualsNull()
{
Resource l1 = new ClasspathResource(_resolver,
"/org/apache/hivemind/util/home.png");
assertTrue(!l1.equals(null));
}
public void testClasspathRelativeSamePath()
{
Resource l1 = new ClasspathResource(_resolver, "/foo/bar/Baz");
Resource l2 = l1.getRelativeResource("/foo/bar/Baz");
assertSame(l1, l2);
}
public void testTrailingSlash()
{
Resource l1 = new ClasspathResource(_resolver, "/");
Resource expected = new ClasspathResource(_resolver, "/foo");
Resource actual = l1.getRelativeResource("foo");
assertEquals(expected, actual);
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/home.png
<<Binary file>>
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestToStringBuilder.java
Index: TestToStringBuilder.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import org.apache.hivemind.test.HiveMindTestCase;
import org.apache.hivemind.util.ToStringBuilder;
/**
* Tests for the [EMAIL PROTECTED] org.apache.hivemind.util.ToStringBuilder}
class.
*
* @author Howard Lewis Ship
*/
public class TestToStringBuilder extends HiveMindTestCase
{
private int _originalDefaultMode;
protected void tearDown() throws Exception
{
super.tearDown();
ToStringBuilder.setDefaultMode(_originalDefaultMode);
}
protected void setUp() throws Exception
{
_originalDefaultMode = ToStringBuilder.getDefaultMode();
}
public void testNull()
{
try
{
new ToStringBuilder(null);
unreachable();
}
catch (NullPointerException ex)
{
}
}
public void testSimple()
{
ToStringBuilder b = new ToStringBuilder(this);
assertEquals("TestToStringBuilder", b.toString());
try
{
b.toString();
unreachable();
}
catch (NullPointerException ex)
{
// Can't invoke toString() twice!
}
}
public void testWithHashCode()
{
ToStringBuilder b = new ToStringBuilder(this,
ToStringBuilder.INCLUDE_HASHCODE);
assertEquals("TestToStringBuilder@" +
Integer.toHexString(hashCode()), b.toString());
}
public void testSetDefault()
{
int mode = ToStringBuilder.INCLUDE_HASHCODE |
ToStringBuilder.INCLUDE_PACKAGE_PREFIX;
ToStringBuilder b1 = new ToStringBuilder(this, mode);
ToStringBuilder.setDefaultMode(mode);
ToStringBuilder b2 = new ToStringBuilder(this);
assertEquals(b1.toString(), b2.toString());
}
public void testAppendString()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("fred", "flintstone");
assertEquals("TestToStringBuilder[fred=flintstone]", b.toString());
}
public void testAppendNullString()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("attr", (String) null);
assertEquals("TestToStringBuilder[attr=null]", b.toString());
}
public void testAppendTwo()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("fred", "flintstone");
b.append("barney", "rubble");
assertEquals("TestToStringBuilder[fred=flintstone barney=rubble]",
b.toString());
}
public void testAppendObject()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("number", new Integer(27));
assertEquals("TestToStringBuilder[number=27]", b.toString());
}
public void testAppendNullObject()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("object", null);
assertEquals("TestToStringBuilder[object=null]", b.toString());
}
public void testAppendBoolean()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("yes", true);
b.append("no", false);
assertEquals("TestToStringBuilder[yes=true no=false]", b.toString());
}
public void testAppendByte()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("byte", (byte) 32);
assertEquals("TestToStringBuilder[byte=32]", b.toString());
}
public void testAppendShort()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("short", (short) - 37);
assertEquals("TestToStringBuilder[short=-37]", b.toString());
}
public void testAppendInt()
{
ToStringBuilder b = new ToStringBuilder(this);
b.append("int", 217);
assertEquals("TestToStringBuilder[int=217]", b.toString());
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/home_fr.png
<<Binary file>>
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestShutdownCoordinator.java
Index: TestShutdownCoordinator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Registry;
import org.apache.hivemind.ShutdownCoordinator;
import org.apache.hivemind.events.RegistryShutdownListener;
import org.apache.hivemind.impl.RegistryBuilder;
import org.apache.hivemind.impl.ShutdownCoordinatorImpl;
import hivemind.test.FrameworkTestCase;
/**
* Tests the [EMAIL PROTECTED] org.apache.hivemind.impl.ShutdownCoordinator}.
*
* @author Howard Lewis Ship
*/
public class TestShutdownCoordinator extends FrameworkTestCase
{
private static class Fixture implements RegistryShutdownListener
{
private boolean _shutdown;
public boolean isShutdown()
{
return _shutdown;
}
public void registryDidShutdown()
{
_shutdown = true;
}
}
public void testShutdownCoordinator()
{
ShutdownCoordinator c = new ShutdownCoordinatorImpl();
Fixture f = new Fixture();
c.addRegistryShutdownListener(f);
c.shutdown();
assertEquals(true, f.isShutdown());
// For good riddens; test no failure if already down.
c.shutdown();
}
public void testShutdownCoordinatorService()
{
Registry r = RegistryBuilder.constructDefaultRegistry();
ShutdownCoordinator c =
(ShutdownCoordinator) r.getService(
"hivemind.ShutdownCoordinator",
ShutdownCoordinator.class);
Fixture f = new Fixture();
c.addRegistryShutdownListener(f);
c.shutdown();
assertEquals(true, f.isShutdown());
}
public void testShutdownFailure() throws Exception
{
ShutdownCoordinator c = new ShutdownCoordinatorImpl();
c.addRegistryShutdownListener(new RegistryShutdownListener()
{
public void registryDidShutdown()
{
throw new ApplicationRuntimeException("I'm just not in the
mood.");
}
});
interceptLogging();
c.shutdown();
assertLoggedMessagePattern("Unable to shutdown .*: I'm just not in
the mood\\.");
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestLocalizedNameGenerator.java
Index: TestLocalizedNameGenerator.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import hivemind.test.FrameworkTestCase;
import java.util.Locale;
import java.util.NoSuchElementException;
import junit.framework.AssertionFailedError;
import org.apache.hivemind.util.LocalizedNameGenerator;
/**
* Suite of tests for [EMAIL PROTECTED]
org.apache.tapestry.util.LocalizedNameGenerator}.
*
* @author Howard Lewis Ship
*/
public class TestLocalizedNameGenerator extends FrameworkTestCase
{
public void testBasic()
{
LocalizedNameGenerator g = new LocalizedNameGenerator("basic",
Locale.US, ".test");
assertTrue(g.more());
assertEquals("basic_en_US.test", g.next());
assertTrue(g.more());
assertEquals("basic_en.test", g.next());
assertTrue(g.more());
assertEquals("basic.test", g.next());
assertTrue(!g.more());
}
public void testNoCountry()
{
LocalizedNameGenerator g = new LocalizedNameGenerator("noCountry",
Locale.FRENCH, ".zap");
assertTrue(g.more());
assertEquals("noCountry_fr.zap", g.next());
assertTrue(g.more());
assertEquals("noCountry.zap", g.next());
assertTrue(!g.more());
}
public void testVariantWithoutCountry()
{
LocalizedNameGenerator g =
new LocalizedNameGenerator("fred", new Locale("en", "", "GEEK"),
".foo");
assertTrue(g.more());
// The double-underscore is correct, it's a kind
// of placeholder for the null country.
// JDK1.3 always converts the locale to upper case. JDK 1.4
// does not. To keep this test happyt, we selected an all-uppercase
// locale.
assertEquals("fred_en__GEEK.foo", g.next());
assertTrue(g.more());
assertEquals("fred_en.foo", g.next());
assertTrue(g.more());
assertEquals("fred.foo", g.next());
assertTrue(!g.more());
}
public void testNullLocale()
{
LocalizedNameGenerator g = new LocalizedNameGenerator("nullLocale",
null, ".bar");
assertTrue(g.more());
assertEquals("nullLocale.bar", g.next());
assertTrue(!g.more());
}
public void testNullSuffix()
{
LocalizedNameGenerator g = new LocalizedNameGenerator("nullSuffix",
null, null);
assertTrue(g.more());
assertEquals("nullSuffix", g.next());
assertTrue(!g.more());
}
public void testForException()
{
LocalizedNameGenerator g = new LocalizedNameGenerator("bob", null,
".foo");
assertTrue(g.more());
assertEquals("bob.foo", g.next());
assertTrue(!g.more());
try
{
g.next();
throw new AssertionFailedError("Unreachable.");
}
catch (NoSuchElementException ex)
{
}
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestEventListenerList.java
Index: TestEventListenerList.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import hivemind.test.FrameworkTestCase;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.hivemind.util.EventListenerList;
/**
* Tests for [EMAIL PROTECTED] org.apache.hivemind.util.EventListenerList}.
*
* @author Howard Lewis Ship
*/
public class TestEventListenerList extends FrameworkTestCase
{
private static class Trigger
{
private boolean _trigger;
public boolean isTrigger()
{
return _trigger;
}
public void setTrigger(boolean b)
{
_trigger = b;
}
}
private Trigger[] buildTriggers(int count)
{
Trigger[] result = new Trigger[count];
for (int i = 0; i < count; i++)
{
result[i] = new Trigger();
}
return result;
}
private void addAll(EventListenerList l, Trigger[] t)
{
for (int i = 0; i < t.length; i++)
l.addListener(t[i]);
}
private void checkAllTrue(Trigger[] t)
{
for (int i = 0; i < t.length; i++)
assertEquals(true, t[i].isTrigger());
}
public void testBasic()
{
EventListenerList l = new EventListenerList();
Trigger[] ta = buildTriggers(20);
addAll(l, ta);
Iterator i = l.getListeners();
while (i.hasNext())
{
Trigger t = (Trigger) i.next();
t.setTrigger(true);
}
}
public void testEmptyList()
{
EventListenerList l = new EventListenerList();
Iterator i = l.getListeners();
assertEquals(false, i.hasNext());
}
public void testLateAdd()
{
Trigger[] ta = buildTriggers(20);
EventListenerList l = new EventListenerList();
addAll(l, ta);
Iterator i = l.getListeners();
for (int j = 0; j < 5; j++)
{
Trigger t = (Trigger) i.next();
t.setTrigger(true);
}
Trigger tnew = new Trigger();
l.addListener(tnew);
while (i.hasNext())
{
Trigger t = (Trigger) i.next();
t.setTrigger(true);
}
assertEquals(false, tnew.isTrigger());
checkAllTrue(ta);
}
public void testLateRemove()
{
Trigger[] ta = buildTriggers(20);
EventListenerList l = new EventListenerList();
addAll(l, ta);
Iterator i = l.getListeners();
for (int j = 0; j < 5; j++)
{
Trigger t = (Trigger) i.next();
t.setTrigger(true);
}
Trigger tremoved = ta[15];
l.removeListener(tremoved);
while (i.hasNext())
{
Trigger t = (Trigger) i.next();
t.setTrigger(true);
}
checkAllTrue(ta);
}
public void testRemoveMissing()
{
Trigger[] ta = buildTriggers(20);
EventListenerList l = new EventListenerList();
addAll(l, ta);
Trigger tremove = new Trigger();
l.removeListener(tremove);
Iterator i = l.getListeners();
while (i.hasNext())
{
Trigger t = (Trigger) i.next();
t.setTrigger(true);
}
checkAllTrue(ta);
}
public void testIteratorRemoveFailure()
{
Trigger[] ta = buildTriggers(20);
EventListenerList l = new EventListenerList();
addAll(l, ta);
Iterator i = l.getListeners();
for (int j = 0; j < 5; j++)
i.next();
try
{
i.remove();
unreachable();
}
catch (UnsupportedOperationException ex)
{
}
}
public void testOutOfMemoryError()
{
Trigger ta = buildTriggers(1)[0];
EventListenerList l = new EventListenerList();
List iterators = new ArrayList();
try
{
for (int i = 0; i < 100; i++)
{
l.addListener(ta);
iterators.add(l.getListeners());
l.removeListener(ta);
}
}
catch (OutOfMemoryError e)
{
fail("Ran out of memory!");
}
catch (Throwable e)
{
fail(e.getMessage());
}
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestStringUtils.java
Index: TestStringUtils.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import org.apache.hivemind.test.HiveMindTestCase;
import org.apache.hivemind.util.StringUtils;
/**
* Tests for the static [EMAIL PROTECTED]
org.apache.hivemind.util.StringUtils} class.
*
* @author Howard Lewis Ship
*/
public class TestStringUtils extends HiveMindTestCase
{
public void testCapitalize()
{
assertEquals("Fred", StringUtils.capitalize("fred"));
assertSame("Barney", StringUtils.capitalize("Barney"));
}
public void testCapitalizeEmpty()
{
assertSame("", StringUtils.capitalize(""));
}
public void testCapitalizeNull()
{
try
{
StringUtils.capitalize(null);
unreachable();
}
catch (NullPointerException ex)
{
assertTrue(true);
}
}
public void testSplit()
{
assertListsEqual(new String[] { "alpha", "beta" },
StringUtils.split("alpha,beta"));
}
public void testSplitSingle()
{
assertListsEqual(new String[] { "alpha" },
StringUtils.split("alpha"));
}
public void testSplitNull()
{
assertListsEqual(new String[0], StringUtils.split(null));
}
public void testSplitEmpty()
{
assertListsEqual(new String[0], StringUtils.split(null));
}
public void testSplitTrailingComma()
{
assertListsEqual(
new String[] { "fred", "barney", "wilma" },
StringUtils.split("fred,barney,wilma,"));
}
public void testJoin()
{
assertEquals("alpha:beta", StringUtils.join(new String[] { "alpha",
"beta" }, ':'));
}
public void testJoinNull()
{
assertEquals(null, StringUtils.join(null, ','));
}
public void testJoinEmpty()
{
assertEquals(null, StringUtils.join(new String[0], ','));
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestBodyBuilder.java
Index: TestBodyBuilder.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import org.apache.hivemind.service.BodyBuilder;
import hivemind.test.FrameworkTestCase;
/**
* Tests the utility class [EMAIL PROTECTED]
org.apache.hivemind.service.BodyBuilder},
* used with dynamic code generation.
*
* @author Howard Lewis Ship
*/
public class TestBodyBuilder extends FrameworkTestCase
{
private BodyBuilder _b = new BodyBuilder();
public void testBasic()
{
_b.begin();
_b.addln("invoke();");
_b.end();
assertEquals("{\n invoke();\n}\n", _b.toString());
}
public void testQuoted()
{
_b.add("invoke(");
_b.addQuoted("fred");
_b.add(");");
assertEquals("invoke(\"fred\");", _b.toString());
}
public void testNested()
{
_b.begin();
_b.add("while(true)");
_b.begin();
_b.add("_i += 1;");
_b.end();
_b.end();
assertEquals("{\n while(true)\n {\n _i += 1;\n }\n}\n",
_b.toString());
}
public void testAddln()
{
_b.begin();
_b.addln("invoke(fred);");
_b.addln("invoke(barney);");
_b.end();
assertEquals("{\n invoke(fred);\n invoke(barney);\n}\n",
_b.toString());
}
public void testClear()
{
_b.add("fred");
assertEquals("fred", _b.toString());
_b.clear();
_b.add("barney");
assertEquals("barney", _b.toString());
}
public void testAddPattern1()
{
_b.add("today is {0}.", "tuesday");
assertEquals("today is tuesday.", _b.toString());
}
public void testAddlnPattern1()
{
_b.addln("The capital of France is {0}.", "Paris");
assertEquals("The capital of France is Paris.\n", _b.toString());
}
public void testAddPattern2()
{
_b.add("Current suspects are: {0} and {1}.", "Tony", "Junior");
assertEquals("Current suspects are: Tony and Junior.", _b.toString());
}
public void testAddlnPattern2()
{
_b.addln("The capital of {0} is {1}.", "Germany", "Berlin");
assertEquals("The capital of Germany is Berlin.\n", _b.toString());
}
public void testAddPattern3()
{
_b.add("{0} + {1} = {2}", new Integer(5), new Integer(7), new
Integer(13));
assertEquals("5 + 7 = 13", _b.toString());
}
public void testAddlnPattern3()
{
_b.addln("The Holy Trinity: {0}, {1} and {2}.", "Tapestry",
"HiveMind", "Hibernate");
assertEquals("The Holy Trinity: Tapestry, HiveMind and Hibernate.\n",
_b.toString());
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestPropertyUtils.java
Index: TestPropertyUtils.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// 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.apache.hivemind.util;
import java.awt.Image;
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.EventSetDescriptor;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Location;
import org.apache.hivemind.Resource;
import org.apache.hivemind.impl.DefaultClassResolver;
import org.apache.hivemind.impl.LocationImpl;
import org.apache.hivemind.test.HiveMindTestCase;
import org.apache.hivemind.util.ClasspathResource;
import org.apache.hivemind.util.PropertyUtils;
/**
* Tests for the [EMAIL PROTECTED] org.apache.hivemind.util.PropertyUtils}
class.
*
* @author Howard Lewis Ship
*/
public class TestPropertyUtils extends HiveMindTestCase
{
private Location makeLocation(int line)
{
Resource r = new ClasspathResource(new DefaultClassResolver(),
"/foo/bar");
return new LocationImpl(r, line);
}
public static class Bean
{
private int _value;
public int getValue()
{
return _value;
}
public void setValue(int value)
{
_value = value;
}
public String toString()
{
return "PropertyUtilsTestBean";
}
public void setWriteOnly(boolean b)
{
}
}
public static class ExceptionBean
{
public boolean getFailure()
{
throw new RuntimeException("getFailure");
}
public void setFailure(boolean b)
{
throw new RuntimeException("setFailure");
}
public String toString()
{
return "PropertyUtilsExceptionBean";
}
}
public static class UglyBean
{
}
public static class UglyBeanBeanInfo implements BeanInfo
{
public BeanInfo[] getAdditionalBeanInfo()
{
return null;
}
public BeanDescriptor getBeanDescriptor()
{
return null;
}
public int getDefaultEventIndex()
{
return 0;
}
public int getDefaultPropertyIndex()
{
return 0;
}
public EventSetDescriptor[] getEventSetDescriptors()
{
return null;
}
public Image getIcon(int iconKind)
{
return null;
}
public MethodDescriptor[] getMethodDescriptors()
{
return null;
}
public PropertyDescriptor[] getPropertyDescriptors()
{
throw new RuntimeException("This is the UglyBean.");
}
}
public void testRead()
{
Bean b = new Bean();
b.setValue(37);
assertEquals(new Integer(37), PropertyUtils.read(b, "value", null));
}
public void testWrite()
{
Bean b = new Bean();
PropertyUtils.write(b, "value", new Integer(412), null);
assertEquals(412, b.getValue());
}
public void testMissingProperty()
{
Location l = makeLocation(19);
Bean b = new Bean();
try
{
PropertyUtils.read(b, "zaphod", l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertEquals(
"Class org.apache.hivemind.util.TestPropertyUtils$Bean does
not "
+ "contain a property named 'zaphod' (at
classpath:/foo/bar, line 19).",
ex.getMessage());
assertEquals(b, ex.getComponent());
assertEquals(l, ex.getLocation());
}
}
public void testReadOnly()
{
Location l = makeLocation(27);
Bean b = new Bean();
try
{
PropertyUtils.write(b, "class", null, l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertEquals(
"Property class of object PropertyUtilsTestBean is "
+ "read-only (at classpath:/foo/bar, line 27).",
ex.getMessage());
assertEquals(b, ex.getComponent());
assertEquals(l, ex.getLocation());
}
}
public void testWriteOnly()
{
Location l = makeLocation(35);
Bean b = new Bean();
try
{
PropertyUtils.read(b, "writeOnly", l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertEquals(
"Property writeOnly of object PropertyUtilsTestBean is
write-only (at classpath:/foo/bar, line 35).",
ex.getMessage());
assertEquals(b, ex.getComponent());
assertEquals(l, ex.getLocation());
}
}
public void testReadFailure()
{
Location l = makeLocation(17);
ExceptionBean b = new ExceptionBean();
try
{
PropertyUtils.read(b, "failure", l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertEquals(
"Unable to read property failure of object
PropertyUtilsExceptionBean "
+ "(at classpath:/foo/bar, line 17): null",
ex.getMessage());
assertEquals(b, ex.getComponent());
assertEquals(l, ex.getLocation());
}
}
public void testWriteFailure()
{
Location l = makeLocation(17);
ExceptionBean b = new ExceptionBean();
try
{
PropertyUtils.write(b, "failure", Boolean.FALSE, l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertEquals(
"Unable to update property failure of object
PropertyUtilsExceptionBean "
+ "(at classpath:/foo/bar, line 17): null",
ex.getMessage());
assertEquals(b, ex.getComponent());
assertEquals(l, ex.getLocation());
}
}
public void testIntrospectFailure()
{
Location l = makeLocation(212);
UglyBean b = new UglyBean();
try
{
PropertyUtils.read(b, "google", l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertEquals(
"Unable to introspect properties of class "
+ "org.apache.hivemind.util.TestPropertyUtils$UglyBean "
+ "(at classpath:/foo/bar, line 212): null",
ex.getMessage());
assertEquals(b, ex.getComponent());
assertEquals(l, ex.getLocation());
}
}
public void testNull()
{
Location l = makeLocation(63);
try
{
PropertyUtils.read(null, "fred", l);
unreachable();
}
catch (ApplicationRuntimeException ex)
{
assertEquals(
"Attempt to read or update properties of null (at
classpath:/foo/bar, line 63).",
ex.getMessage());
assertEquals(l, ex.getLocation());
}
}
public void testGetPropertyType()
{
Bean b = new Bean();
assertEquals(int.class, PropertyUtils.getPropertyType(b, "value",
null));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]