|
| 1 | +package com.baeldung.commons.lang3; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotSame; |
| 6 | +import static org.junit.Assert.assertSame; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | +import static org.junit.Assert.fail; |
| 9 | + |
| 10 | +import java.beans.PropertyChangeEvent; |
| 11 | +import java.beans.PropertyChangeListener; |
| 12 | +import java.io.File; |
| 13 | +import java.lang.reflect.Field; |
| 14 | +import java.util.Locale; |
| 15 | +import java.util.concurrent.Future; |
| 16 | + |
| 17 | +import org.apache.commons.lang3.ArchUtils; |
| 18 | +import org.apache.commons.lang3.BooleanUtils; |
| 19 | +import org.apache.commons.lang3.SystemUtils; |
| 20 | +import org.apache.commons.lang3.arch.Processor; |
| 21 | +import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException; |
| 22 | +import org.apache.commons.lang3.concurrent.ConcurrentUtils; |
| 23 | +import org.apache.commons.lang3.event.EventUtils; |
| 24 | +import org.apache.commons.lang3.reflect.FieldUtils; |
| 25 | +import org.apache.commons.lang3.time.FastDateFormat; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class Lang3UtilsTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void test_to_Boolean_fromString() { |
| 33 | + assertFalse(BooleanUtils.toBoolean("off")); |
| 34 | + assertTrue(BooleanUtils.toBoolean("true")); |
| 35 | + assertTrue(BooleanUtils.toBoolean("tRue")); |
| 36 | + assertFalse(BooleanUtils.toBoolean("no")); |
| 37 | + assertFalse(BooleanUtils.isTrue(Boolean.FALSE)); |
| 38 | + assertFalse(BooleanUtils.isTrue(null)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testGetUserHome() { |
| 43 | + final File dir = SystemUtils.getUserHome(); |
| 44 | + Assert.assertNotNull(dir); |
| 45 | + Assert.assertTrue(dir.exists()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetJavaHome() { |
| 50 | + final File dir = SystemUtils.getJavaHome(); |
| 51 | + Assert.assertNotNull(dir); |
| 52 | + Assert.assertTrue(dir.exists()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testProcessorArchType() { |
| 57 | + Processor processor = ArchUtils.getProcessor("x86"); |
| 58 | + assertTrue(processor.is32Bit()); |
| 59 | + assertFalse(processor.is64Bit()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testProcessorArchType64Bit() { |
| 64 | + Processor processor = ArchUtils.getProcessor("x86_64"); |
| 65 | + assertFalse(processor.is32Bit()); |
| 66 | + assertTrue(processor.is64Bit()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test(expected = IllegalArgumentException.class) |
| 70 | + public void testConcurrentRuntimeExceptionCauseError() { |
| 71 | + new ConcurrentRuntimeException("An error", new Error()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testConstantFuture_Integer() throws Exception { |
| 76 | + Future<Integer> test = ConcurrentUtils.constantFuture(5); |
| 77 | + assertTrue(test.isDone()); |
| 78 | + assertSame(5, test.get()); |
| 79 | + assertFalse(test.isCancelled()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testFieldUtilsGetAllFields() { |
| 84 | + final Field[] fieldsNumber = Number.class.getDeclaredFields(); |
| 85 | + assertArrayEquals(fieldsNumber, FieldUtils.getAllFields(Number.class)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void test_getInstance_String_Locale() { |
| 90 | + final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.US); |
| 91 | + final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY); |
| 92 | + |
| 93 | + assertNotSame(format1, format3); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testAddEventListenerThrowsException() { |
| 98 | + final ExceptionEventSource src = new ExceptionEventSource(); |
| 99 | + try { |
| 100 | + EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() { |
| 101 | + @Override |
| 102 | + public void propertyChange(final PropertyChangeEvent e) { |
| 103 | + // Do nothing! |
| 104 | + } |
| 105 | + }); |
| 106 | + fail("Add method should have thrown an exception, so method should fail."); |
| 107 | + } catch (final RuntimeException e) { |
| 108 | + |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public static class ExceptionEventSource { |
| 113 | + public void addPropertyChangeListener(final PropertyChangeListener listener) { |
| 114 | + throw new RuntimeException(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments