001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.parser.xml;
019:
020: import java.io.BufferedReader;
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.File;
024: import java.io.FileReader;
025: import java.io.InputStreamReader;
026: import java.net.URL;
027: import java.util.Arrays;
028: import java.util.Date;
029: import java.util.GregorianCalendar;
030: import java.util.HashMap;
031: import java.util.List;
032: import java.util.Map;
033:
034: import junit.framework.TestCase;
035:
036: import org.apache.ivy.Ivy;
037: import org.apache.ivy.core.module.descriptor.Artifact;
038: import org.apache.ivy.core.module.descriptor.Configuration;
039: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
040: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
041: import org.apache.ivy.core.module.id.ModuleRevisionId;
042: import org.apache.ivy.core.settings.IvySettings;
043: import org.apache.ivy.plugins.repository.BasicResource;
044: import org.apache.ivy.util.FileUtil;
045: import org.xml.sax.SAXParseException;
046:
047: public class XmlModuleUpdaterTest extends TestCase {
048:
049: public void testUpdate() throws Exception {
050: /*
051: * For updated file to be equals to updated.xml, we have to fix the line separator to the
052: * one used in updated.xml, in order for this test to works in all platforms (default line
053: * separator used in updater being platform dependent
054: */
055: XmlModuleDescriptorUpdater.LINE_SEPARATOR = "\n";
056: File dest = new File("build/updated-test.xml");
057: dest.deleteOnExit();
058: Map resolvedRevisions = new HashMap();
059: resolvedRevisions.put(ModuleRevisionId.newInstance("yourorg",
060: "yourmodule2", "2+"), "2.5");
061: resolvedRevisions.put(ModuleRevisionId.newInstance("yourorg",
062: "yourmodule6", "latest.integration"), "6.3");
063:
064: GregorianCalendar cal = new GregorianCalendar();
065: cal.set(2005, 2, 22, 14, 32, 54);
066:
067: Ivy ivy = Ivy.newInstance();
068: ivy.setVariable("myvar", "myconf1");
069: XmlModuleDescriptorUpdater.update(ivy.getSettings(),
070: XmlModuleUpdaterTest.class
071: .getResource("test-update.xml"), dest,
072: resolvedRevisions, "release", "mynewrev",
073: cal.getTime(), null, true, null);
074:
075: assertTrue(dest.exists());
076: String expected = FileUtil.readEntirely(new BufferedReader(
077: new InputStreamReader(XmlModuleUpdaterTest.class
078: .getResourceAsStream("updated.xml"))));
079: String updated = FileUtil.readEntirely(new BufferedReader(
080: new FileReader(dest)));
081: assertEquals(expected, updated);
082: }
083:
084: public void testVariableReplacement() throws Exception {
085: /*
086: * For updated file to be equals to updated.xml, we have to fix the line separator to the
087: * one used in updated.xml, in order for this test to works in all platforms (default line
088: * separator used in updater being platform dependent
089: */
090: XmlModuleDescriptorUpdater.LINE_SEPARATOR = "\n";
091: File dest = new File("build/updated-test2.xml");
092: dest.deleteOnExit();
093: Map resolvedRevisions = new HashMap();
094: resolvedRevisions.put(ModuleRevisionId.newInstance("yourorg",
095: "yourmodule2", "2+"), "2.5");
096: resolvedRevisions.put(ModuleRevisionId.newInstance("yourorg",
097: "yourmodule6", "latest.integration"), "6.3");
098:
099: GregorianCalendar cal = new GregorianCalendar();
100: cal.set(2005, 2, 22, 14, 32, 54);
101:
102: Ivy ivy = Ivy.newInstance();
103: ivy.setVariable("myorg", "myorg");
104: ivy.setVariable("mymodule", "mymodule");
105: ivy.setVariable("myrev", "myrev");
106: ivy.setVariable("mystatus", "integration");
107: ivy.setVariable("mypubdate", "20050322143254");
108: ivy.setVariable("mylicense", "MyLicense");
109: ivy.setVariable("mylicenseurl",
110: "http://www.my.org/mymodule/mylicense.html");
111: ivy.setVariable("myorgurl", "http://www.myorg.org/");
112: ivy.setVariable("ivyrep", "ivyrep");
113: ivy.setVariable("ivyrepurl",
114: "http://www.jayasoft.fr/org/ivyrep/");
115: ivy.setVariable("ivyreppattern",
116: "[organisation]/[module]/ivy-[revision].xml");
117: ivy.setVariable("ivys", "true");
118: ivy.setVariable("artifacts", "false");
119: ivy.setVariable("homepage", "http://www.my.org/mymodule/");
120: ivy.setVariable("includefile",
121: "imported-configurations-with-mapping.xml");
122: ivy.setVariable("mydesc", "desc 1");
123: ivy.setVariable("visibility", "public");
124: ivy.setVariable("myvar", "myconf1");
125: ivy.setVariable("deprecated", "20050115");
126: ivy.setVariable("myartifact1", "myartifact1");
127: ivy.setVariable("mytype", "jar");
128: ivy.setVariable("mymodule2", "mymodule2");
129: ivy.setVariable("mymodule2rev", "2.0");
130: ivy.setVariable("changing", "true");
131: ivy.setVariable("transitive", "false");
132: ivy.setVariable("targetconf", "yourconf1");
133: ivy.setVariable("art9-1", "yourartifact9-1");
134: ivy.setVariable("conf3", "myconf3");
135: ivy.setVariable("includename", "your.*");
136: ivy.setVariable("includeext", "xml");
137: ivy.setVariable("excludename", "toexclude");
138: ivy.setVariable("excludemodule", "*servlet*");
139: ivy.setVariable("excludematcher", "glob");
140: ivy.setVariable("excludeorg", "acme");
141: ivy.setVariable("excludeartifact", "test");
142: ivy.setVariable("excludetype", "source");
143: ivy.setVariable("yourorg", "yourorg");
144: ivy.setVariable("yourmodule", ".*");
145: ivy.setVariable("all", "all");
146: ivy.setVariable("regexp", "regexp");
147: ivy.setVariable("theirrev", "1.0, 1.1");
148:
149: XmlModuleDescriptorUpdater.update(ivy.getSettings(),
150: XmlModuleUpdaterTest.class
151: .getResource("test-update-withvar.xml"), dest,
152: resolvedRevisions, "release", "mynewrev",
153: cal.getTime(), null, true, null);
154:
155: assertTrue(dest.exists());
156: String expected = FileUtil.readEntirely(new BufferedReader(
157: new InputStreamReader(XmlModuleUpdaterTest.class
158: .getResourceAsStream("updated.xml"))));
159: String updated = FileUtil.readEntirely(new BufferedReader(
160: new FileReader(dest)));
161: assertEquals(expected, updated);
162: }
163:
164: public void testUpdateWithImportedMappingOverride()
165: throws Exception {
166: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
167: URL settingsUrl = new File(
168: "test/java/org/apache/ivy/plugins/parser/xml/"
169: + "test-configurations-import4.xml").toURL();
170: XmlModuleDescriptorUpdater.update(new IvySettings(),
171: settingsUrl, buffer, new HashMap(), "release",
172: "mynewrev", new Date(), null, true, null);
173:
174: String updatedXml = buffer.toString();
175:
176: // just make sure that 'confmappingoverride="true"' is declared somewhere in the XML.
177: assertTrue(
178: "Updated XML doesn't define the confmappingoverride attribute",
179: updatedXml.indexOf("confmappingoverride=\"true\"") != -1);
180: }
181:
182: public void testUpdateWithExcludeConfigurations1() throws Exception {
183: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
184: URL settingsUrl = new File(
185: "test/java/org/apache/ivy/plugins/parser/xml/"
186: + "test-update-excludedconfs1.xml").toURL();
187: XmlModuleDescriptorUpdater.update(new IvySettings(),
188: settingsUrl, buffer, new HashMap(), "release",
189: "mynewrev", new Date(), null, true,
190: new String[] { "myconf2" });
191:
192: XmlModuleDescriptorParser parser = XmlModuleDescriptorParser
193: .getInstance();
194: ModuleDescriptor updatedMd = parser.parseDescriptor(
195: new IvySettings(), new ByteArrayInputStream(buffer
196: .toByteArray()), new BasicResource("test",
197: false, 0, 0, false), true);
198:
199: // test the number of configurations
200: Configuration[] configs = updatedMd.getConfigurations();
201: assertNotNull("Configurations shouldn't be null", configs);
202: assertEquals("Number of configurations incorrect", 3,
203: configs.length);
204:
205: // test that the correct configuration has been removed
206: assertNull("myconf2 hasn't been removed", updatedMd
207: .getConfiguration("myconf2"));
208:
209: // test that the other configurations aren't removed
210: assertNotNull("myconf1 has been removed", updatedMd
211: .getConfiguration("myconf1"));
212: assertNotNull("myconf3 has been removed", updatedMd
213: .getConfiguration("myconf3"));
214: assertNotNull("myconf4 has been removed", updatedMd
215: .getConfiguration("myconf4"));
216: }
217:
218: public void testUpdateWithExcludeConfigurations2() throws Exception {
219: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
220: URL settingFile = new File(
221: "test/java/org/apache/ivy/plugins/parser/xml/"
222: + "test-update-excludedconfs2.xml").toURL();
223: try {
224: XmlModuleDescriptorUpdater.update(new IvySettings(),
225: settingFile, buffer, new HashMap(), "release",
226: "mynewrev", new Date(), null, true,
227: new String[] { "myconf2" });
228: fail("IllegalArgumentException hasn't been thrown");
229: } catch (IllegalArgumentException e) {
230: // this is ok
231: } catch (SAXParseException e) {
232: // this is ok too
233: }
234: }
235:
236: public void testUpdateWithExcludeConfigurations3() throws Exception {
237: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
238: URL settingsUrl = new File(
239: "test/java/org/apache/ivy/plugins/parser/xml/"
240: + "test-update-excludedconfs3.xml").toURL();
241:
242: XmlModuleDescriptorUpdater.update(new IvySettings(),
243: settingsUrl, buffer, new HashMap(), "release",
244: "mynewrev", new Date(), null, true, new String[] {
245: "myconf2", "conf2" });
246:
247: XmlModuleDescriptorParser parser = XmlModuleDescriptorParser
248: .getInstance();
249: ModuleDescriptor updatedMd = parser.parseDescriptor(
250: new IvySettings(), new ByteArrayInputStream(buffer
251: .toByteArray()), new BasicResource("test",
252: false, 0, 0, false), true);
253:
254: // test the number of configurations
255: Configuration[] configs = updatedMd.getConfigurations();
256: assertNotNull("Configurations shouldn't be null", configs);
257: assertEquals("Number of configurations incorrect", 4,
258: configs.length);
259:
260: // test that the correct configuration has been removed
261: assertNull("myconf2 hasn't been removed", updatedMd
262: .getConfiguration("myconf2"));
263: assertNull("conf2 hasn't been removed", updatedMd
264: .getConfiguration("conf2"));
265:
266: // test that the other configurations aren't removed
267: assertNotNull("conf1 has been removed", updatedMd
268: .getConfiguration("conf1"));
269: assertNotNull("myconf1 has been removed", updatedMd
270: .getConfiguration("myconf1"));
271: assertNotNull("myconf3 has been removed", updatedMd
272: .getConfiguration("myconf3"));
273: assertNotNull("myconf4 has been removed", updatedMd
274: .getConfiguration("myconf4"));
275: }
276:
277: public void testUpdateWithExcludeConfigurations4() throws Exception {
278: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
279: URL settingsUrl = new File(
280: "test/java/org/apache/ivy/plugins/parser/xml/"
281: + "test-update-excludedconfs4.xml").toURL();
282: XmlModuleDescriptorUpdater.update(new IvySettings(),
283: settingsUrl, buffer, new HashMap(), "release",
284: "mynewrev", new Date(), null, true,
285: new String[] { "myconf2" });
286:
287: XmlModuleDescriptorParser parser = XmlModuleDescriptorParser
288: .getInstance();
289: ModuleDescriptor updatedMd = parser.parseDescriptor(
290: new IvySettings(), new ByteArrayInputStream(buffer
291: .toByteArray()), new BasicResource("test",
292: false, 0, 0, false), true);
293:
294: // test the number of configurations
295: Artifact[] artifacts = updatedMd.getAllArtifacts();
296: assertNotNull("Published artifacts shouldn't be null",
297: artifacts);
298: assertEquals("Number of published artifacts incorrect", 4,
299: artifacts.length);
300:
301: // test that the correct configuration has been removed
302: for (int i = 0; i < artifacts.length; i++) {
303: Artifact current = artifacts[i];
304: List currentConfs = Arrays.asList(current
305: .getConfigurations());
306: assertTrue("myconf2 hasn't been removed for artifact "
307: + current.getName(), !currentConfs
308: .contains("myconf2"));
309: }
310: }
311:
312: public void testUpdateWithExcludeConfigurations5() throws Exception {
313: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
314: URL settingsUrl = new File(
315: "test/java/org/apache/ivy/plugins/parser/xml/"
316: + "test-update-excludedconfs5.xml").toURL();
317: XmlModuleDescriptorUpdater.update(new IvySettings(),
318: settingsUrl, buffer, new HashMap(), "release",
319: "mynewrev", new Date(), null, true,
320: new String[] { "myconf2" });
321:
322: XmlModuleDescriptorParser parser = XmlModuleDescriptorParser
323: .getInstance();
324: ModuleDescriptor updatedMd = parser.parseDescriptor(
325: new IvySettings(), new ByteArrayInputStream(buffer
326: .toByteArray()), new BasicResource("test",
327: false, 0, 0, false), true);
328:
329: DependencyDescriptor[] deps = updatedMd.getDependencies();
330: assertNotNull("Dependencies shouldn't be null", deps);
331: assertEquals("Number of dependencies is incorrect", 8,
332: deps.length);
333:
334: // check that none of the dependencies contains myconf2
335: for (int i = 0; i < deps.length; i++) {
336: String name = deps[i].getDependencyId().getName();
337: assertFalse(
338: "Dependency "
339: + name
340: + " shouldn't have myconf2 as module configuration",
341: Arrays.asList(deps[i].getModuleConfigurations())
342: .contains("myconf2"));
343: assertEquals(
344: "Dependency "
345: + name
346: + " shouldn't have a dependency artifact for configuration myconf2",
347: 0, deps[i].getDependencyArtifacts("myconf2").length);
348: }
349: }
350: }
|