| /* |
| * Copyright (C) 2012 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.ide.common.res2; |
| |
| import static java.io.File.separator; |
| |
| import com.android.testutils.TestUtils; |
| |
| import java.io.File; |
| import java.io.IOException; |
| |
| public class ResourceSetTest extends BaseTestCase { |
| |
| public void testBaseResourceSetByCount() throws Exception { |
| ResourceSet resourceSet = getBaseResourceSet(); |
| assertEquals(27, resourceSet.size()); |
| } |
| |
| public void testBaseResourceSetByName() throws Exception { |
| ResourceSet resourceSet = getBaseResourceSet(); |
| |
| verifyResourceExists(resourceSet, |
| "drawable/icon", |
| "drawable/patch", |
| "raw/foo", |
| "layout/main", |
| "layout/layout_ref", |
| "layout/alias_replaced_by_file", |
| "layout/file_replaced_by_alias", |
| "drawable/color_drawable", |
| "drawable/drawable_ref", |
| "color/color", |
| "string/basic_string", |
| "string/xliff_string", |
| "string/styled_string", |
| "style/style", |
| "array/string_array", |
| "attr/dimen_attr", |
| "attr/string_attr", |
| "attr/enum_attr", |
| "attr/flag_attr", |
| "attr/blah", |
| "attr/blah2", |
| "attr/flagAttr", |
| "declare-styleable/declare_styleable", |
| "dimen/dimen", |
| "id/item_id", |
| "integer/integer", |
| "plurals/plurals" |
| ); |
| } |
| |
| public void testDupResourceSet() throws Exception { |
| File root = TestUtils.getRoot("resources", "dupSet"); |
| |
| ResourceSet set = new ResourceSet("main"); |
| set.addSource(new File(root, "res1")); |
| set.addSource(new File(root, "res2")); |
| boolean gotException = false; |
| RecordingLogger logger = new RecordingLogger(); |
| try { |
| set.loadFromFiles(logger); |
| } catch (DuplicateDataException e) { |
| gotException = true; |
| String message = e.getMessage(); |
| // Clean up paths etc for unit test |
| int index = message.indexOf("dupSet"); |
| assertTrue(index != -1); |
| String prefix = message.substring(0, index); |
| message = message.replaceAll(prefix, "<PREFIX>").replace('\\','/'); |
| assertEquals("<PREFIX>dupSet/res2/drawable/icon.png: Error: Duplicate resources: " |
| + "<PREFIX>dupSet/res2/drawable/icon.png:drawable/icon, " |
| + "<PREFIX>dupSet/res1/drawable/icon.png:drawable/icon", message); |
| } |
| |
| checkLogger(logger); |
| assertTrue(gotException); |
| } |
| |
| public void testBrokenSet() throws Exception { |
| File root = TestUtils.getRoot("resources", "brokenSet"); |
| |
| ResourceSet set = new ResourceSet("main"); |
| set.addSource(root); |
| |
| boolean gotException = false; |
| RecordingLogger logger = new RecordingLogger(); |
| try { |
| set.loadFromFiles(logger); |
| } catch (MergingException e) { |
| gotException = true; |
| assertEquals(new File(root, "values" + separator + "dimens.xml").getAbsolutePath() + |
| ":0:0: Error: Content is not allowed in prolog.", |
| e.getMessage()); |
| } |
| |
| assertTrue("ResourceSet processing should have failed, but didn't", gotException); |
| assertFalse(logger.getErrorMsgs().isEmpty()); |
| } |
| |
| public void testBrokenSet2() throws Exception { |
| File root = TestUtils.getRoot("resources", "brokenSet2"); |
| |
| ResourceSet set = new ResourceSet("main"); |
| set.addSource(root); |
| |
| boolean gotException = false; |
| RecordingLogger logger = new RecordingLogger(); |
| try { |
| set.loadFromFiles(logger); |
| } catch (MergingException e) { |
| gotException = true; |
| assertEquals(new File(root, "values" + separator + "values.xml").getAbsolutePath() + |
| ": Error: Found item String/app_name more than one time", |
| e.getMessage()); |
| } |
| |
| assertTrue("ResourceSet processing should have failed, but didn't", gotException); |
| assertFalse(logger.getErrorMsgs().isEmpty()); |
| } |
| |
| public void testBrokenSet3() throws Exception { |
| File root = TestUtils.getRoot("resources", "brokenSet3"); |
| |
| ResourceSet set = new ResourceSet("main"); |
| set.addSource(root); |
| |
| boolean gotException = false; |
| RecordingLogger logger = new RecordingLogger(); |
| try { |
| set.loadFromFiles(logger); |
| } catch (MergingException e) { |
| gotException = true; |
| assertEquals(new File(root, "values" + separator + "values.xml").getAbsolutePath() + |
| ": Error: Found item Attr/d_common_attr more than one time", |
| e.getMessage()); |
| } |
| |
| assertTrue("ResourceSet processing should have failed, but didn't", gotException); |
| assertFalse(logger.getErrorMsgs().isEmpty()); |
| } |
| |
| public void testBrokenSet4() throws Exception { |
| File root = TestUtils.getRoot("resources", "brokenSet4"); |
| |
| ResourceSet set = new ResourceSet("main"); |
| set.addSource(root); |
| |
| boolean gotException = false; |
| RecordingLogger logger = new RecordingLogger(); |
| try { |
| set.loadFromFiles(logger); |
| } catch (MergingException e) { |
| gotException = true; |
| assertEquals(new File(root, "values" + separator + "values.xml").getAbsolutePath() + |
| ":6:5: Error: The element type \"declare-styleable\" " |
| + "must be terminated by the matching end-tag \"</declare-styleable>\".", |
| e.getMessage()); |
| } |
| |
| assertTrue("ResourceSet processing should have failed, but didn't", gotException); |
| assertFalse(logger.getErrorMsgs().isEmpty()); |
| } |
| |
| static ResourceSet getBaseResourceSet() throws MergingException, IOException { |
| File root = TestUtils.getRoot("resources", "baseSet"); |
| |
| ResourceSet resourceSet = new ResourceSet("main"); |
| resourceSet.addSource(root); |
| RecordingLogger logger = new RecordingLogger(); |
| resourceSet.loadFromFiles(logger); |
| |
| checkLogger(logger); |
| |
| return resourceSet; |
| } |
| } |