1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.services.properties;
16
17 import org.figure8.join.core.InvalidParameterException;
18 import org.figure8.join.businessobjects.commons.Release;
19 import org.figure8.join.businessobjects.artifact.Assembly;
20 import org.figure8.join.businessobjects.artifact.Component;
21 import org.figure8.join.businessobjects.artifact.ComponentType;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import java.util.Date;
28 import java.util.Properties;
29 /**
30 * JUnit test case for ComponentPropertiesExtractor.
31 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
32 * @version $Revision: 1.2 $
33 */
34 public class ComponentPropertiesExtractorTest extends TestCase{
35
36
37
38 /** The Component to test extractor with */
39 private Component component = null;
40 /** The PropertiesExtractor implementation to test */
41 private ComponentPropertiesExtractor extractor = new ComponentPropertiesExtractor();
42
43
44
45
46 /** Default constructor. Build a test case using a name. */
47 public ComponentPropertiesExtractorTest(String testName){
48 super(testName);
49 }
50
51
52
53
54 /**
55 * Make this class a TestSuite.
56 * @return A TestSuite containing this TestCase.
57 */
58 public static Test suite(){
59 TestSuite suite = new TestSuite(ComponentPropertiesExtractorTest.class);
60 return suite;
61 }
62
63
64
65
66 /** Prepare a component for tests */
67 public void setUp(){
68
69 Release release = new Release("1.0", 1, 0, new Date());
70 ComponentType type = new ComponentType("key", "label", "key-v{0}", "description");
71 component = new Component("01", 1024, type);
72 component.setAssembly(new Assembly("01", "comments", "lbroudoux", release));
73 }
74
75 /** Test simple extraction */
76 public void testExtract(){
77 Properties result = extractor.extract(component);
78 assertOnProperties(result, ComponentPropertiesExtractor.COMPONENT_PREFIX);
79 }
80
81 /** Test extraction with a prefix */
82 public void testExtractWithPrefix(){
83 Properties result = extractor.extract(component, "myprefix.");
84 assertOnProperties(result, "myprefix.");
85 }
86
87 /** Test extraction with invalid object */
88 public void testExtractWithBadParam(){
89 boolean fail = false;
90 try {extractor.extract("mybadparam");}
91 catch (InvalidParameterException ipe){
92 fail = true;
93 }
94 if (!fail)
95 fail("InvalidParameterException was not thrown with bad param");
96 }
97
98
99
100
101 /** Assert on properties content using given prefix. */
102 private void assertOnProperties(Properties result, String prefix){
103
104 assertNotNull(result);
105
106 assertEquals("key-v01", result.getProperty(prefix + "key"));
107 assertEquals("01", result.getProperty(prefix + "version"));
108 assertEquals("1024", result.getProperty(prefix + "size"));
109 assertEquals("1.0", result.getProperty(prefix + "release.name"));
110 assertEquals("key", result.getProperty(prefix + "type.key"));
111 assertEquals("label", result.getProperty(prefix + "type.label"));
112 }
113 }