001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.api.templates;
029:
030: import freemarker.ext.beans.BeansWrapper;
031: import freemarker.template.Configuration;
032: import freemarker.template.Template;
033: import java.io.BufferedWriter;
034: import java.io.File;
035: import java.io.FileOutputStream;
036: import java.io.FileWriter;
037: import java.io.InputStreamReader;
038: import java.io.OutputStreamWriter;
039: import java.io.Writer;
040: import java.util.HashMap;
041: import javax.script.ScriptContext;
042: import javax.script.ScriptEngine;
043: import javax.script.ScriptEngineManager;
044: import junit.framework.Test;
045: import org.netbeans.junit.NbTestCase;
046: import org.netbeans.junit.NbTestSuite;
047: import org.openide.filesystems.FileObject;
048: import org.openide.filesystems.LocalFileSystem;
049:
050: /**
051: *
052: * @author Jaroslav Tulach
053: */
054: public class SpeedTest extends NbTestCase {
055: private HashMap<String, String> parameters;
056: private FileObject fo;
057: private File[] whereTo;
058: private ScriptEngine eng;
059:
060: public SpeedTest(String name) {
061: super (name);
062: }
063:
064: public static Test suite() {
065: return NbTestSuite.speedSuite(SpeedTest.class, 2, 3);
066: }
067:
068: @Override
069: protected void setUp() throws Exception {
070: clearWorkDir();
071:
072: FileWriter w = new FileWriter(new File(getWorkDir(),
073: "template.txt"));
074: w.write("<html><h1>${title}</h1></html>");
075: w.close();
076:
077: parameters = new HashMap<String, String>();
078: parameters.put("title", "SOME_TITLE");
079:
080: LocalFileSystem lfs = new LocalFileSystem();
081: lfs.setRootDirectory(getWorkDir());
082: fo = lfs.findResource("template.txt");
083:
084: ScriptEngineManager mgr = new ScriptEngineManager();
085: eng = mgr.getEngineByName("freemarker");
086: assertNotNull("We do have such engine", eng);
087: eng.getContext().setAttribute(FileObject.class.getName(), fo,
088: ScriptContext.ENGINE_SCOPE);
089: eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE)
090: .putAll(parameters);
091:
092: whereTo = new File[10000];
093: for (int i = 0; i < whereTo.length; i++) {
094: whereTo[i] = new File(getWorkDir(), "outFile" + i + ".txt");
095: }
096: }
097:
098: public void testSpeedOfFreemarker() throws Exception {
099: Configuration cfg = new Configuration();
100: cfg.setDirectoryForTemplateLoading(getWorkDir());
101: cfg.setObjectWrapper(new BeansWrapper());
102: Template templ = cfg.getTemplate("template.txt");
103:
104: for (int i = 0; i < whereTo.length; i++) {
105: Writer out = new BufferedWriter(new FileWriter(whereTo[i]));
106: templ.process(parameters, out);
107: out.close();
108: }
109: }
110:
111: public void testSpeedThruIntegration() throws Exception {
112: for (int i = 0; i < whereTo.length; i++) {
113: Writer w = new BufferedWriter(new FileWriter(whereTo[i]));
114: eng.getContext().setWriter(w);
115: InputStreamReader is = new InputStreamReader(fo
116: .getInputStream());
117: eng.eval(is);
118: is.close();
119: w.close();
120: }
121: }
122: }
|