View Javadoc

    1   package net.sourceforge.pmd;
    2
    3   import static org.junit.Assert.assertEquals;
    4
    5   import java.io.File;
    6
    7   import junit.framework.JUnit4TestAdapter;
    8   import net.sourceforge.pmd.lang.LanguageVersion;
    9   import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
    10
    11  import org.junit.Test;
    12
    13  public class LanguageVersionDiscovererTest {
    14
    15      /**
    16       * Test on JSP file.
    17       */
    18      @Test
    19      public void testJspFile() {
    20          LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
    21          File jspFile = new File("/path/to/MyPage.jsp");
    22          LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(jspFile);
    23          assertEquals("LanguageVersion must be JSP!", LanguageVersion.JSP, languageVersion);
    24      }
    25
    26      /**
    27       * Test on Java file with default options.
    28       */
    29      @Test
    30      public void testJavaFileUsingDefaults() {
    31          LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
    32          File javaFile = new File("/path/to/MyClass.java");
    33
    34          LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(javaFile);
    35          assertEquals("LanguageVersion must be Java 1.7 !", LanguageVersion.JAVA_17, languageVersion);
    36      }
    37
    38      /**
    39       * Test on Java file with Java version set to 1.4.
    40       */
    41      @Test
    42      public void testJavaFileUsing15() {
    43          LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
    44          discoverer.setDefaultLanguageVersion(LanguageVersion.JAVA_14);
    45          File javaFile = new File("/path/to/MyClass.java");
    46
    47          LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(javaFile);
    48          assertEquals("LanguageVersion must be Java 1.4!", LanguageVersion.JAVA_14, languageVersion);
    49      }
    50
    51      public static junit.framework.Test suite() {
    52          return new JUnit4TestAdapter(LanguageVersionDiscovererTest.class);
    53      }
    54  }