View Javadoc

1   /*
2    * Copyright 2012-2013 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.properties.resource.repository;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import de.smartics.properties.resource.domain.ArtifactId;
32  import de.smartics.properties.resource.domain.ArtifactRef;
33  import de.smartics.properties.resource.domain.ClassPathEnvironment;
34  import de.smartics.testdoc.annotations.Uut;
35  import de.smartics.testdoc.categories.type.Coverage;
36  
37  /**
38   * Tests {@link ClassPathEnvironment}.
39   */
40  public class ClassPathEnvironmentTest
41  {
42    // ********************************* Fields *********************************
43  
44    // --- constants ------------------------------------------------------------
45  
46    static final ArtifactRef DEFAULT_URL;
47  
48    static final ArtifactRef ANOTHER_ARTIFACT_REF;
49  
50    static
51    {
52      try
53      {
54        final ArtifactId.Builder builder = new ArtifactId.Builder();
55        builder.withGroupId("com.example.test").withName("one").withVersion("1.0").withArchiveType("jar");
56        final ArtifactId id1 = builder.build();
57        final ArtifactId id2 = builder.withName("two").build();
58  
59        DEFAULT_URL = new ArtifactRef(id1, new URL("http://example.com/one"));
60        ANOTHER_ARTIFACT_REF = new ArtifactRef(id2, new URL("http://example.com/two"));
61      }
62      catch (final MalformedURLException e)
63      {
64        throw new IllegalStateException("Cannot create test URL.", e);
65      }
66    }
67  
68    // --- members --------------------------------------------------------------
69  
70    @Uut
71    private ClassPathEnvironment uut;
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- prepare --------------------------------------------------------------
78  
79    @Before
80    public void setUp()
81    {
82      uut = new ClassPathEnvironment();
83    }
84  
85    // --- helper ---------------------------------------------------------------
86  
87    // --- tests ----------------------------------------------------------------
88  
89    @Test
90    public void neverRetursNullForAnRefsList()
91    {
92      final List<ArtifactRef> refs = uut.getArtifactRefs();
93  
94      assertThat(refs.isEmpty(), is(true));
95    }
96  
97    @Test(expected = UnsupportedOperationException.class)
98    public void returnedRefsListIsNotModifiable()
99    {
100     final List<ArtifactRef> refs = uut.getArtifactRefs();
101     refs.add(DEFAULT_URL);
102   }
103 
104   @Test
105   public void allowsToAddRefsToTheListOfSearchRoots()
106   {
107     uut.addArchiveArtifactRef(DEFAULT_URL);
108 
109     final List<ArtifactRef> urls = uut.getArtifactRefs();
110     final List<ArtifactRef> expected = new ArrayList<ArtifactRef>();
111     expected.add(DEFAULT_URL);
112     assertThat(urls, is(equalTo(expected)));
113   }
114 
115   @Test
116   public void orderOfAddingRefsIsPreserved()
117   {
118     uut.addArchiveArtifactRef(DEFAULT_URL);
119     uut.addArchiveArtifactRef(ANOTHER_ARTIFACT_REF);
120 
121     final List<ArtifactRef> refs = uut.getArtifactRefs();
122     final List<ArtifactRef> expected = new ArrayList<ArtifactRef>();
123     expected.add(DEFAULT_URL);
124     expected.add(ANOTHER_ARTIFACT_REF);
125     assertThat(refs, is(equalTo(expected)));
126   }
127 
128   @Test
129   public void printsClassPathByItsToString()
130   {
131     uut.addArchiveArtifactRef(DEFAULT_URL);
132     uut.addArchiveArtifactRef(ANOTHER_ARTIFACT_REF);
133 
134     final String string = uut.toString();
135     assertThat(string.length() > 0, is(true));
136   }
137 
138   @Test
139   @Category(Coverage.class)
140   public void printsClassPathByItsToStringOnAnEmptyList()
141   {
142     final String string = uut.toString();
143     assertThat(string.length() > 0, is(true));
144   }
145 }