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