001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.jumpimpl.module.contentstore;
026:
027: import junit.framework.*;
028:
029: import java.io.*;
030: import java.io.IOException;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.Map;
034: import java.util.Properties;
035:
036: import com.sun.jump.module.contentstore.*;
037: import com.sun.jump.executive.JUMPExecutive;
038:
039: public class FileStoreTest extends TestCase {
040:
041: FileStoreTestInternal actualClass;
042: boolean verbose = false;
043:
044: public FileStoreTest(String testName) {
045: super (testName);
046: }
047:
048: protected void setUp() {
049: actualClass = new FileStoreTestInternal();
050: actualClass.setupTest();
051: }
052:
053: public void testFileStore() {
054: actualClass.runTest();
055: }
056:
057: // data URIs for tests to use.
058: static String[] sampleDataUris = { "./Apps/Amark/title",
059: "./Apps/Amark/size", "./Apps/Amark/icon",
060: "./Apps/Amark/descriptor",
061: "./Apps/Amark/data/Subdata.properties",
062: "./Apps/Amark/Amark.properties" };
063:
064: // Data files corresponding to sampleDataUris above.
065: static JUMPData[] datas = { new JUMPData(true), new JUMPData(1.0f),
066: new JUMPData(12345), new JUMPData("String"),
067: new JUMPData(new Properties()),
068: new JUMPData(new Properties())
069: //new JUMPData(System.getProperties())
070: };
071:
072: // list URIs for tests to use.
073: static String[] sampleListUris = { "./Apps/Amark",
074: "./Apps/Amark/data" };
075:
076: class FileStoreTestInternal extends JUMPContentStoreSubClass {
077:
078: // The directory name which "." of this content store will be using.
079:
080: void setupTest() {
081:
082: // For the standalone test run, emulate the executive setup
083: if (JUMPExecutive.getInstance() == null) {
084: JUMPStoreFactory factory = new com.sun.jumpimpl.module.contentstore.StoreFactoryImpl();
085: factory.load(com.sun.jumpimpl.process.JUMPModulesConfig
086: .getProperties());
087:
088: }
089:
090: }
091:
092: void runTest() {
093:
094: boolean testFailed = false;
095:
096: trace("Starting the test run");
097:
098: // Get the store handle from the JUMPContentStoreSubClass.
099: JUMPStoreHandle storeHandle = openStore(true);
100:
101: trace("Got a type of JUMPStore: " + storeHandle);
102:
103: try {
104: // first, try to create list nodes.
105: for (int i = 0; i < sampleListUris.length; i++) {
106: storeHandle.createNode(sampleListUris[i]);
107: }
108:
109: // then, create all data nodes.
110: for (int i = 0; i < sampleDataUris.length; i++) {
111: storeHandle.createDataNode(sampleDataUris[i],
112: datas[i]);
113: }
114:
115: // get back all the data nodes we just created and check the data content.
116: trace("All data created, testing equality");
117: for (int i = 0; i < sampleDataUris.length; i++) {
118: JUMPData data = ((JUMPNode.Data) storeHandle
119: .getNode(sampleDataUris[i])).getData();
120: assertTrue(data.equals(datas[i]));
121: }
122:
123: // get the listing of all nodes starting at the root.
124: JUMPNode.List dirnode = (JUMPNode.List) storeHandle
125: .getNode(".");
126:
127: trace("List of storeHandle content");
128: printChildren(dirnode, " ");
129:
130: // try updating a node content.
131: trace("Testing update of storeHandle content");
132: storeHandle.updateDataNode(sampleDataUris[0], datas[1]);
133: JUMPData data = ((JUMPNode.Data) storeHandle
134: .getNode(sampleDataUris[0])).getData();
135: assertTrue(data.equals(datas[1]));
136:
137: // now, delete all list nodes.
138: trace("Delete all");
139: for (int i = 0; i < sampleListUris.length; i++) {
140: storeHandle.deleteNode(sampleListUris[i]);
141: }
142:
143: //trace("List of storeHandle content");
144: //printChildren(dirnode, " ");
145: } catch (IOException ioe) {
146: trace("IOException was thrown!");
147: ioe.printStackTrace(System.out);
148: testFailed = true;
149: }
150:
151: trace("Closing the store");
152: closeStore(storeHandle);
153:
154: trace("Done.");
155:
156: assertTrue(!testFailed);
157: }
158:
159: // Recursively print all the nodes in the tree.
160: void printChildren(JUMPNode jumpNode, String tab) {
161: if (jumpNode != null) {
162: trace(tab + jumpNode);
163: if (jumpNode instanceof JUMPNode.List) {
164: JUMPNode.List list = (JUMPNode.List) jumpNode;
165: for (Iterator itn = list.getChildren(); itn
166: .hasNext();) {
167: JUMPNode node = (JUMPNode) itn.next();
168: printChildren(node, tab + " ");
169: }
170: }
171: }
172: }
173:
174: void trace(String output) {
175: if (verbose)
176: System.out.println(output);
177: }
178: }
179:
180: class JUMPContentStoreSubClass extends JUMPContentStore {
181:
182: protected JUMPStore getStore() {
183: return JUMPStoreFactory.getInstance().getModule(
184: JUMPStoreFactory.TYPE_FILE);
185: }
186:
187: public void load(Map map) {
188: }
189:
190: public void unload() {
191: }
192: }
193:
194: }
|