Source Code Cross Referenced for CommentedPropertiesTest.java in  » Wiki-Engine » JSPWiki » com » ecyrd » jspwiki » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Wiki Engine » JSPWiki » com.ecyrd.jspwiki.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.ecyrd.jspwiki.util;
002:
003:        import java.io.File;
004:        import java.io.FileOutputStream;
005:        import java.io.IOException;
006:        import java.io.InputStream;
007:        import java.io.OutputStream;
008:        import java.net.URI;
009:        import java.net.URISyntaxException;
010:        import java.net.URL;
011:        import java.util.Properties;
012:
013:        import junit.framework.Test;
014:        import junit.framework.TestCase;
015:        import junit.framework.TestSuite;
016:
017:        public class CommentedPropertiesTest extends TestCase {
018:            Properties m_props = new CommentedProperties();
019:
020:            public void setUp() throws IOException {
021:                InputStream in = CommentedPropertiesTest.class.getClassLoader()
022:                        .getResourceAsStream("test.properties");
023:                m_props.load(in);
024:                in.close();
025:            }
026:
027:            public void testLoadProperties() {
028:                assertEquals(5, m_props.keySet().size());
029:                assertEquals("Foo", m_props.get("testProp1"));
030:                assertEquals("Bar", m_props.get("testProp2"));
031:                assertEquals("", m_props.get("testProp3"));
032:                assertEquals("FooAgain", m_props.get("testProp4"));
033:                assertEquals("BarAgain", m_props.get("testProp5"));
034:                assertNull(m_props.get("testProp6"));
035:
036:                // String we read in, including comments is 208 bytes
037:                assertEquals(208, m_props.toString().length());
038:            }
039:
040:            public void testSetProperty() {
041:                m_props.setProperty("testProp1", "newValue");
042:
043:                // Length of stored string should now be 5 bytes more
044:                assertEquals(208 + 5, m_props.toString().length());
045:                assertTrue(m_props.toString().indexOf("newValue") != -1);
046:
047:                // Create new property; should add 21 (1+7+3+9+1) bytes
048:                m_props.setProperty("newProp", "newValue2");
049:                m_props.containsKey("newProp");
050:                m_props.containsValue("newValue2");
051:                assertEquals(208 + 5 + 21, m_props.toString().length());
052:                assertTrue(m_props.toString().indexOf("newProp = newValue2") != -1);
053:            }
054:
055:            public void testRemove() {
056:                // Remove prop 1; length of stored string should be 14 (1+9+1+3) bytes less
057:                m_props.remove("testProp1");
058:                assertFalse(m_props.containsKey("testProp1"));
059:                assertEquals(208 - 14, m_props.toString().length());
060:
061:                // Remove prop 2; length of stored string should be 15 (1+9+2+3) bytes less
062:                m_props.remove("testProp2");
063:                assertFalse(m_props.containsKey("testProp2"));
064:                assertEquals(208 - 14 - 15, m_props.toString().length());
065:
066:                // Remove prop 3; length of stored string should be 11 (1+9+1) bytes less
067:                m_props.remove("testProp3");
068:                assertFalse(m_props.containsKey("testProp3"));
069:                assertEquals(208 - 14 - 15 - 11, m_props.toString().length());
070:
071:                // Remove prop 4; length of stored string should be 19 (1+9+1+8) bytes less
072:                m_props.remove("testProp4");
073:                assertFalse(m_props.containsKey("testProp4"));
074:                assertEquals(208 - 14 - 15 - 11 - 19, m_props.toString()
075:                        .length());
076:
077:                // Remove prop 5; length of stored string should be 19 (1+9+1+8) bytes less
078:                m_props.remove("testProp5");
079:                assertFalse(m_props.containsKey("testProp5"));
080:                assertEquals(208 - 14 - 15 - 11 - 19 - 19, m_props.toString()
081:                        .length());
082:            }
083:
084:            public void testStore() throws Exception {
085:                // Write results to a new file
086:                File outFile = createFile("test2.properties");
087:                OutputStream out = new FileOutputStream(outFile);
088:                m_props.store(out, null);
089:                out.close();
090:
091:                // Load the file into new props object; should return identical strings
092:                Properties props2 = new CommentedProperties();
093:                InputStream in = CommentedPropertiesTest.class.getClassLoader()
094:                        .getResourceAsStream("test2.properties");
095:                props2.load(in);
096:                in.close();
097:                assertEquals(m_props.toString(), props2.toString());
098:
099:                // Remove props1, 2, 3 & resave props to new file
100:                m_props.remove("testProp1");
101:                m_props.remove("testProp2");
102:                m_props.remove("testProp3");
103:                outFile = createFile("test3.properties");
104:                out = new FileOutputStream(outFile);
105:                m_props.store(out, null);
106:                out.close();
107:
108:                // Load the new file; should not have props1/2/3 & is shorter
109:                Properties props3 = new CommentedProperties();
110:                in = CommentedPropertiesTest.class.getClassLoader()
111:                        .getResourceAsStream("test3.properties");
112:                props3.load(in);
113:                in.close();
114:                assertNotSame(m_props.toString(), props3.toString());
115:                assertFalse(props3.containsKey("testProp1"));
116:                assertFalse(props3.containsKey("testProp2"));
117:                assertFalse(props3.containsKey("testProp3"));
118:                assertTrue(props3.containsKey("testProp4"));
119:                assertTrue(props3.containsKey("testProp5"));
120:
121:                // Clean up
122:                File file = getFile("test2.properties");
123:                if (file != null && file.exists()) {
124:                    file.delete();
125:                }
126:                file = getFile("test3.properties");
127:                if (file != null && file.exists()) {
128:                    file.delete();
129:                }
130:            }
131:
132:            private File createFile(String file) throws URISyntaxException {
133:                // Get the test.properties file
134:                URL url = CommentedPropertiesTest.class.getClassLoader()
135:                        .getResource("test.properties");
136:                if (url == null) {
137:                    throw new IllegalStateException(
138:                            "Very odd. We can't find test.properties!");
139:                }
140:
141:                // Construct new file in same directory
142:                File testFile = new File(new URI(url.toString()));
143:                File dir = testFile.getParentFile();
144:                return new File(dir, file);
145:            }
146:
147:            private File getFile(String name) {
148:                // Get the test.properties file
149:                URL url = CommentedPropertiesTest.class.getClassLoader()
150:                        .getResource(name);
151:                if (url == null) {
152:                    throw new IllegalStateException(
153:                            "Very odd. We can't find test.properties!");
154:                }
155:                // Return the file
156:                File file = null;
157:
158:                try {
159:                    file = new File(new URI(url.toString()));
160:                } catch (URISyntaxException e) {
161:                    // TODO Auto-generated catch block
162:                    e.printStackTrace();
163:                }
164:
165:                return file;
166:            }
167:
168:            public static Test suite() {
169:                return new TestSuite(CommentedPropertiesTest.class);
170:            }
171:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.