001: /*
002: * Created on May 24, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package de.schlund.pfixxml.targets;
008:
009: import java.io.File;
010: import java.util.Iterator;
011: import java.util.Random;
012: import java.util.TreeSet;
013:
014: import junit.framework.TestCase;
015:
016: import org.w3c.dom.Document;
017:
018: import de.schlund.pfixxml.XMLException;
019: import de.schlund.pfixxml.config.GlobalConfigurator;
020: import de.schlund.pfixxml.resources.FileResource;
021: import de.schlund.pfixxml.resources.ResourceUtil;
022: import de.schlund.pfixxml.util.Xml;
023:
024: public class TargetGeneratorTest extends TestCase {
025: // TODO
026: private static final File DOCROOT = new File("projects")
027: .getAbsoluteFile();
028:
029: static {
030: GlobalConfigurator.setDocroot(DOCROOT.getAbsolutePath());
031: }
032:
033: public void testEmpty() throws Exception {
034:
035: TargetGenerator gen;
036:
037: gen = create("<make project='foo' lang='bar'/>");
038: assertEquals(0, gen.getAllTargets().size());
039: assertEquals("foo", gen.getName());
040: assertEquals("bar", gen.getLanguage());
041: assertNotNull(gen.getDisccachedir().toURI().getPath());
042: }
043:
044: public void testTarget() throws Exception {
045: TargetGenerator gen;
046:
047: gen = create("<make project='foo' lang='bar'>"
048: + " <navigation/> "
049: + " <target name='master.xsl' type='xsl'>"
050: + " <depxml name='core/xsl/master.xsl'/>"
051: + " <depxsl name='core/xsl/customizemaster.xsl'/>"
052: + " </target>" + "</make>");
053: assertEquals(3, gen.getAllTargets().size());
054: // TODO: more tests
055: }
056:
057: public void testXmlMissing() throws Exception {
058: createInvalid("<make project='foo' lang='bar'>"
059: + " <target name='master.xsl' type='xsl'>"
060: + " <depxsl name='core/xsl/customizemaster.xsl'/>"
061: + " </target>" + "</make>", "without [depxml]");
062: }
063:
064: public void testXslMissing() throws Exception {
065: createInvalid("<make project='foo' lang='bar'>"
066: + " <target name='master.xsl' type='xsl'>"
067: + " <depxml name='core/xsl/master.xsl'/>"
068: + " </target>" + "</make>", "without [depxsl]");
069: }
070:
071: public void testDocrootNoLongerAllowed() throws Exception {
072: createInvalid("<make project='foo' lang='bar'>"
073: + " <target name='master.xsl' type='xsl'>"
074: + " <depxml name='core/xsl/master.xsl'/>"
075: + " <depxsl name='core/xsl/customizemaster.xsl'/>"
076: + " <param name='docroot' value='foo'/>"
077: + " </target>" + "</make>",
078: "The docroot parameter is no longer allowed");
079: }
080:
081: public void createInvalid(String str, String msg) throws Exception {
082: String full;
083:
084: try {
085: create(str);
086: fail("'" + str + "' does not raise exception with '" + msg
087: + "'");
088: } catch (XMLException e) {
089: full = e.getMessage();
090: if (full.indexOf(msg) != -1) {
091: // ok
092: } else {
093: fail("exception message '" + full
094: + "' does not contain '" + msg + "'");
095: }
096: }
097: }
098:
099: private TargetGenerator create(String str) throws Exception {
100: TargetGenerator gen;
101: File file;
102:
103: Document doc = Xml.parseStringMutable(str);
104: file = File.createTempFile("depend", ".xml", new File(
105: "projects"));
106: file.deleteOnExit();
107: Xml.serialize(doc, file, true, true);
108: gen = new TargetGenerator(ResourceUtil.getFileResource(file
109: .toURI()));
110: return gen;
111: }
112:
113: public void testConcurrency() throws Exception {
114: File cacheDir = new File("projects/.cache");
115: File tmpCacheDir = new File("projects/.cache_tmp");
116: if (cacheDir.exists())
117: cacheDir.renameTo(tmpCacheDir);
118: try {
119: FileResource res = ResourceUtil.getFileResource(new File(
120: "projects/sample1/conf/depend.xml").toURI());
121: TargetGenerator generator = new TargetGenerator(res);
122: generator.setIsGetModTimeMaybeUpdateSkipped(true);
123: TreeSet<Target> topTargets = generator.getPageTargetTree()
124: .getToplevelTargets();
125: final Target[] targets = new Target[topTargets.size()];
126: Iterator<Target> it = topTargets.iterator();
127: for (int i = 0; i < targets.length; i++)
128: targets[i] = (Target) it.next();
129: int threadNo = 50;
130: final int requestNo = 10;
131: Thread[] threads = new Thread[threadNo];
132: for (int i = 0; i < threadNo; i++) {
133: final long seed = i;
134: final int max = targets.length;
135: Thread thread = new Thread() {
136: @Override
137: public void run() {
138: Random random = new Random(System
139: .currentTimeMillis()
140: / (seed + 1));
141: for (int j = 0; j < requestNo; j++) {
142: int ind = random.nextInt(max);
143: try {
144: Object obj = targets[ind].getValue();
145: assertNotNull(obj);
146: } catch (TargetGenerationException x) {
147: throw new RuntimeException("Error", x);
148: }
149: }
150: }
151: };
152: threads[i] = thread;
153: thread.start();
154: }
155: for (int i = 0; i < threadNo; i++)
156: threads[i].join();
157: } finally {
158: if (cacheDir.exists())
159: delete(cacheDir);
160: if (tmpCacheDir.exists())
161: tmpCacheDir.renameTo(cacheDir);
162: }
163: }
164:
165: private static boolean delete(File file) {
166: if (file.isDirectory()) {
167: File[] files = file.listFiles();
168: for (int i = 0; i < files.length; i++) {
169: delete(files[i]);
170: }
171: }
172: return file.delete();
173: }
174:
175: public static void main(String[] args) throws Exception {
176: new TargetGeneratorTest().testConcurrency();
177: }
178:
179: }
|