• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package vogar;
18 
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import vogar.Target.ScriptBuilder;
23 
24 /**
25  * Tests the {@link ScriptBuilder#escape(String)} method.
26  */
27 public class ScriptBuilderEscapingTest {
28 
suite()29     public static Test suite() {
30         TestSuite suite = new TestSuite(ScriptBuilderEscapingTest.class.getName());
31         char[] chars = " '\"<>&|$".toCharArray();
32         for (char c : chars) {
33             suite.addTest(new SingleCharacterEscapeTest(c));
34         }
35         suite.addTest(new MixedCharacterEscapeTest());
36         return suite;
37     }
38 
39     private static class SingleCharacterEscapeTest extends TestCase {
40 
41         private final String uc;
42         private final String qc;
43 
SingleCharacterEscapeTest(char c)44         public SingleCharacterEscapeTest(char c) {
45             this.uc = Character.toString(c);
46             this.qc = "\\" + c;
47             setName("Escape '" + uc + "' as '" + qc + "'");
48         }
49 
50         @Override
runTest()51         protected void runTest() throws Throwable {
52             assertEquals(qc, ScriptBuilder.escape(uc));
53             assertEquals("a" + qc, ScriptBuilder.escape("a" + uc));
54             assertEquals(qc + "b", ScriptBuilder.escape(uc + "b"));
55             assertEquals("a" + qc + "b", ScriptBuilder.escape("a" + uc + "b"));
56             assertEquals(qc + "a" + qc + qc + qc + "b" + qc,
57                     ScriptBuilder.escape(uc + "a" + uc + uc + uc + "b" + uc));
58 
59         }
60     }
61 
62     private static class MixedCharacterEscapeTest extends TestCase {
63 
MixedCharacterEscapeTest()64         public MixedCharacterEscapeTest() {
65             super("mixed character escape test");
66         }
67 
68         @Override
runTest()69         protected void runTest() throws Throwable {
70             assertEquals("\\ \\'\\\"\\<\\>\\&\\|\\$",
71                     ScriptBuilder.escape(" '\"<>&|$"));
72         }
73     }
74 }
75