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.m2;
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:
026: import junit.framework.TestCase;
027:
028: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
029: import org.apache.ivy.core.settings.IvySettings;
030: import org.apache.ivy.util.FileUtil;
031:
032: public class PomModuleDescriptorWriterTest extends TestCase {
033: private static String LICENSE;
034: static {
035: try {
036: LICENSE = FileUtil
037: .readEntirely(new BufferedReader(
038: new InputStreamReader(
039: PomModuleDescriptorWriterTest.class
040: .getResourceAsStream("license.xml"))));
041: } catch (IOException e) {
042: e.printStackTrace();
043: }
044: }
045: private File _dest = new File("build/test/test-write.xml");
046:
047: public void testSimple() throws Exception {
048: ModuleDescriptor md = PomModuleDescriptorParser.getInstance()
049: .parseDescriptor(new IvySettings(),
050: getClass().getResource("test-simple.pom"),
051: false);
052: PomModuleDescriptorWriter.write(md, LICENSE,
053: PomModuleDescriptorWriter.DEFAULT_MAPPING, _dest);
054: assertTrue(_dest.exists());
055:
056: String wrote = FileUtil.readEntirely(
057: new BufferedReader(new FileReader(_dest))).replaceAll(
058: "\r\n", "\n").replace('\r', '\n');
059: String expected = readEntirely("test-write-simple.xml")
060: .replaceAll("\r\n", "\n").replace('\r', '\n');
061: assertEquals(expected, wrote);
062: }
063:
064: public void testSimpleDependencies() throws Exception {
065: ModuleDescriptor md = PomModuleDescriptorParser
066: .getInstance()
067: .parseDescriptor(
068: new IvySettings(),
069: getClass().getResource("test-dependencies.pom"),
070: false);
071: PomModuleDescriptorWriter.write(md, LICENSE,
072: PomModuleDescriptorWriter.DEFAULT_MAPPING, _dest);
073: assertTrue(_dest.exists());
074:
075: String wrote = FileUtil.readEntirely(
076: new BufferedReader(new FileReader(_dest))).replaceAll(
077: "\r\n", "\n").replace('\r', '\n');
078: String expected = readEntirely(
079: "test-write-simple-dependencies.xml").replaceAll(
080: "\r\n", "\n").replace('\r', '\n');
081: assertEquals(expected, wrote);
082: }
083:
084: public void testDependenciesWithScope() throws Exception {
085: ModuleDescriptor md = PomModuleDescriptorParser.getInstance()
086: .parseDescriptor(
087: new IvySettings(),
088: getClass().getResource(
089: "test-dependencies-with-scope.pom"),
090: false);
091: PomModuleDescriptorWriter.write(md, LICENSE,
092: PomModuleDescriptorWriter.DEFAULT_MAPPING, _dest);
093: assertTrue(_dest.exists());
094:
095: String wrote = FileUtil.readEntirely(
096: new BufferedReader(new FileReader(_dest))).replaceAll(
097: "\r\n", "\n").replace('\r', '\n');
098: String expected = readEntirely(
099: "test-write-dependencies-with-scope.xml").replaceAll(
100: "\r\n", "\n").replace('\r', '\n');
101: assertEquals(expected, wrote);
102: }
103:
104: public void testOptional() throws Exception {
105: ModuleDescriptor md = PomModuleDescriptorParser.getInstance()
106: .parseDescriptor(new IvySettings(),
107: getClass().getResource("test-optional.pom"),
108: false);
109: PomModuleDescriptorWriter.write(md, LICENSE,
110: PomModuleDescriptorWriter.DEFAULT_MAPPING, _dest);
111: assertTrue(_dest.exists());
112:
113: String wrote = FileUtil.readEntirely(
114: new BufferedReader(new FileReader(_dest))).replaceAll(
115: "\r\n", "\n").replace('\r', '\n');
116: String expected = readEntirely(
117: "test-write-dependencies-optional.xml").replaceAll(
118: "\r\n", "\n").replace('\r', '\n');
119: assertEquals(expected, wrote);
120: }
121:
122: private String readEntirely(String resource) throws IOException {
123: return FileUtil.readEntirely(new BufferedReader(
124: new InputStreamReader(
125: PomModuleDescriptorWriterTest.class
126: .getResource(resource).openStream())));
127: }
128:
129: public void setUp() {
130: // don't add ivy version to se static files for comparison
131: PomModuleDescriptorWriter.setAddIvyVersion(false);
132: if (_dest.exists()) {
133: _dest.delete();
134: }
135: if (!_dest.getParentFile().exists()) {
136: _dest.getParentFile().mkdirs();
137: }
138: }
139:
140: protected void tearDown() throws Exception {
141: if (_dest.exists()) {
142: _dest.delete();
143: }
144: }
145: }
|