View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package test.de.smartics.ci.config.load;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  import static org.hamcrest.Matchers.notNullValue;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.when;
23  
24  import java.io.File;
25  import java.io.FileNotFoundException;
26  
27  import org.apache.commons.io.IOUtils;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  import org.xml.sax.InputSource;
32  
33  import de.smartics.ci.config.load.LocationManager;
34  import de.smartics.ci.config.projects.Project;
35  import de.smartics.testdoc.annotations.Uut;
36  import de.smartics.testdoc.categories.type.Coverage;
37  import de.smartics.util.test.io.FileTestUtils;
38  
39  /**
40   * Tests {@link LocationManager}.
41   */
42  public class LocationManagerTest
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    // --- members --------------------------------------------------------------
49  
50    @Uut
51    private LocationManager uut;
52  
53    // ****************************** Inner Classes *****************************
54  
55    // ********************************* Methods ********************************
56  
57    // --- prepare --------------------------------------------------------------
58  
59    @Before
60    public void setUp()
61    {
62      uut = new LocationManager();
63    }
64  
65    // --- helper ---------------------------------------------------------------
66  
67    // --- tests ----------------------------------------------------------------
68  
69    @Test
70    public void allowsToAddAFileLocation() throws FileNotFoundException
71    {
72      final File location =
73          FileTestUtils.getFileFromRelativeResource(Project.class,
74              Project.GLOBAL_RESOURCES);
75      uut.addLocation(location);
76    }
77  
78    @Test(expected = NullPointerException.class)
79    public void allowsNotNullToBeAddedAsAFileLocation()
80    {
81      uut.addLocation((File) null);
82    }
83  
84    @Test(expected = FileNotFoundException.class)
85    public void signalsMissingResources() throws FileNotFoundException
86    {
87      final File location =
88          FileTestUtils.getFileFromRelativeResource(Project.class,
89              Project.GLOBAL_RESOURCES);
90      uut.addLocation(location);
91      uut.open("unknown/resource.xml");
92    }
93  
94    @Test
95    public void opensResourcesFromKnownLocations() throws FileNotFoundException
96    {
97      final File location =
98          FileTestUtils.getFileFromRelativeResource(Project.class,
99              Project.GLOBAL_RESOURCES);
100     uut.addLocation(location);
101     final InputSource source = uut.open(Project.GLOBAL_ROOT_XML);
102     assertThat(source, is(notNullValue()));
103     IOUtils.closeQuietly(source.getByteStream());
104   }
105 
106   @Test(expected = IllegalArgumentException.class)
107   @Category(Coverage.class)
108   public void rejectsInvalidFiles() throws FileNotFoundException
109   {
110     final File location = mock(File.class);
111     when(location.toURI()).thenThrow(new IllegalArgumentException("Test"));
112     uut.addLocation(location);
113   }
114 }