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.Deliverable;
20  import org.figure8.join.businessobjects.artifact.DeliverableType;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import java.util.Properties;
27  import java.util.Date;
28  /**
29   * JUnit test case for DeliverablePropertiesExtractor.
30   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31   * @version Revision$
32   */
33  public class DeliverablePropertiesExtractorTest extends TestCase{
34  
35     // Attributes ---------------------------------------------------------------
36  
37     /** The Deliverable to test extractor with. */
38     private Deliverable deliverable = null;
39     /** The PropertiesExtractor implementation to test */
40     private DeliverablePropertiesExtractor extractor = new DeliverablePropertiesExtractor();
41  
42  
43     // Constructors -------------------------------------------------------------
44  
45     /** Default constructor. Build a test case using a name. */
46     public DeliverablePropertiesExtractorTest(String testName){
47        super(testName);
48     }
49  
50  
51     // Override of TestCase -----------------------------------------------------
52  
53     /**
54      * Make this class a TestSuite.
55      * @return A TestSuite containing this TestCase.
56      */
57     public static Test suite(){
58        TestSuite suite = new TestSuite(DeliverablePropertiesExtractorTest.class);
59        return suite;
60     }
61     
62     
63     // Public -------------------------------------------------------------------
64  
65     /** Prepare a deliverable for tests */
66     public void setUp(){
67        // Create required objects for component.
68        Release release = new Release("1.0", 1, 0, new Date());
69        DeliverableType type = new DeliverableType("key", "label", "key-r{0}-v{1}", true, true);
70        deliverable = new Deliverable("01", "comments", "lbroudoux", release, type);
71     }
72  
73     /** Test simple extraction */
74     public void testExtract(){
75        Properties result = extractor.extract(deliverable);
76        assertOnProperties(result, DeliverablePropertiesExtractor.DELIVERABLE_PREFIX);
77     }
78  
79     /** Test extraction with a prefix */
80     public void testExtractWithPrefix(){
81        Properties result = extractor.extract(deliverable, "myprefix.");
82        assertOnProperties(result, "myprefix.");
83     }
84  
85     /** Test extraction with invalid object */
86     public void testExtractWithBadParam(){
87        boolean fail = false;
88        try {extractor.extract("mybadparam");}
89        catch (InvalidParameterException ipe){
90           fail = true;
91        }
92        if (!fail)
93           fail("InvalidParameterException was not thrown with bad param");
94     }
95  
96  
97     // Private ------------------------------------------------------------------
98  
99     /** Assert on properties content using given prefix. */
100    private void assertOnProperties(Properties result, String prefix){
101       // Assert we have something.
102       assertNotNull(result);
103       // Assert on properties content.
104       assertEquals("key-r1.0-v01", result.getProperty(prefix + "key"));
105       assertEquals("comments", result.getProperty(prefix + "comments"));
106       assertEquals("lbroudoux", result.getProperty(prefix + "supplier.id"));
107       assertEquals("1.0", result.getProperty(prefix + "release.name"));
108       assertEquals("key", result.getProperty(prefix + "type.key"));
109       assertEquals("label", result.getProperty(prefix + "type.label"));
110    }
111 }