1 /*******************************************************************************
2  * Copyright (c) 2009, 2018 Tasktop Technologies and others.
3  *
4  * This pro8gram and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Tasktop Technologies - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.equinox.p2.discovery.tests.core;
16 
17 import static org.junit.Assert.*;
18 
19 import java.io.IOException;
20 import java.io.StringReader;
21 import org.eclipse.equinox.internal.p2.discovery.compatibility.Directory;
22 import org.eclipse.equinox.internal.p2.discovery.compatibility.DirectoryParser;
23 import org.junit.Before;
24 import org.junit.Test;
25 
26 /**
27  * @author David Green
28  */
29 public class DirectoryParserTest {
30 
31 	private DirectoryParser parser;
32 
33 	@Before
setUp()34 	public void setUp() {
35 		parser = new DirectoryParser();
36 	}
37 
38 	@Test
testParse()39 	public void testParse() throws IOException {
40 		Directory directory = parser.parse(new StringReader(
41 				"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\"/></directory>")); //$NON-NLS-1$
42 		assertNotNull(directory);
43 		assertEquals(1, directory.getEntries().size());
44 		assertEquals("http://foo.bar.nodomain/baz.jar", directory.getEntries().get(0).getLocation()); //$NON-NLS-1$
45 	}
46 
47 	@Test
testParseBadFormat()48 	public void testParseBadFormat() {
49 		try {
50 			parser.parse(new StringReader(
51 					"<directory2 xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\"/></directory2>")); //$NON-NLS-1$
52 			fail("Expected exception"); //$NON-NLS-1$
53 		} catch (IOException e) {
54 			// expected
55 		}
56 	}
57 
58 	@Test
testParseMalformed()59 	public void testParseMalformed() {
60 		try {
61 			parser.parse(new StringReader(
62 					"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\">")); //$NON-NLS-1$
63 			fail("Expected exception"); //$NON-NLS-1$
64 		} catch (IOException e) {
65 			// expected
66 		}
67 	}
68 
69 	@Test
testParseUnexpectedElementsAndAttributes()70 	public void testParseUnexpectedElementsAndAttributes() throws IOException {
71 		Directory directory = parser.parse(new StringReader(
72 				"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\" id=\"asdf\"><baz/></entry><foo/></directory>")); //$NON-NLS-1$
73 		assertNotNull(directory);
74 		assertEquals(1, directory.getEntries().size());
75 		assertEquals("http://foo.bar.nodomain/baz.jar", directory.getEntries().get(0).getLocation()); //$NON-NLS-1$
76 	}
77 
78 	@Test
testParseNoNS()79 	public void testParseNoNS() throws IOException {
80 		Directory directory = parser
81 				.parse(new StringReader("<directory><entry url=\"http://foo.bar.nodomain/baz.jar\"/></directory>")); //$NON-NLS-1$
82 		assertNotNull(directory);
83 		assertEquals(1, directory.getEntries().size());
84 		assertEquals("http://foo.bar.nodomain/baz.jar", directory.getEntries().get(0).getLocation()); //$NON-NLS-1$
85 	}
86 
87 	@Test
testParsePermitCategoriesTrue()88 	public void testParsePermitCategoriesTrue() throws IOException {
89 		Directory directory = parser.parse(new StringReader(
90 				"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\" permitCategories=\"true\"/></directory>")); //$NON-NLS-1$
91 		assertNotNull(directory);
92 		assertEquals(1, directory.getEntries().size());
93 		assertEquals(true, directory.getEntries().get(0).isPermitCategories());
94 	}
95 
96 	@Test
testParsePermitCategoriesFalse()97 	public void testParsePermitCategoriesFalse() throws IOException {
98 		Directory directory = parser.parse(new StringReader(
99 				"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\" permitCategories=\"false\"/></directory>")); //$NON-NLS-1$
100 		assertNotNull(directory);
101 		assertEquals(1, directory.getEntries().size());
102 		assertEquals(false, directory.getEntries().get(0).isPermitCategories());
103 	}
104 
105 	@Test
testParsePermitCategoriesNotSpecified()106 	public void testParsePermitCategoriesNotSpecified() throws IOException {
107 		Directory directory = parser.parse(new StringReader(
108 				"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\"/></directory>")); //$NON-NLS-1$
109 		assertNotNull(directory);
110 		assertEquals(1, directory.getEntries().size());
111 		assertEquals(false, directory.getEntries().get(0).isPermitCategories());
112 	}
113 
114 	@Test
testParsePermitCategoriesSpecifiedBadly()115 	public void testParsePermitCategoriesSpecifiedBadly() throws IOException {
116 		Directory directory = parser.parse(new StringReader(
117 				"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\" permitCategories=\"\"/></directory>")); //$NON-NLS-1$
118 		assertNotNull(directory);
119 		assertEquals(1, directory.getEntries().size());
120 		assertEquals(false, directory.getEntries().get(0).isPermitCategories());
121 	}
122 
123 	@Test
testParsePermitCategoriesSpecifiedBadly2()124 	public void testParsePermitCategoriesSpecifiedBadly2() throws IOException {
125 		Directory directory = parser.parse(new StringReader(
126 				"<directory xmlns=\"http://www.eclipse.org/mylyn/discovery/directory/\"><entry url=\"http://foo.bar.nodomain/baz.jar\" permitCategories=\"asdf\"/></directory>")); //$NON-NLS-1$
127 		assertNotNull(directory);
128 		assertEquals(1, directory.getEntries().size());
129 		assertEquals(false, directory.getEntries().get(0).isPermitCategories());
130 	}
131 }
132