1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb.descriptors;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.InputStream;
20  import java.text.ParseException;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  import org.vafer.jdeb.utils.MapVariableResolver;
27  
28  public final class PackageDescriptorTestCase extends TestCase {
29  
30  	public void testParse() throws Exception {
31  		
32  		final InputStream is = new ByteArrayInputStream(
33  				("Key1: Value1\n" +
34  				 "Key2: Value2\n" +
35  				 " Value2.1\n" +
36  				 " Value2.2\n" +
37  				 "Key3: Value3\n").getBytes());
38  		
39  		final PackageDescriptor d = new PackageDescriptor(is, null);
40  		assertFalse(d.isValid());
41  
42  		assertEquals("key 1", "Value1", d.get("Key1"));
43  		assertEquals("key 2", "Value2\nValue2.1\nValue2.2", d.get("Key2"));
44  		assertEquals("key 3", "Value3", d.get("Key3"));
45  	}
46  
47  	public void testToString() throws Exception {
48  		PackageDescriptor descriptor = new PackageDescriptor();
49  		descriptor.set("Package", "test-package");
50  		descriptor.set("Description", "This is\na description\non several lines");
51  		descriptor.set("Version", "1.0");
52  
53  		String s = descriptor.toString();
54  
55  		PackageDescriptor descriptor2 = new PackageDescriptor(new ByteArrayInputStream(s.getBytes()), null);
56  		assertEquals("Package", descriptor.get("Package"), descriptor2.get("Package"));
57  		assertEquals("Description", descriptor.get("Description"), descriptor2.get("Description"));
58  		assertEquals("Version 3", descriptor.get("Version"), descriptor2.get("Version"));
59  	}
60  	
61  	public void testVariableSubstitution() {
62  		
63  		final Map map = new HashMap();
64  		map.put("VERSION", "1.2");
65  		map.put("MAINTAINER", "Torsten Curdt <tcurdt@vafer.org>");
66  
67  		final PackageDescriptor d = new PackageDescriptor(new MapVariableResolver(map));
68  		d.set("Version", "[[VERSION]]");
69  		d.set("Maintainer", "[[MAINTAINER]]");
70  		d.set("NoResolve1", "test[[test");
71  		d.set("NoResolve2", "[[test]]");
72  		
73  		assertEquals("1.2", d.get("Version"));
74  		assertEquals("Torsten Curdt <tcurdt@vafer.org>", d.get("Maintainer"));
75  		assertEquals("test[[test", d.get("NoResolve1"));
76  		assertEquals("[[test]]", d.get("NoResolve2"));
77  	}
78  	
79  	public void testEmptyLines() throws Exception {
80  		final InputStream is = new ByteArrayInputStream(
81  				("Key1: Value1\n" +
82  				 "Key2: Value2\n" +
83  				 "\n").getBytes());
84  		try {
85  			new PackageDescriptor(is, null);
86  			fail("Should throw a ParseException");
87  		} catch(ParseException e) {
88  			
89  		}		
90  	}
91  }