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.8 !", LanguageVersion.JAVA_18, languageVersion);
36      }
37  
38      /**
39       * Test on Java file with Java version set to 1.4.
40       */
41      @Test
42      public void testJavaFileUsing14() {
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      /**
52       * Test on PLSQL file with default version
53       */
54      @Test
55      public void testPlsql() {
56          LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
57          File plsqlFile = new File("/path/to/MY_PACKAGE.sql");
58  
59          LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(plsqlFile);
60          assertEquals("LanguageVersion must be PLSQL!", LanguageVersion.PLSQL, languageVersion);
61      }
62  
63      public static junit.framework.Test suite() {
64          return new JUnit4TestAdapter(LanguageVersionDiscovererTest.class);
65      }
66  }