KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > config > SavableConfigurationTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.config;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.ByteArrayOutputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.text.ParseException JavaDoc;
43 import java.util.Date JavaDoc;
44
45 /** JUnit test class for testing SavableConfiguration.
46   *
47   * @author <a HREF="mailto:chrisw@rice.edu">Chris Warrington</a>
48   * @version $Id: $
49   */

50 public class SavableConfigurationTest extends DrJavaTestCase {
51   /**
52    * This is the date format the Date.toString() uses.
53    */

54   SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("EEE MMM dd HH:mm:ss zzz yyyy");
55   
56   ByteArrayOutputStream JavaDoc outputBytes = null;
57   
58   public void setUp() throws Exception JavaDoc {
59     super.setUp();
60     outputBytes = new ByteArrayOutputStream JavaDoc();
61   }
62   
63   /**
64    * Tests the saveConfiguration method with no configuration data
65    */

66   public void testEmptyConfiguration() throws IOException JavaDoc {
67     SavableConfiguration emptyConfig = new SavableConfiguration(new DefaultOptionMap());
68     
69     emptyConfig.saveConfiguration(outputBytes, "header");
70     
71     String JavaDoc outputString = outputBytes.toString();
72     String JavaDoc[] lines = outputString.split("\n");
73     
74     assertTrue("Data exists", outputString.length() > 0);
75     assertEquals("Number of lines", 2, lines.length);
76     assertEquals("Starts with \"#header\"", "#header", lines[0]);
77     try {
78       //get the date without the #
79
String JavaDoc bareDate = lines[1].substring(1, lines[1].length());
80       Date JavaDoc readInDate = dateFormat.parse(bareDate);
81       assertTrue("Embedded date less than now",
82                  readInDate.compareTo(new Date JavaDoc()) <= 0);
83     }
84     catch (ParseException JavaDoc pe) {
85       fail("Could not parse second line into a date.");
86     }
87   }
88   
89   /**
90    * Tests the saveConfiguration method with some configuration data.
91    */

92   public void testNonEmptyConfiguration() throws IOException JavaDoc {
93     DefaultOptionMap optionsMap = new DefaultOptionMap();
94     optionsMap.setOption(new BooleanOption("tests_are_good", false), true);
95     optionsMap.setOption(new IntegerOption("meaning_of_life", 0), 42);
96     optionsMap.setOption(new StringOption("yay_strings", "hello?"), "goodbye");
97     
98     SavableConfiguration nonEmptyConfig = new SavableConfiguration(optionsMap);
99     
100     nonEmptyConfig.saveConfiguration(outputBytes, "header");
101     
102     String JavaDoc outputString = outputBytes.toString();
103     String JavaDoc[] lines = outputString.split("\n");
104
105     assertTrue("Data exists", outputString.length() > 0);
106     assertEquals("Number of lines", 5, lines.length);
107     assertEquals("Starts with \"#header\"", "#header", lines[0]);
108     try {
109       //get the date without the #
110
String JavaDoc bareDate = lines[1].substring(1, lines[1].length());
111       Date JavaDoc readInDate = dateFormat.parse(bareDate);
112       assertTrue("Embedded date less than now",
113                  readInDate.compareTo(new Date JavaDoc()) <= 0);
114     }
115     catch (ParseException JavaDoc pe) {
116       fail("Could not parse second line into a date.");
117     }
118     assertEquals("BooleanOption", "tests_are_good = true", lines[2]);
119     assertEquals("IntegerOption", "meaning_of_life = 42", lines[3]);
120     assertEquals("StringOption", "yay_strings = goodbye", lines[4]);
121   }
122 }
Popular Tags