1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci.compilers;
19  
20  import junit.framework.TestCase;
21  
22  public final class JavacJavaCompilerSettingsTestCase extends TestCase {
23  
24  	private static void assertEquals(String[] expected, String[] result) {
25  		assertEquals(expected.length, result.length);
26  
27  		for (int i = 0; i < expected.length; i++) {
28  			assertEquals("Unexpected value at index " + i, expected[i], result[i]);
29  		}
30  	}
31  	
32  	public void testDefaultSettings() {
33  		final String[] expected = {
34  			"-nowarn",
35  			"-target", "1.4",
36  			"-source", "1.4",
37  			"-encoding", "UTF-8"				
38  		};
39  		
40  		final String[] s = new JavacJavaCompilerSettings().toNativeSettings();
41  
42  		assertEquals(expected, s);
43  	}
44  
45  	public void testSourceVersion() {
46  		final JavacJavaCompilerSettings s = new JavacJavaCompilerSettings();
47  		s.setSourceVersion("1.1");
48  		assertEquals("1.1", s.toNativeSettings()[4]);
49  		s.setSourceVersion("1.2");
50  		assertEquals("1.2", s.toNativeSettings()[4]);
51  		s.setSourceVersion("1.3");
52  		assertEquals("1.3", s.toNativeSettings()[4]);
53  		s.setSourceVersion("1.4");
54  		assertEquals("1.4", s.toNativeSettings()[4]);
55  		s.setSourceVersion("1.5");
56  		assertEquals("1.5", s.toNativeSettings()[4]);
57  		s.setSourceVersion("1.6");
58  		assertEquals("1.6", s.toNativeSettings()[4]);
59  	}
60  
61  	public void testTargetVersion() {
62  		final JavacJavaCompilerSettings s = new JavacJavaCompilerSettings();
63  		s.setTargetVersion("1.1");
64  		assertEquals("1.1", s.toNativeSettings()[2]);
65  		s.setTargetVersion("1.2");
66  		assertEquals("1.2", s.toNativeSettings()[2]);
67  		s.setTargetVersion("1.3");
68  		assertEquals("1.3", s.toNativeSettings()[2]);
69  		s.setTargetVersion("1.4");
70  		assertEquals("1.4", s.toNativeSettings()[2]);
71  		s.setTargetVersion("1.5");
72  		assertEquals("1.5", s.toNativeSettings()[2]);
73  		s.setTargetVersion("1.6");
74  		assertEquals("1.6", s.toNativeSettings()[2]);
75  	}
76  
77  	public void testEncoding() {
78  		final JavacJavaCompilerSettings s = new JavacJavaCompilerSettings();
79  		s.setSourceEncoding("ASCII");
80  		assertEquals("ASCII", s.toNativeSettings()[6]);
81  	}
82  
83  	public void testWarnings() {
84  		final JavacJavaCompilerSettings s = new JavacJavaCompilerSettings();
85  		s.setWarnings(true);
86  		assertFalse("-nowarn".equals(s.toNativeSettings()[0]));
87  		s.setWarnings(false);
88  		assertEquals("-nowarn", s.toNativeSettings()[0]);
89  
90  	}
91  
92  	public void testDeprecations() {
93  		final JavacJavaCompilerSettings s = new JavacJavaCompilerSettings();
94  		s.setDeprecations(true);
95  		assertEquals("-deprecation", s.toNativeSettings()[0]);
96  		s.setDeprecations(false);
97  		assertFalse("-deprecation".equals(s.toNativeSettings()[0]));
98  	}
99  }