View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import static org.junit.Assert.assertEquals;
7   import net.sourceforge.pmd.ant.SourceLanguage;
8   import net.sourceforge.pmd.lang.Language;
9   import net.sourceforge.pmd.lang.LanguageRegistry;
10  import net.sourceforge.pmd.lang.LanguageVersion;
11  
12  import org.junit.Test;
13  import org.junit.runner.RunWith;
14  import org.junit.runners.Parameterized;
15  
16  /**
17   * Base test class for {@link LanguageVersion} implementations.
18   * <br>Each language implementation should subclass this and provide a data method.
19   * <pre>
20   * @Parameters
21   *     public static Collection<Object[]> data() {
22   *       return Arrays.asList(new Object[][] {
23   *            { MyLanguageModule.NAME, MyLanguageModule.TERSE_NAME, "1.1",
24   *              LanguageRegistry.getLanguage(MyLanguageModule.NAME).getVersion("1.1") },
25   *            { MyLanguageModule.NAME, MyLanguageModule.TERSE_NAME, "1.2",
26   *              LanguageRegistry.getLanguage(MyLanguageModule.NAME).getVersion("1.2") },
27   *
28   *            // doesn't exist
29   *            { MyLanguageModule.NAME, MyLanguageModule.TERSE_NAME, "1.3",
30   *              null }
31   *       });
32   * </pre>
33   * For the parameters, see the constructor {@link #AbstractLanguageVersionTest(String, String, String, LanguageVersion)}.
34   */
35  @RunWith(Parameterized.class)
36  public class AbstractLanguageVersionTest {
37  
38      private String name;
39      private String version;
40      private String terseName;
41      private LanguageVersion expected;
42  
43      /**
44       * Creates a new {@link AbstractLanguageVersionTest}
45       * @param name the name under which the language module is registered
46       * @param terseName the terse name under which the language module is registered
47       * @param version the specific version of the language version
48       * @param expected the expected {@link LanguageVersion} instance
49       */
50      public AbstractLanguageVersionTest(String name, String terseName, String version, LanguageVersion expected) {
51          this.name = name;
52          this.version = version;
53          this.terseName = terseName;
54          if (version != null && !version.isEmpty()) {
55              this.terseName += " " + version;
56          }
57          this.expected = expected;
58      }
59  
60      /**
61       * Checks that the expected {@link LanguageVersion} can be found by the combination of
62       * {@link #terseName} and {@link #version}.
63       */
64      @Test
65      public void testGetLanguageVersionForTerseName() {
66          assertEquals(expected, LanguageRegistry.findLanguageVersionByTerseName(terseName));
67      }
68  
69      /**
70       * Checks that the expected {@link LanguageVersion} can be found via {@link #name} and {@link #version}.
71       */
72      @Test
73      public void testFindVersionsForLanguageNameAndVersion() {
74          SourceLanguage sourceLanguage = new SourceLanguage();
75          sourceLanguage.setName(name);
76          sourceLanguage.setVersion(version);
77  
78          Language language = LanguageRegistry.getLanguage(sourceLanguage.getName());
79          LanguageVersion languageVersion = null;
80          if(language != null) {
81              languageVersion = language.getVersion(sourceLanguage.getVersion());
82          }
83  
84          assertEquals(expected, languageVersion);
85      }
86  }