View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.google.common.base.Predicate;
33  import com.google.common.collect.ImmutableSet;
34  import com.google.common.collect.Iterables;
35  import java.lang.reflect.Modifier;
36  import java.util.Set;
37  import org.hamcrest.CustomTypeSafeMatcher;
38  import org.hamcrest.MatcherAssert;
39  import org.hamcrest.Matchers;
40  import org.junit.Rule;
41  import org.junit.Test;
42  
43  /**
44   * Test for visibility.
45   * Checks that there are not public classes in package
46   * {@code com.jcabi.github}. Certain types including all Smart types are
47   * excluded.
48   *
49   * @author Carlos Crespo (carlos.a.crespo@gmail.com)
50   * @version $Id: bc3f1c00652102bebd6a9f61b4702274666f7de7 $
51   */
52  public final class VisibilityTest {
53  
54      /**
55       * Set of classes/interfaces that can be public.
56       */
57      private static final Set<String> SKIP = ImmutableSet.<String>builder()
58          .add("com.jcabi.github.RtGithub")
59          .add("com.jcabi.github.Bulk")
60          .add("com.jcabi.github.Smarts")
61          .add("com.jcabi.github.wire.CarefulWire")
62          .add("com.jcabi.github.mock.MkGithub")
63          .build();
64  
65      /**
66       * ClasspathRule.
67       * @checkstyle VisibilityModifierCheck (3 lines)
68       */
69      @Rule
70      public final transient ClasspathRule classpath = new ClasspathRule();
71  
72      /**
73       * Test for visibility.
74       * Checks that there are not public classes in package
75       * {@code com.jcabi.github}.
76       *
77       * @throws Exception If some problem inside
78       */
79      @Test
80      public void checkVisibility() throws Exception {
81          MatcherAssert.assertThat(
82              Iterables.filter(
83                  this.classpath.allTypes(),
84                  new Predicate<Class<?>>() {
85                      @Override
86                      public boolean apply(final Class<?> input) {
87                          return !(
88                              input.isInterface()
89                                  || SKIP.contains(input.getName())
90                                  || (input.getEnclosingClass() != null
91                                      && input.getName().endsWith("Smart"))
92                              );
93                      }
94                  }
95              ),
96              Matchers.everyItem(
97                  new CustomTypeSafeMatcher<Class<?>>("not public type") {
98                      @Override
99                      protected boolean matchesSafely(final Class<?> item) {
100                         return !Modifier.isPublic(item.getModifiers());
101                     }
102                 }
103             )
104         );
105     }
106 
107 }