001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jmx.xmbean;
023:
024: import java.io.File;
025:
026: import javax.management.Attribute;
027: import javax.management.AttributeList;
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030:
031: import org.jboss.mx.persistence.AttributePersistenceManager;
032: import org.jboss.system.ServiceMBeanSupport;
033: import org.jboss.system.pm.XMLAttributePersistenceManager;
034: import org.jboss.system.server.ServerConfigLocator;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038:
039: /**
040: * A test service that wraps an XMLAttributePersistenceManager
041: * configured to write to a random directory containing a space
042: * in its name, e.g. "./tmp/XmlApmXXXXXTest .dir"
043: *
044: * @see org.jboss.test.jmx.test.MLAttributePersistenceManagerUnitTestCase
045: *
046: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
047: * @version $Revision: 57211 $
048: */
049: public class XMLAttributePersistenceManagerTestService extends
050: ServiceMBeanSupport {
051: private AttributePersistenceManager apm;
052: private File storeDir;
053:
054: protected void startService() throws Exception {
055: File tmpDir = ServerConfigLocator.locate().getServerTempDir();
056: boolean result;
057:
058: // Get a temporary file in the server tmp dir, with a space in its name
059: storeDir = File.createTempFile("XmlApm", "Test .dir", tmpDir);
060:
061: // Remove the tmp file
062: result = storeDir.delete();
063:
064: // Recreate it as directory
065: result = storeDir.mkdir();
066: log
067: .info("Created 'bad' store dir: " + storeDir + ", "
068: + result);
069:
070: String dirURL = storeDir.toURL().toString();
071: log.info("Dir URL: " + dirURL);
072:
073: apm = new XMLAttributePersistenceManager();
074:
075: // Initialize an XMLAttributePeristenceManager and
076: // configure it to point to the "bad" directory
077: apm.create(null, prepareConfig(dirURL));
078: }
079:
080: protected void stopService() throws Exception {
081: if (apm != null) {
082: apm.removeAll();
083: apm.destroy();
084: log.info("Destroyed AttributePersistenceManager");
085: }
086: if (storeDir != null) {
087: boolean result = storeDir.delete();
088: log.info("Removed: " + storeDir + ", " + result);
089: }
090: }
091:
092: public void store(String id, AttributeList atlist) throws Exception {
093: apm.store(id, atlist);
094: }
095:
096: public AttributeList load(String id) throws Exception {
097: return apm.load(id);
098: }
099:
100: public void selftest() throws Exception {
101: // Store some attributes under an id
102: AttributeList alist = new AttributeList();
103: String storeId = "bananarama";
104:
105: Integer anInteger = new Integer(666);
106: String aString = new String("Evil Test");
107: alist.add(new Attribute("Attr1", anInteger));
108: alist.add(new Attribute("Attr2", aString));
109: apm.store(storeId, alist);
110:
111: // Read them back
112: AttributeList alist2 = apm.load(storeId);
113: }
114:
115: private Element prepareConfig(String dir) throws Exception {
116: // build the config XML Element in memory using DOM
117: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
118: .newDocumentBuilder();
119: Document doc = builder.newDocument();
120:
121: // Create config element
122: Element config = doc
123: .createElement(XMLAttributePersistenceManager.DATA_DIR_ELEMENT);
124:
125: // Insert a text node with the directory name
126: Node text = doc.createTextNode(dir);
127:
128: config.appendChild(text);
129:
130: // Return the config
131: return config;
132: }
133: }
|