MiscUtilsTest.java :  » Utilities » autils » sk » baka » autils » Android Open Source

Android Open Source » Utilities » autils 
autils » sk » baka » autils » MiscUtilsTest.java
/**
 *     AUtils - A collection of utility classes for the Android system.
Copyright (C) 2009 Martin Vysny

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.baka.autils;

import java.io.Closeable;
import java.io.IOException;
import java.util.Set;
import org.junit.Test;
import org.mockito.Mockito;
import sk.baka.autils.bind.ComplexEnum;
import sk.baka.autils.bind.SimpleEnum;
import static org.junit.Assert.*;

/**
 * Tests the {@link MiscUtils} class.
 * @author Martin Vysny
 */
public class MiscUtilsTest {

    @Test
    public void testCloseQuietly() throws IOException {
        MiscUtils.closeQuietly((Closeable) null);
        final Closeable mock = Mockito.mock(Closeable.class);
        MiscUtils.closeQuietly(mock);
        Mockito.verify(mock).close();
        Mockito.verifyNoMoreInteractions(mock);
        // we cannot test the case when close() throws an exception: an attempt
        // to log this exception via android Log logger will fail with a STUB exception.
    }

    @Test
    public void testGetStacktrace() {
        final String stacktrace = MiscUtils.getStacktrace(new RuntimeException("Simulated failure"));
        assertContains("Simulated failure", stacktrace);
    }

    public static void assertContains(String expected, String containedInHere) {
        if (!containedInHere.contains(expected)) {
            fail("The string '" + containedInHere + "' is expected to contain '" + expected + "'");
        }
    }

    @Test
    public void testEnumClasses() {
        assertTrue(MiscUtils.isEnum(Enum.class));
        assertTrue(MiscUtils.isEnum(SimpleEnum.class));
        assertTrue(MiscUtils.isEnum(ComplexEnum.class));
    }

    @Test
    public void testNonEnumClasses() {
        assertFalse(MiscUtils.isEnum(Object.class));
        assertFalse(MiscUtils.isEnum(String.class));
        assertFalse(MiscUtils.isEnum(Set.class));
    }

    @Test
    public void testRemoveWhitespaces() {
        assertEquals("", MiscUtils.removeWhitespaces(""));
        assertEquals("", MiscUtils.removeWhitespaces(" \t\n\n   "));
        assertEquals("FOO", MiscUtils.removeWhitespaces("FOO"));
        assertEquals("FOO", MiscUtils.removeWhitespaces("    FOO\n  \n"));
        assertEquals("FOO", MiscUtils.removeWhitespaces("    F\n\nO\n\nO    "));
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.