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.File;
022: import java.io.FileReader;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.util.GregorianCalendar;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
030: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
031: import org.apache.ivy.core.module.id.ModuleRevisionId;
032: import org.apache.ivy.core.settings.IvySettings;
033: import org.apache.ivy.util.FileUtil;
034:
035: public class XmlModuleDescriptorWriterTest extends TestCase {
036: private static String LICENSE;
037: static {
038: try {
039: LICENSE = FileUtil
040: .readEntirely(new BufferedReader(
041: new InputStreamReader(
042: XmlModuleDescriptorWriterTest.class
043: .getResourceAsStream("license.xml"))));
044: } catch (IOException e) {
045: e.printStackTrace();
046: }
047: }
048:
049: private File _dest = new File("build/test/test-write.xml");
050:
051: public void testSimple() throws Exception {
052: DefaultModuleDescriptor md = (DefaultModuleDescriptor) XmlModuleDescriptorParser
053: .getInstance().parseDescriptor(
054: new IvySettings(),
055: XmlModuleDescriptorWriterTest.class
056: .getResource("test-simple.xml"), true);
057: md.setResolvedPublicationDate(new GregorianCalendar(2005, 4, 1,
058: 11, 0, 0).getTime());
059: md.setResolvedModuleRevisionId(new ModuleRevisionId(md
060: .getModuleRevisionId().getModuleId(), "NONE"));
061: XmlModuleDescriptorWriter.write(md, LICENSE, _dest);
062:
063: assertTrue(_dest.exists());
064: String wrote = FileUtil.readEntirely(
065: new BufferedReader(new FileReader(_dest))).replaceAll(
066: "\r\n", "\n").replace('\r', '\n');
067: String expected = readEntirely("test-write-simple.xml")
068: .replaceAll("\r\n", "\n").replace('\r', '\n');
069: assertEquals(expected, wrote);
070: }
071:
072: public void testDependencies() throws Exception {
073: ModuleDescriptor md = XmlModuleDescriptorParser.getInstance()
074: .parseDescriptor(
075: new IvySettings(),
076: XmlModuleDescriptorWriterTest.class
077: .getResource("test-dependencies.xml"),
078: true);
079: XmlModuleDescriptorWriter.write(md, LICENSE, _dest);
080:
081: assertTrue(_dest.exists());
082: String wrote = FileUtil.readEntirely(
083: new BufferedReader(new FileReader(_dest))).replaceAll(
084: "\r\n", "\n").replace('\r', '\n');
085: String expected = readEntirely("test-write-dependencies.xml")
086: .replaceAll("\r\n", "\n").replace('\r', '\n');
087: assertEquals(expected, wrote);
088: }
089:
090: public void testFull() throws Exception {
091: ModuleDescriptor md = XmlModuleDescriptorParser.getInstance()
092: .parseDescriptor(
093: new IvySettings(),
094: XmlModuleDescriptorWriterTest.class
095: .getResource("test.xml"), false);
096: XmlModuleDescriptorWriter.write(md, LICENSE, _dest);
097:
098: assertTrue(_dest.exists());
099: String wrote = FileUtil.readEntirely(
100: new BufferedReader(new FileReader(_dest))).replaceAll(
101: "\r\n", "\n").replace('\r', '\n');
102: String expected = readEntirely("test-write-full.xml")
103: .replaceAll("\r\n", "\n").replace('\r', '\n');
104: assertEquals(expected, wrote);
105: }
106:
107: private String readEntirely(String resource) throws IOException {
108: return FileUtil.readEntirely(new BufferedReader(
109: new InputStreamReader(
110: XmlModuleDescriptorWriterTest.class
111: .getResource(resource).openStream())));
112: }
113:
114: public void setUp() {
115: if (_dest.exists()) {
116: _dest.delete();
117: }
118: if (!_dest.getParentFile().exists()) {
119: _dest.getParentFile().mkdirs();
120: }
121: }
122:
123: protected void tearDown() throws Exception {
124: if (_dest.exists()) {
125: _dest.delete();
126: }
127: }
128: }
|