[Aprog]
[aprog.git] / Aprog / test / net / sourceforge / aprog / tools / ToolsTest.java
blob174e65f53e9e13f87d0b54e67dbc80527662746d
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
5 *
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
22 * THE SOFTWARE.
25 package net.sourceforge.aprog.tools;
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
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;
35 import java.util.Set;
36 import java.util.Vector;
37 import java.util.concurrent.Callable;
39 import org.junit.Test;
41 /**
42 * Automated tests using JUnit 4 for {@link Tools}.
44 * @author codistmonk (creation 2010-06-11)
46 public final class ToolsTest {
48 @Test
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());
69 @Test
70 public final void testClose() throws IOException {
71 final InputStream input = Tools.getResourceAsStream(Tools.getThisPackagePath() + "test.txt");
73 assertTrue(input.available() > 0);
75 Tools.close(input);
77 try {
78 input.available();
80 fail("This point shouldn't be reached");
81 } catch (final IOException exception) {
82 assertEquals("Stream closed", exception.getMessage());
86 @Test
87 public final void testListAndIterable() {
88 final Vector<Object> vector = new Vector<Object>();
90 vector.add(42);
91 vector.add(33);
93 assertEquals(vector, Tools.list(Tools.iterable(vector.elements())));
96 @Test
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));
104 @Test
105 public final void testSet() {
106 final Set<?> set = Tools.set(42, 33, 42);
108 assertArrayEquals(Tools.array(42, 33), set.toArray());
111 @Test
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));
121 @Test
122 public final void testInvoke() {
123 assertEquals(42, Tools.invoke(Integer.class, "parseInt", "42"));
124 assertEquals(42, Tools.invoke(42L, "intValue"));
127 @Test
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());
163 @Test
164 public final void testGetGetterFailure() {
165 final ObjectWithArbitraryProperties objectWithArbitraryProperties = new ObjectWithArbitraryProperties();
167 try {
168 // Missing property
169 final Method getter = Tools.getGetter(objectWithArbitraryProperties, "missingProperty");
171 fail("getGetter() should have failed but instead returned " + getter);
172 } catch (final RuntimeException expectedException) {
173 // Do nothing
177 try {
178 // Bad casing
179 final Method getter = Tools.getGetter(objectWithArbitraryProperties, "INTPROPERTY");
181 fail("getGetter() should have failed but instead returned " + getter);
182 } catch (final RuntimeException expectedException) {
183 // Do nothing
188 @Test
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());
212 @Test
213 public final void testGetSetterFailure() {
214 final ObjectWithArbitraryProperties objectWithArbitraryProperties = new ObjectWithArbitraryProperties();
217 try {
218 // Missing property
219 final Method setter = Tools.getGetter(objectWithArbitraryProperties, "missingProperty");
221 fail("getSetter() should have failed but instead returned " + setter);
222 } catch (final RuntimeException expectedException) {
223 // Do nothing
228 try {
229 // Bad casing
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) {
234 // Do nothing
238 try {
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) {
244 // Do nothing
249 @Test
250 public final void testToUpperCamelCase() {
251 assertEquals("CamelCase", Tools.toUpperCamelCase("camelCase"));
254 @Test
255 public final void testEmptyIfNull() {
256 assertEquals("", Tools.emptyIfNull(null));
257 assertSame("", Tools.emptyIfNull(""));
258 assertSame("42", Tools.emptyIfNull("42"));
261 @Test
262 public final void testGetPackagePath() {
263 assertEquals("net/sourceforge/aprog/tools/", Tools.getPackagePath(ToolsTest.class));
266 @Test
267 public final void testGetCallerPackagePath() {
268 assertEquals("net/sourceforge/aprog/tools/", Tools.getThisPackagePath());
271 @Test
272 public final void testGetTopLevelEclosingClass() throws Exception {
273 assertEquals(this.getClass(), new Callable<Class<?>>() {
275 @Override
276 public final Class<?> call() throws Exception {
277 return Tools.getTopLevelEnclosingClass(this.getClass());
280 }.call());
283 @Test
284 public final void testGetCallerClass() {
285 assertEquals(this.getClass(), ToolsTest.getCallerClass());
288 @Test
289 public final void testGetLoggerForThisMethod() {
290 assertTrue(Tools.getLoggerForThisMethod().getName().endsWith("testGetLoggerForThisMethod"));
293 @Test
294 public final void testThrowUnchecked() {
296 final Throwable originalThrowable = new RuntimeException();
298 try {
299 Tools.throwUnchecked(originalThrowable);
300 } catch(final RuntimeException caughtThrowable) {
301 assertSame(originalThrowable, caughtThrowable);
306 final Throwable originalThrowable = new Exception();
308 try {
309 Tools.throwUnchecked(originalThrowable);
310 } catch(final RuntimeException caughtThrowable) {
311 assertNotNull(caughtThrowable.getCause());
312 assertSame(originalThrowable, caughtThrowable.getCause());
317 final Throwable originalThrowable = new Error();
319 try {
320 Tools.throwUnchecked(originalThrowable);
321 } catch(final Error caughtThrowable) {
322 assertSame(originalThrowable, caughtThrowable);
327 final Throwable originalThrowable = new Throwable();
329 try {
330 Tools.throwUnchecked(originalThrowable);
331 } catch(final Throwable caughtThrowable) {
332 assertSame(originalThrowable, caughtThrowable.getCause());
337 @Test
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));
361 @Test
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);
370 assertNull(badCast);
373 @Test
374 public final void testCastToCurrentClass() {
375 assertNull(Tools.castToCurrentClass(42));
376 assertSame(this, Tools.castToCurrentClass(this));
377 assertNotNull(Tools.castToCurrentClass(new ToolsTest()));
380 @Test
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));
391 @Test
392 public final void testHashCode() {
393 final Object object = "42";
395 assertEquals(0, Tools.hashCode(null));
396 assertEquals(object.hashCode(), Tools.hashCode(object));
401 * @return
402 * <br>Maybe null
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;
428 * @return
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;
482 * @return
483 * <br>A possibly null value
484 * <br>A shared value
486 final String getPackagePrivateStringProperty() {
487 return this.packagePrivateStringProperty;
492 * @param packagePrivateStringProperty
493 * <br>Can be null
494 * <br>Shared parameter
496 final void setPackagePrivateStringProperty(final String packagePrivateStringProperty) {
497 this.packagePrivateStringProperty = packagePrivateStringProperty;