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.core.setup;
16  
17  import junit.framework.Test;
18  import junit.framework.TestCase;
19  import junit.framework.TestSuite;
20  import org.apache.commons.digester.Digester;
21  
22  import java.io.InputStream;
23  import java.io.ByteArrayInputStream;
24  import java.util.ArrayList;
25  /**
26   * Test for application config digester rule set.
27   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28   * @version $Revision: 1.2 $
29   */
30  public class ApplicationConfigRuleSetTest extends TestCase{
31  
32     // Attributes ---------------------------------------------------------------
33  
34     /** Content of configuration stream */
35     private String configContent;
36     /** Array for storing read configuration properties */
37     private ArrayList configProps = new ArrayList();
38  
39  
40     // Constructors -------------------------------------------------------------
41  
42     /** Default constructor. Build a test case using a name. */
43     public ApplicationConfigRuleSetTest(String testName){
44        super(testName);
45     }
46  
47  
48     // Override of TestCase -----------------------------------------------------
49  
50     /**
51      * Make this class a TestSuite.
52      * @return A TestSuite containing this TestCase.
53      */
54     public static Test suite(){
55        TestSuite suite = new TestSuite(ApplicationConfigRuleSetTest.class);
56        return suite;
57     }
58  
59  
60     // Public -------------------------------------------------------------------
61  
62     /**
63      * Setup the configuration content.
64      */
65     public void setUp(){
66        StringBuffer buffer = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
67        buffer.append("<join-configuration><properties>");
68        buffer.append("<property key=\"key1\" value=\"value1\"/>");
69        buffer.append("<property key=\"key2\" value=\"value2\"/>");
70        buffer.append("<property key=\"key3\" value=\"value3\"/>");
71        buffer.append("</properties></join-configuration>");
72        // Transfer buffer to configContent.
73        configContent = buffer.toString();
74     }
75  
76     /**
77      * Test the <code>ApplicationConfigRuleSet</code> against
78      * configuration content field (<b>configContent</b>).
79      */
80     public void testRuleSet(){
81        // Init digester with correct rules and push this.
82        Digester digester = new Digester();
83        digester.setValidating(false);
84        digester.setNamespaceAware(true);
85        digester.setUseContextClassLoader(true);
86        digester.addRuleSet(new ApplicationConfigRuleSet());
87        digester.push(this);
88  
89        try{
90           // Prepare stream to parse.
91           InputStream is = new ByteArrayInputStream(configContent.getBytes());
92           digester.parse(is);
93        }
94        catch (Exception e){
95           e.printStackTrace();
96        }
97  
98        // Assert.
99        assertEquals(configProps.size(), 3);
100    }
101 
102    /**
103     * Helper method to store config properties.
104     * @param property A ConfigProperty object
105     */
106    public void addConfigProperty(ConfigProperty property){
107       configProps.add(property);
108    }
109 }