View Javadoc

1   package liquibase.resource;
2   
3   import static org.easymock.EasyMock.*;
4   import org.junit.After;
5   import static org.junit.Assert.*;
6   import org.junit.Before;
7   import org.junit.Test;
8   
9   import java.io.IOException;
10  import java.io.InputStream;
11  import java.net.URL;
12  import java.util.Enumeration;
13  import java.util.Vector;
14  
15  public class CompositeFileOpenerTest {
16      ResourceAccessor first;
17      ResourceAccessor second;
18      ResourceAccessor hasDir;
19      ResourceAccessor noDir;
20      CompositeResourceAccessor composite;
21      InputStream validStream;
22      Enumeration<URL> empty;
23      Enumeration<URL> hasElements;
24      
25      @Before
26      public void setUp() throws IOException {
27          first = createMock(ResourceAccessor.class);
28          second = createMock(ResourceAccessor.class);
29          composite = new CompositeResourceAccessor(first,second);
30          validStream = this.getClass().getClassLoader().getResourceAsStream("liquibase/resource/CompositeFileOpenerTest.class");
31          
32          empty = new Vector<URL>().elements();
33          hasElements = this.getClass().getClassLoader().getResources("liquibase");
34      }
35      
36      @After
37      public void tearDown() throws IOException {
38          validStream.close();
39          
40      }
41      
42      @Test
43      public void streamFirstHas() throws IOException {
44          expect(first.getResourceAsStream("file")).andReturn(validStream);
45          replay(first);
46          replay(second);
47          InputStream is = composite.getResourceAsStream("file");
48          assertEquals(validStream,is);
49          verify(first);
50          verify(second);
51      }
52      
53      @Test
54      public void streamSecondHas() throws IOException {
55          expect(first.getResourceAsStream("file")).andReturn(null);
56          expect(second.getResourceAsStream("file")).andReturn(validStream);
57          replay(first);
58          replay(second);
59          InputStream is = composite.getResourceAsStream("file");
60          assertEquals(validStream,is);
61          verify(first);
62          verify(second);
63      }
64      
65      @Test
66      public void streamNeitherHas() throws IOException {
67          expect(first.getResourceAsStream("file")).andReturn(null);
68          expect(second.getResourceAsStream("file")).andReturn(null);
69          replay(first);
70          replay(second);
71          InputStream is = composite.getResourceAsStream("file");
72          assertNull(is);
73          verify(first);
74          verify(second);
75      }
76      
77      @Test
78      public void resourcesFirstHas() throws IOException {
79          expect(first.getResources("file")).andReturn(hasElements);
80          //expect(second.getResources("file")).andReturn(empty);
81          replay(first);
82          replay(second);
83          Enumeration<URL> urls = composite.getResources("file");
84  //        assertEquals(hasElements,urls);
85          verify(first);
86          verify(second);
87      }
88      
89      @Test
90      public void resourcesSecondHas() throws IOException {
91          expect(first.getResources("file")).andReturn(empty);
92          expect(second.getResources("file")).andReturn(hasElements);
93          replay(first);
94          replay(second);
95          Enumeration<URL> urls = composite.getResources("file");
96  //        assertEquals(hasElements,urls);
97          verify(first);
98          verify(second);
99      }
100     
101     @Test
102     public void resourcesNeitherHas() throws IOException {
103         expect(first.getResources("file")).andReturn(empty);
104         expect(second.getResources("file")).andReturn(empty);
105         replay(first);
106         replay(second);
107         Enumeration<URL> urls = composite.getResources("file");
108         assertFalse(urls.hasMoreElements());
109         assertFalse(urls == empty);
110         verify(first);
111         verify(second);
112     }
113 }