--- /dev/null
+/*\r
+ * Copyright (C) 2009 The Android Open Source Project\r
+ *\r
+ * Licensed under the Eclipse Public License, Version 1.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.eclipse.org/org/documents/epl-v10.php\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.android.sdklib.internal.repository;\r
+\r
+import com.android.sdklib.internal.repository.AddonsListFetcher.Site;\r
+import com.android.sdklib.repository.SdkAddonsListConstants;\r
+\r
+import org.w3c.dom.Document;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+import junit.framework.TestCase;\r
+\r
+/**\r
+ * Tests for {@link AddonsListFetcher}\r
+ */\r
+public class AddonsListFetcherTest extends TestCase {\r
+\r
+ /**\r
+ * An internal helper class to give us visibility to the protected members we want\r
+ * to test.\r
+ */\r
+ private static class MockAddonsListFetcher extends AddonsListFetcher {\r
+\r
+ public Site[] _parseAddonsList(Document doc, String nsUri, ITaskMonitor monitor) {\r
+ return super.parseAddonsList(doc, nsUri, monitor);\r
+ }\r
+\r
+ public int _getXmlSchemaVersion(InputStream xml) {\r
+ return super.getXmlSchemaVersion(xml);\r
+ }\r
+\r
+ public String _validateXml(InputStream xml, String url, int version,\r
+ String[] outError, Boolean[] validatorFound) {\r
+ return super.validateXml(xml, url, version, outError, validatorFound);\r
+ }\r
+\r
+ public Document _getDocument(InputStream xml, ITaskMonitor monitor) {\r
+ return super.getDocument(xml, monitor);\r
+ }\r
+\r
+ }\r
+\r
+ private MockAddonsListFetcher mFetcher;\r
+\r
+ @Override\r
+ protected void setUp() throws Exception {\r
+ super.setUp();\r
+\r
+ mFetcher = new MockAddonsListFetcher();\r
+ }\r
+\r
+ @Override\r
+ protected void tearDown() throws Exception {\r
+ super.tearDown();\r
+\r
+ mFetcher = null;\r
+ }\r
+\r
+ /**\r
+ * Validate we can still load a valid addon schema version 1\r
+ */\r
+ public void testLoadSample_1() throws Exception {\r
+ InputStream xmlStream =\r
+ getTestResource("/com/android/sdklib/testdata/addons_list_sample_1.xml");\r
+\r
+ // guess the version from the XML document\r
+ int version = mFetcher._getXmlSchemaVersion(xmlStream);\r
+ assertEquals(1, version);\r
+\r
+ Boolean[] validatorFound = new Boolean[] { Boolean.FALSE };\r
+ String[] validationError = new String[] { null };\r
+ String url = "not-a-valid-url://addons_list.xml";\r
+\r
+ String uri = mFetcher._validateXml(\r
+ xmlStream, url, version, validationError, validatorFound);\r
+ assertEquals(Boolean.TRUE, validatorFound[0]);\r
+ assertEquals(null, validationError[0]);\r
+ assertEquals(SdkAddonsListConstants.getSchemaUri(1), uri);\r
+\r
+ // Validation was successful, load the document\r
+ MockMonitor monitor = new MockMonitor();\r
+ Document doc = mFetcher._getDocument(xmlStream, monitor);\r
+ assertNotNull(doc);\r
+\r
+ // Get the sites\r
+ Site[] result = mFetcher._parseAddonsList(doc, uri, monitor);\r
+\r
+ assertEquals("", monitor.getCapturedDescriptions());\r
+ assertEquals("", monitor.getCapturedResults());\r
+\r
+ // check the sites we found...\r
+ assertEquals(3, result.length);\r
+\r
+ assertEquals("My Example Add-ons.", result[0].getUiName());\r
+ assertEquals("http://www.example.com/my_addons.xml", result[0].getUrl());\r
+\r
+ // The XML file is UTF-8 so we support character sets (but the Java source file is\r
+ // not, so we use the \\u notation to create the Unicode String)\r
+ assertEquals("\u3042\u308A\u304C\u3068\u3046\u3054\u3056\u3044\u307E\u3059\u3002",\r
+ result[1].getUiName());\r
+ assertEquals("http://www.example.co.jp/addons.xml", result[1].getUrl());\r
+\r
+ assertEquals("Example of directory URL.", result[2].getUiName());\r
+ assertEquals("http://www.example.com/", result[2].getUrl());\r
+ }\r
+\r
+ /**\r
+ * Returns an SdkLib file resource as a {@link ByteArrayInputStream},\r
+ * which has the advantage that we can use {@link InputStream#reset()} on it\r
+ * at any time to read it multiple times.\r
+ * <p/>\r
+ * The default for getResourceAsStream() is to return a {@link FileInputStream} that\r
+ * does not support reset(), yet we need it in the tested code.\r
+ *\r
+ * @throws IOException if some I/O read fails\r
+ */\r
+ private ByteArrayInputStream getTestResource(String filename) throws IOException {\r
+ InputStream xmlStream = this.getClass().getResourceAsStream(filename);\r
+\r
+ try {\r
+ byte[] data = new byte[8192];\r
+ int offset = 0;\r
+ int n;\r
+\r
+ while ((n = xmlStream.read(data, offset, data.length - offset)) != -1) {\r
+ offset += n;\r
+\r
+ if (offset == data.length) {\r
+ byte[] newData = new byte[offset + 8192];\r
+ System.arraycopy(data, 0, newData, 0, offset);\r
+ data = newData;\r
+ }\r
+ }\r
+\r
+ return new ByteArrayInputStream(data, 0, offset);\r
+ } finally {\r
+ if (xmlStream != null) {\r
+ xmlStream.close();\r
+ }\r
+ }\r
+ }\r
+}\r