19a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin/*
29a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * Copyright (C) 2015 The Android Open Source Project
39a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin *
49a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * Licensed under the Apache License, Version 2.0 (the "License");
59a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * you may not use this file except in compliance with the License.
69a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * You may obtain a copy of the License at
79a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin *
89a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin *      http://www.apache.org/licenses/LICENSE-2.0
99a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin *
109a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * Unless required by applicable law or agreed to in writing, software
119a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * distributed under the License is distributed on an "AS IS" BASIS,
129a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * See the License for the specific language governing permissions and
149a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin * limitations under the License.
159a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin */
169a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin
17411cb1fb67241125203629229600ecbd821eb9c7Paul Duffinpackage vogar;
18411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
19411cb1fb67241125203629229600ecbd821eb9c7Paul Duffinimport junit.framework.Test;
20411cb1fb67241125203629229600ecbd821eb9c7Paul Duffinimport junit.framework.TestCase;
21411cb1fb67241125203629229600ecbd821eb9c7Paul Duffinimport junit.framework.TestSuite;
22411cb1fb67241125203629229600ecbd821eb9c7Paul Duffinimport vogar.Target.ScriptBuilder;
23411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
24411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin/**
25411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin * Tests the {@link ScriptBuilder#escape(String)} method.
26411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin */
27411cb1fb67241125203629229600ecbd821eb9c7Paul Duffinpublic class ScriptBuilderEscapingTest {
28411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
29411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin    public static Test suite() {
30411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        TestSuite suite = new TestSuite(ScriptBuilderEscapingTest.class.getName());
31411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        char[] chars = " '\"<>&|$".toCharArray();
32411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        for (char c : chars) {
33411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            suite.addTest(new SingleCharacterEscapeTest(c));
34411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        }
35411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        suite.addTest(new MixedCharacterEscapeTest());
36411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        return suite;
37411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin    }
38411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
39411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin    private static class SingleCharacterEscapeTest extends TestCase {
40411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
41411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        private final String uc;
42411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        private final String qc;
43411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
44411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        public SingleCharacterEscapeTest(char c) {
45411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            this.uc = Character.toString(c);
46411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            this.qc = "\\" + c;
47411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            setName("Escape '" + uc + "' as '" + qc + "'");
48411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        }
49411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
50411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        @Override
51411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        protected void runTest() throws Throwable {
52411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            assertEquals(qc, ScriptBuilder.escape(uc));
53411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            assertEquals("a" + qc, ScriptBuilder.escape("a" + uc));
54411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            assertEquals(qc + "b", ScriptBuilder.escape(uc + "b"));
55411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            assertEquals("a" + qc + "b", ScriptBuilder.escape("a" + uc + "b"));
56411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            assertEquals(qc + "a" + qc + qc + qc + "b" + qc,
57411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin                    ScriptBuilder.escape(uc + "a" + uc + uc + uc + "b" + uc));
58411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
59411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        }
60411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin    }
61411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
62411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin    private static class MixedCharacterEscapeTest extends TestCase {
63411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
64411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        public MixedCharacterEscapeTest() {
65411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            super("mixed character escape test");
66411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        }
67411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin
68411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        @Override
69411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        protected void runTest() throws Throwable {
70411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin            assertEquals("\\ \\'\\\"\\<\\>\\&\\|\\$",
71411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin                    ScriptBuilder.escape(" '\"<>&|$"));
72411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin        }
73411cb1fb67241125203629229600ecbd821eb9c7Paul Duffin    }
749a8263b4e14fe4d9498db2c44130eae47695444cPaul Duffin}
75