001package org.andromda.core.common;
002
003import static org.junit.Assert.assertEquals;
004import static org.junit.Assert.assertFalse;
005import static org.junit.Assert.assertNotNull;
006import static org.junit.Assert.assertTrue;
007import java.io.File;
008import java.io.FileInputStream;
009import java.io.InputStream;
010import java.net.URL;
011import java.util.List;
012import org.apache.commons.io.IOUtils;
013import org.junit.Test;
014
015/**
016 * JUnit test for {@link org.andromda.core.common.ResourceUtils}
017 *
018 * @author Chad Brandon
019 */
020public class ResourceUtilsTest
021{
022  /**
023   * @throws Exception
024   */
025  @Test
026  public void testGetDirectoryContentsZeroLevels()
027        throws Exception
028    {
029        URL resource = ResourceUtilsTest.class.getResource("ResourceUtilsTestDir");
030        assertNotNull(resource);
031        List resources = ResourceUtils.getDirectoryContents(resource, 0);
032        assertNotNull(resources);
033        assertFalse(resources.isEmpty());
034        assertEquals(
035            6,
036            resources.size());
037    }
038
039    /**
040     * @throws Exception
041     */
042    @Test
043    public void testGetDirectoryContentsTwoLevels()
044        throws Exception
045    {
046        URL resource = ResourceUtilsTest.class.getResource("ResourceUtilsTestDir/one/two");
047        assertNotNull(resource);
048        List resources = ResourceUtils.getDirectoryContents(resource, 2);
049        assertNotNull(resources);
050        assertFalse(resources.isEmpty());
051        assertEquals(
052            6,
053            resources.size());
054    }
055
056    /**
057     * @throws Exception
058     */
059    @Test
060    public void testResolveClasspathResource()
061        throws Exception
062    {
063        URL resource = ResourceUtils.resolveClasspathResource(
064            "classpath:" + ResourceUtilsTest.class.getPackage().getName().replace('.', '/') + '/' +
065            "ResourceUtilsTestDir/one.zip!/two.zip!/three.zip!/file.txt" );
066        assertNotNull(resource);
067    }
068
069    /**
070     * Test for ResourceUtils.writeUrlToFile method
071     * @throws Exception
072     */
073    @Test
074    public void testWriteUrlToFile()
075        throws Exception
076    {
077        URL resource = ResourceUtils.resolveClasspathResource(
078            "classpath:" + ResourceUtilsTest.class.getPackage().getName().replace('.', '/') + '/' +
079            "ResourceUtilsTestDir/backblue.gif" );
080        assertNotNull(resource);
081        final File tempFile = File.createTempFile("andromda", "writeUrlToFileTest");
082        assertNotNull(tempFile);
083        tempFile.deleteOnExit();
084
085        ResourceUtils.writeUrlToFile(resource, tempFile.getName());
086        new File(tempFile.getName()).deleteOnExit();
087
088        final InputStream inputStream = resource.openStream();
089        assertNotNull(inputStream);
090        final InputStream inputStream2 = new FileInputStream(tempFile.getName());
091        assertNotNull(inputStream2);
092
093        assertTrue(IOUtils.contentEquals(inputStream, inputStream2));
094
095        IOUtils.closeQuietly(inputStream);
096        IOUtils.closeQuietly(inputStream2);
097    }
098}