001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.ui.tests.util.xml;
011:
012: import java.io.File;
013: import java.io.IOException;
014: import java.net.URL;
015:
016: import javax.xml.parsers.FactoryConfigurationError;
017: import javax.xml.parsers.ParserConfigurationException;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import org.eclipse.core.runtime.FileLocator;
024: import org.eclipse.pde.internal.core.XMLDefaultHandler;
025: import org.eclipse.pde.internal.core.util.DOMParserWrapper;
026: import org.eclipse.pde.internal.core.util.SAXParserWrapper;
027: import org.eclipse.pde.internal.ui.tests.macro.MacroPlugin;
028: import org.osgi.framework.Bundle;
029: import org.xml.sax.SAXException;
030:
031: /**
032: * SAXParserWrapperTestCase
033: *
034: */
035: public class ParserWrapperTestCase extends TestCase {
036:
037: protected static final int FTHREADCOUNT = 5;
038: protected static final int FSAX = 0;
039: protected static final int FDOM = 1;
040: protected static File fXMLFile;
041: protected static final String FFILENAME = "/plugin.xml"; //$NON-NLS-1$
042:
043: public static Test suite() {
044: return new TestSuite(ParserWrapperTestCase.class);
045: }
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: MacroPlugin plugin = MacroPlugin.getDefault();
050: if (plugin == null)
051: throw new Exception("ERROR: Macro plug-in uninitialized"); //$NON-NLS-1$
052: Bundle bundle = plugin.getBundle();
053: if (bundle == null)
054: throw new Exception("ERROR: Bundle uninitialized"); //$NON-NLS-1$
055: URL url = bundle.getEntry(FFILENAME);
056: if (url == null)
057: throw new Exception("ERROR: URL not found: " + FFILENAME); //$NON-NLS-1$
058: String path = FileLocator.resolve(url).getPath();
059: if ("".equals(path)) //$NON-NLS-1$
060: throw new Exception("ERROR: URL unresolved: " + FFILENAME); //$NON-NLS-1$
061: fXMLFile = new File(path);
062: }
063:
064: public void testSAXParserWrapperConcurrency() throws Exception {
065:
066: ParserThread[] threads = new ParserThread[FTHREADCOUNT];
067:
068: for (int x = 0; x < FTHREADCOUNT; x++) {
069: threads[x] = new ParserThread(FSAX, fXMLFile);
070: threads[x].start();
071: }
072:
073: for (int x = 0; x < FTHREADCOUNT; x++) {
074: threads[x].join();
075: assertFalse(threads[x].getError());
076: }
077:
078: }
079:
080: public void testDOMParserWrapperConcurrency() throws Exception {
081:
082: ParserThread[] threads = new ParserThread[FTHREADCOUNT];
083:
084: for (int x = 0; x < FTHREADCOUNT; x++) {
085: threads[x] = new ParserThread(FDOM, fXMLFile); //$NON-NLS-1$
086: threads[x].start();
087: }
088:
089: for (int x = 0; x < FTHREADCOUNT; x++) {
090: threads[x].join();
091: assertFalse(threads[x].getError());
092: }
093:
094: }
095:
096: public class ParserThread extends Thread {
097:
098: protected final int FITERATIONS = 100;
099: protected File fXMLFile;
100: protected boolean fError;
101: protected int fParserType;
102:
103: public ParserThread(int parserType, File file) {
104: fError = false;
105: fParserType = parserType;
106: fXMLFile = file;
107: }
108:
109: public void run() {
110:
111: if (fParserType == ParserWrapperTestCase.FSAX) {
112: runSAX();
113: } else {
114: runDOM();
115: }
116:
117: }
118:
119: public void runSAX() {
120:
121: for (int x = 0; x < FITERATIONS; x++) {
122:
123: try {
124: XMLDefaultHandler handler = new XMLDefaultHandler();
125: SAXParserWrapper parser = new SAXParserWrapper();
126: parser.parse(fXMLFile, handler);
127: parser.dispose();
128: } catch (ParserConfigurationException e) {
129: e.printStackTrace();
130: fError = true;
131: } catch (SAXException e) {
132: e.printStackTrace();
133: fError = true;
134: } catch (FactoryConfigurationError e) {
135: e.printStackTrace();
136: fError = true;
137: } catch (IOException e) {
138: e.printStackTrace();
139: fError = true;
140: }
141: // If an error was encountered abort the thread
142: // Any type of exception experienced is bad
143: if (fError)
144: return;
145:
146: }
147:
148: }
149:
150: public void runDOM() {
151:
152: for (int x = 0; x < FITERATIONS; x++) {
153:
154: try {
155: DOMParserWrapper parser = new DOMParserWrapper();
156: parser.parse(fXMLFile);
157: parser.dispose();
158: } catch (ParserConfigurationException e) {
159: e.printStackTrace();
160: fError = true;
161: } catch (SAXException e) {
162: e.printStackTrace();
163: fError = true;
164: } catch (FactoryConfigurationError e) {
165: e.printStackTrace();
166: fError = true;
167: } catch (IOException e) {
168: e.printStackTrace();
169: fError = true;
170: }
171: // If an error was encountered abort the thread
172: // Any type of exception experienced is bad
173: if (fError)
174: return;
175:
176: }
177: }
178:
179: public boolean getError() {
180: return fError;
181: }
182:
183: }
184:
185: }
|