4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.tools
;
27 import java
.io
.ByteArrayInputStream
;
29 import java
.io
.FileInputStream
;
30 import java
.io
.IOException
;
31 import java
.io
.InputStream
;
32 import static org
.junit
.Assert
.*;
34 import java
.lang
.reflect
.Method
;
36 import java
.util
.Vector
;
37 import java
.util
.concurrent
.Callable
;
39 import org
.junit
.Test
;
42 * Automated tests using JUnit 4 for {@link Tools}.
44 * @author codistmonk (creation 2010-06-11)
46 public final class ToolsTest
{
49 public final void testCreateTemporaryFile() {
51 final File temporaryFile
= Tools
.createTemporaryFile("prefix", "suffix", null);
53 assertTrue(temporaryFile
.exists());
54 assertTrue(temporaryFile
.getName().startsWith("prefix"));
55 assertTrue(temporaryFile
.getName().endsWith("suffix"));
56 assertEquals(0L, temporaryFile
.length());
59 final File temporaryFile
= Tools
.createTemporaryFile("prefix", "suffix",
60 Tools
.getResourceAsStream(Tools
.getThisPackagePath() + "test.txt"));
62 assertTrue(temporaryFile
.exists());
63 assertTrue(temporaryFile
.getName().startsWith("prefix"));
64 assertTrue(temporaryFile
.getName().endsWith("suffix"));
65 assertEquals(2L, temporaryFile
.length());
70 public final void testClose() throws IOException
{
71 final InputStream input
= Tools
.getResourceAsStream(Tools
.getThisPackagePath() + "test.txt");
73 assertTrue(input
.available() > 0);
80 fail("This point shouldn't be reached");
81 } catch (final IOException exception
) {
82 assertEquals("Stream closed", exception
.getMessage());
87 public final void testListAndIterable() {
88 final Vector
<Object
> vector
= new Vector
<Object
>();
93 assertEquals(vector
, Tools
.list(Tools
.iterable(vector
.elements())));
97 public final void testArray() {
98 final Object
[] array
= new Object
[] { 42, 33 };
100 assertSame(array
, Tools
.array(array
));
101 assertArrayEquals(array
, Tools
.array(42, 33));
105 public final void testSet() {
106 final Set
<?
> set
= Tools
.set(42, 33, 42);
108 assertArrayEquals(Tools
.array(42, 33), set
.toArray());
112 public final void testAppend() {
113 final Object
[] empty
= new Object
[0];
115 assertArrayEquals(Tools
.array(42, 33, 42), Tools
.append(Tools
.array(42), Tools
.array(33, 42)));
116 assertArrayEquals(Tools
.array(42), Tools
.append(Tools
.array(42), empty
));
117 assertArrayEquals(Tools
.array(42), Tools
.append(empty
, Tools
.array((Object
) 42)));
118 assertArrayEquals(empty
, Tools
.append(empty
, empty
));
122 public final void testInvoke() {
123 assertEquals(42, Tools
.invoke(Integer
.class, "parseInt", "42"));
124 assertEquals(42, Tools
.invoke(42L, "intValue"));
128 public final void testGetGetter() {
129 final ObjectWithArbitraryProperties objectWithArbitraryProperties
= new ObjectWithArbitraryProperties();
132 final Method getter
= Tools
.getGetter(objectWithArbitraryProperties
, "intProperty");
134 assertNotNull(getter
);
135 assertEquals("getIntProperty", getter
.getName());
138 final Method getter
= Tools
.getGetter(objectWithArbitraryProperties
, "booleanProperty1");
140 assertNotNull(getter
);
141 assertEquals("isBooleanProperty1", getter
.getName());
144 final Method getter
= Tools
.getGetter(objectWithArbitraryProperties
, "booleanProperty2");
146 assertNotNull(getter
);
147 assertEquals("hasBooleanProperty2", getter
.getName());
150 final Method getter
= Tools
.getGetter(objectWithArbitraryProperties
, "booleanProperty3");
152 assertNotNull(getter
);
153 assertEquals("getBooleanProperty3", getter
.getName());
156 final Method getter
= Tools
.getGetter(objectWithArbitraryProperties
, "packagePrivateStringProperty");
158 assertNotNull(getter
);
159 assertEquals("getPackagePrivateStringProperty", getter
.getName());
164 public final void testGetGetterFailure() {
165 final ObjectWithArbitraryProperties objectWithArbitraryProperties
= new ObjectWithArbitraryProperties();
169 final Method getter
= Tools
.getGetter(objectWithArbitraryProperties
, "missingProperty");
171 fail("getGetter() should have failed but instead returned " + getter
);
172 } catch (final RuntimeException expectedException
) {
179 final Method getter
= Tools
.getGetter(objectWithArbitraryProperties
, "INTPROPERTY");
181 fail("getGetter() should have failed but instead returned " + getter
);
182 } catch (final RuntimeException expectedException
) {
189 public final void testGetSetter() {
190 final ObjectWithArbitraryProperties objectWithArbitraryProperties
= new ObjectWithArbitraryProperties();
193 final Method setter
= Tools
.getSetter(objectWithArbitraryProperties
, "intProperty", int.class);
195 assertNotNull(setter
);
196 assertEquals("setIntProperty", setter
.getName());
199 final Method setter
= Tools
.getSetter(objectWithArbitraryProperties
, "booleanProperty1", boolean.class);
201 assertNotNull(setter
);
202 assertEquals("setBooleanProperty1", setter
.getName());
205 final Method setter
= Tools
.getSetter(objectWithArbitraryProperties
, "packagePrivateStringProperty", String
.class);
207 assertNotNull(setter
);
208 assertEquals("setPackagePrivateStringProperty", setter
.getName());
213 public final void testGetSetterFailure() {
214 final ObjectWithArbitraryProperties objectWithArbitraryProperties
= new ObjectWithArbitraryProperties();
219 final Method setter
= Tools
.getGetter(objectWithArbitraryProperties
, "missingProperty");
221 fail("getSetter() should have failed but instead returned " + setter
);
222 } catch (final RuntimeException expectedException
) {
230 final Method setter
= Tools
.getSetter(objectWithArbitraryProperties
, "INTPROPERTY", int.class);
232 fail("getSetter() should have failed but instead returned " + setter
);
233 } catch (final RuntimeException expectedException
) {
239 // Mismatching parameter type
240 final Method setter
= Tools
.getSetter(objectWithArbitraryProperties
, "intProperty", boolean.class);
242 fail("getSetter() should have failed but instead returned " + setter
);
243 } catch (final RuntimeException expectedException
) {
250 public final void testToUpperCamelCase() {
251 assertEquals("CamelCase", Tools
.toUpperCamelCase("camelCase"));
255 public final void testEmptyIfNull() {
256 assertEquals("", Tools
.emptyIfNull(null));
257 assertSame("", Tools
.emptyIfNull(""));
258 assertSame("42", Tools
.emptyIfNull("42"));
262 public final void testGetPackagePath() {
263 assertEquals("net/sourceforge/aprog/tools/", Tools
.getPackagePath(ToolsTest
.class));
267 public final void testGetCallerPackagePath() {
268 assertEquals("net/sourceforge/aprog/tools/", Tools
.getThisPackagePath());
272 public final void testGetTopLevelEclosingClass() throws Exception
{
273 assertEquals(this.getClass(), new Callable
<Class
<?
>>() {
276 public final Class
<?
> call() throws Exception
{
277 return Tools
.getTopLevelEnclosingClass(this.getClass());
284 public final void testGetCallerClass() {
285 assertEquals(this.getClass(), ToolsTest
.getCallerClass());
289 public final void testGetLoggerForThisMethod() {
290 assertTrue(Tools
.getLoggerForThisMethod().getName().endsWith("testGetLoggerForThisMethod"));
294 public final void testThrowUnchecked() {
296 final Throwable originalThrowable
= new RuntimeException();
299 Tools
.throwUnchecked(originalThrowable
);
300 } catch(final RuntimeException caughtThrowable
) {
301 assertSame(originalThrowable
, caughtThrowable
);
306 final Throwable originalThrowable
= new Exception();
309 Tools
.throwUnchecked(originalThrowable
);
310 } catch(final RuntimeException caughtThrowable
) {
311 assertNotNull(caughtThrowable
.getCause());
312 assertSame(originalThrowable
, caughtThrowable
.getCause());
317 final Throwable originalThrowable
= new Error();
320 Tools
.throwUnchecked(originalThrowable
);
321 } catch(final Error caughtThrowable
) {
322 assertSame(originalThrowable
, caughtThrowable
);
327 final Throwable originalThrowable
= new Throwable();
330 Tools
.throwUnchecked(originalThrowable
);
331 } catch(final Throwable caughtThrowable
) {
332 assertSame(originalThrowable
, caughtThrowable
.getCause());
338 public final void testUnchecked() {
340 final Throwable cause
= new Throwable();
342 assertSame(cause
, Tools
.unchecked(cause
).getCause());
345 final Throwable cause
= new Error();
347 assertSame(cause
, Tools
.unchecked(cause
).getCause());
350 final Throwable cause
= new Exception();
352 assertSame(cause
, Tools
.unchecked(cause
).getCause());
355 final Throwable cause
= new RuntimeException();
357 assertSame(cause
, Tools
.unchecked(cause
));
362 public final void testCast() {
363 final Object object
= "42";
364 final String that
= Tools
.cast(String
.class, object
);
366 assertSame(object
, that
);
368 final Integer badCast
= Tools
.cast(Integer
.class, object
);
374 public final void testCastToCurrentClass() {
375 assertNull(Tools
.castToCurrentClass(42));
376 assertSame(this, Tools
.castToCurrentClass(this));
377 assertNotNull(Tools
.castToCurrentClass(new ToolsTest()));
381 public final void testEquals() {
382 final Object object
= "42";
384 assertTrue(Tools
.equals(null, null));
385 assertFalse(Tools
.equals(object
, null));
386 assertTrue(Tools
.equals(object
, object
));
387 assertTrue(Tools
.equals(new Integer(6 * 7).toString(), object
));
388 assertFalse(Tools
.equals(object
, 42));
392 public final void testHashCode() {
393 final Object object
= "42";
395 assertEquals(0, Tools
.hashCode(null));
396 assertEquals(object
.hashCode(), Tools
.hashCode(object
));
404 private static final Class
<?
> getCallerClass() {
405 return Tools
.getCallerClass();
409 * This class is package-private to suppress visibility and usage warnings.
411 * @author codistmonk (creation 2010-05-19)
414 static class ObjectWithArbitraryProperties
{
416 private int intProperty
;
418 private boolean booleanProperty1
;
420 private boolean booleanProperty2
;
422 private boolean booleanProperty3
;
424 private String packagePrivateStringProperty
;
429 * <br>Range: Any integer
431 public final int getIntProperty() {
432 return this.intProperty
;
437 * @param intProperty an arbitrary integer
438 * <br>Range: Any integer
440 public final void setIntProperty(final int intProperty
) {
441 this.intProperty
= intProperty
;
444 public final boolean isBooleanProperty1() {
445 return this.booleanProperty1
;
450 * @param booleanProperty1 an arbitrary boolean
452 public final void setBooleanProperty1(final boolean booleanProperty1
) {
453 this.booleanProperty1
= booleanProperty1
;
456 public final boolean hasBooleanProperty2() {
457 return this.booleanProperty2
;
462 * @param booleanProperty2 an arbitrary boolean
464 public final void setBooleanProperty2(final boolean booleanProperty2
) {
465 this.booleanProperty2
= booleanProperty2
;
468 public final boolean getBooleanProperty3() {
469 return this.booleanProperty3
;
474 * @param booleanProperty3 an arbitrary boolean
476 public final void setBooleanProperty3(final boolean booleanProperty3
) {
477 this.booleanProperty3
= booleanProperty3
;
483 * <br>A possibly null value
486 final String
getPackagePrivateStringProperty() {
487 return this.packagePrivateStringProperty
;
492 * @param packagePrivateStringProperty
494 * <br>Shared parameter
496 final void setPackagePrivateStringProperty(final String packagePrivateStringProperty
) {
497 this.packagePrivateStringProperty
= packagePrivateStringProperty
;