001: package liquibase;
002:
003: import liquibase.CompositeFileOpener;
004: import liquibase.FileOpener;
005: import static org.easymock.EasyMock.*;
006: import org.junit.After;
007: import static org.junit.Assert.*;
008: import org.junit.Before;
009: import org.junit.Test;
010:
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.net.URL;
014: import java.util.Enumeration;
015: import java.util.Vector;
016:
017: public class CompositeFileOpenerTest {
018: FileOpener first;
019: FileOpener second;
020: FileOpener hasDir;
021: FileOpener noDir;
022: CompositeFileOpener composite;
023: InputStream validStream;
024: Enumeration<URL> empty;
025: Enumeration<URL> hasElements;
026:
027: @Before
028: public void setUp() throws IOException {
029: first = createMock(FileOpener.class);
030: second = createMock(FileOpener.class);
031: composite = new CompositeFileOpener(first, second);
032: validStream = this .getClass().getClassLoader()
033: .getResourceAsStream(
034: "liquibase/CompositeFileOpenerTest.class");
035:
036: empty = new Vector<URL>().elements();
037: hasElements = this .getClass().getClassLoader().getResources(
038: "liquibase");
039: }
040:
041: @After
042: public void tearDown() throws IOException {
043: validStream.close();
044:
045: }
046:
047: @Test
048: public void streamFirstHas() throws IOException {
049: expect(first.getResourceAsStream("file"))
050: .andReturn(validStream);
051: replay(first);
052: replay(second);
053: InputStream is = composite.getResourceAsStream("file");
054: assertEquals(validStream, is);
055: verify(first);
056: verify(second);
057: }
058:
059: @Test
060: public void streamSecondHas() throws IOException {
061: expect(first.getResourceAsStream("file")).andReturn(null);
062: expect(second.getResourceAsStream("file")).andReturn(
063: validStream);
064: replay(first);
065: replay(second);
066: InputStream is = composite.getResourceAsStream("file");
067: assertEquals(validStream, is);
068: verify(first);
069: verify(second);
070: }
071:
072: @Test
073: public void streamNeitherHas() throws IOException {
074: expect(first.getResourceAsStream("file")).andReturn(null);
075: expect(second.getResourceAsStream("file")).andReturn(null);
076: replay(first);
077: replay(second);
078: InputStream is = composite.getResourceAsStream("file");
079: assertNull(is);
080: verify(first);
081: verify(second);
082: }
083:
084: @Test
085: public void resourcesFirstHas() throws IOException {
086: expect(first.getResources("file")).andReturn(hasElements);
087: replay(first);
088: replay(second);
089: Enumeration<URL> urls = composite.getResources("file");
090: assertEquals(hasElements, urls);
091: verify(first);
092: verify(second);
093: }
094:
095: @Test
096: public void resourcesSecondHas() throws IOException {
097: expect(first.getResources("file")).andReturn(empty);
098: expect(second.getResources("file")).andReturn(hasElements);
099: replay(first);
100: replay(second);
101: Enumeration<URL> urls = composite.getResources("file");
102: assertEquals(hasElements, urls);
103: verify(first);
104: verify(second);
105: }
106:
107: @Test
108: public void resourcesNeitherHas() throws IOException {
109: expect(first.getResources("file")).andReturn(empty);
110: expect(second.getResources("file")).andReturn(empty);
111: replay(first);
112: replay(second);
113: Enumeration<URL> urls = composite.getResources("file");
114: assertFalse(urls.hasMoreElements());
115: assertFalse(urls == empty);
116: verify(first);
117: verify(second);
118: }
119: }
|