Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/res2/FileResourceNameValidatorTest.java

raw
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 com.android.ide.common.res2;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22
23 import com.android.annotations.Nullable;
24 import com.android.resources.ResourceType;
25
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29
30 import java.io.File;
31 import java.util.Arrays;
32 import java.util.Collection;
33
34 @RunWith(Parameterized.class)
35 public class FileResourceNameValidatorTest {
36
37 @Parameterized.Parameters(name = "file={0}, resourceType={1} gives error {2}")
38 public static Collection<Object[]> expected() {
39 final String IS_NOT_A_VALID_ETC =
40 " is not a valid file-based resource name character: File-based resource names "
41 + "must contain only lowercase a-z, 0-9, or underscore";
42 final String THE_FILE_NAME_MUST_END_WITH_XML_OR_PNG =
43 "The file name must end with .xml or .png";
44 return Arrays.asList(new Object[][]{
45 //{ resourceName, resourceType, sourceFile, expectedException }
46 {"", ResourceType.ANIMATOR, "Resource must have a name"},
47 {"foo.xml", ResourceType.DRAWABLE, null},
48 {"foo.XML", ResourceType.DRAWABLE, null},
49 {"foo.xML", ResourceType.DRAWABLE, null},
50 {"foo.png", ResourceType.DRAWABLE, null},
51 {"foo.9.png", ResourceType.DRAWABLE, null},
52 {"foo.gif", ResourceType.DRAWABLE, null},
53 {"foo.jpg", ResourceType.DRAWABLE, null},
54 {"foo.jpeg", ResourceType.DRAWABLE, null},
55 {"foo.bmp", ResourceType.DRAWABLE, null},
56 {"foo.webp", ResourceType.DRAWABLE, null},
57 {"foo.other.png", ResourceType.DRAWABLE, "'.'" + IS_NOT_A_VALID_ETC},
58 {"foo.xml", ResourceType.XML, null},
59 {"foo.xsd", ResourceType.XML, null},
60 {"_foo.png", ResourceType.DRAWABLE, null},
61 {"foo.png", ResourceType.XML, "The file name must end with .xml"},
62 {"foo.8.xml", ResourceType.DRAWABLE, "'.'" + IS_NOT_A_VALID_ETC},
63 {"foo", ResourceType.DRAWABLE, THE_FILE_NAME_MUST_END_WITH_XML_OR_PNG},
64 {"foo.txt", ResourceType.RAW, null},
65 {"foo", ResourceType.RAW, null},
66 {"foo", ResourceType.RAW, null},
67 {"foo.txt", ResourceType.DRAWABLE, THE_FILE_NAME_MUST_END_WITH_XML_OR_PNG},
68 {"1foo.png", ResourceType.DRAWABLE, "The resource name must start with a letter"},
69 {"Foo.png", ResourceType.DRAWABLE, "'F'" + IS_NOT_A_VALID_ETC},
70 {"foo$.png", ResourceType.DRAWABLE, "'$'" + IS_NOT_A_VALID_ETC},
71 {"bAr.png", ResourceType.DRAWABLE, "'A'" + IS_NOT_A_VALID_ETC},
72 {"enum.png", ResourceType.DRAWABLE,
73 "enum is not a valid resource name (reserved Java keyword)"},
74 });
75 }
76
77 @Parameterized.Parameter(value = 0)
78 public String mSourceFileName;
79
80 @Parameterized.Parameter(value = 1)
81 public ResourceType mResourceType;
82
83 @Parameterized.Parameter(value = 2)
84 public String mExpectedErrorMessage;
85
86
87 @Test
88 public void validate() {
89 String errorMessage = null;
90 File file = new File(mSourceFileName);
91 try {
92 FileResourceNameValidator.validate(file, mResourceType);
93 } catch (MergingException e) {
94 errorMessage = e.getMessage();
95 }
96 assertErrorMessageCorrect(mExpectedErrorMessage, errorMessage, file);
97 }
98
99 @Test
100 public void validatePartial() {
101 if (mExpectedErrorMessage == null) {
102 for (int i = 1; i <= mSourceFileName.length(); i++) {
103 String partialName = mSourceFileName.substring(0, i);
104 String actual = FileResourceNameValidator.getErrorTextForPartialName(
105 partialName, mResourceType);
106 assertNull("Was not expecting error for " + partialName, actual);
107 }
108 } else {
109 // A partial error message may or may not validate, but the full result should either
110 // be the same or there should be no error (i.e. if ther error is a missing filename);
111 String actual = FileResourceNameValidator.getErrorTextForPartialName(
112 mSourceFileName, mResourceType);
113 if (!mExpectedErrorMessage.startsWith("The file name must end with")) {
114 assertEquals(mExpectedErrorMessage, actual);
115 }
116 }
117 }
118
119
120 /* package */ static void assertErrorMessageCorrect(@Nullable String expected, @Nullable String actual,
121 @Nullable File file) {
122 if (expected == null) {
123 assertNull("Was not expecting error ", actual);
124 } else {
125 assertNotNull("Was expecting error " + expected + " but passed", actual);
126 if (file == null) {
127 assertEquals("Error: " + expected, actual);
128 } else {
129 assertEquals(file.getAbsolutePath() + ": Error: " + expected, actual);
130 }
131 }
132 }
133
134 /* package */ static void assertErrorMessageCorrect(@Nullable String expected, @Nullable String actual) {
135 if (expected == null) {
136 assertNull("Was not expecting error ", actual);
137 } else {
138 assertNotNull("Was expecting error " + expected + " but passed", actual);
139 assertEquals(expected, actual);
140 }
141 }
142 }