001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package examples;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029: import org.picocontainer.PicoContainer;
030: import org.picocontainer.defaults.DefaultPicoContainer;
031: import org.radeox.api.engine.RenderEngine;
032: import org.radeox.api.engine.context.InitialRenderContext;
033: import org.radeox.api.engine.context.RenderContext;
034: import org.radeox.engine.BaseRenderEngine;
035: import org.radeox.engine.context.BaseRenderContext;
036: import org.radeox.engine.context.BaseInitialRenderContext;
037:
038: import java.util.Locale;
039: import java.util.Enumeration;
040: import java.util.ResourceBundle;
041:
042: /**
043: * Example which shows howto use Radeox with PicoContainer
044: *
045: * @author Stephan J. Schmidt
046: * @version $Id: PicoContainerExample.java 4158 2005-11-25 23:25:19Z ian@caret.cam.ac.uk $
047: */
048:
049: public class PicoContainerExample extends RadeoxTestSupport {
050: public PicoContainerExample(String name) {
051: super (name);
052: }
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056: }
057:
058: public static Test suite() {
059: return new TestSuite(PicoContainerExample.class);
060: }
061:
062: public void testPicoContainer() {
063: // cut:start-1
064: DefaultPicoContainer dc = new DefaultPicoContainer();
065: try {
066: // Register BaseRenderEngine as an Implementation
067: // of RenderEngine
068: dc.registerComponentImplementation(RenderEngine.class,
069: BaseRenderEngine.class);
070: } catch (Exception e) {
071: System.err.println("Could not register component.");
072: }
073:
074: // now only work with container
075: PicoContainer container = dc;
076:
077: // Only ask for RenderEngine, we automatically
078: // get an available object
079: // that implements RenderEngine
080: RenderEngine engine = (RenderEngine) container
081: .getComponentInstance(RenderEngine.class);
082: RenderContext context = new BaseRenderContext();
083: String result = engine.render("__SnipSnap__", context);
084: // cut:end-1
085: assertEquals("Rendered with PicoContainer.",
086: "<b class=\"bold\">SnipSnap</b>", result);
087: }
088:
089: public void testPicoWithInitialRenderContext() {
090: // cut:start-2
091: DefaultPicoContainer dc = new DefaultPicoContainer();
092: try {
093: InitialRenderContext initialContext = new BaseInitialRenderContext();
094: initialContext.set(RenderContext.OUTPUT_LOCALE, new Locale(
095: "mywiki", "mywiki"));
096: dc.registerComponentInstance(InitialRenderContext.class,
097: initialContext);
098: dc.registerComponentImplementation(RenderEngine.class,
099: BaseRenderEngine.class);
100: } catch (Exception e) {
101: System.err.println("Could not register component.");
102: }
103: // cut:end-2
104: // now only work with container
105: PicoContainer container = dc;
106:
107: // Only ask for RenderEngine, we automatically
108: // get an available object
109: // that implements RenderEngine
110: RenderEngine engine = (RenderEngine) container
111: .getComponentInstance(RenderEngine.class);
112:
113: assertNotNull("Component found.", engine);
114: RenderContext context = new BaseRenderContext();
115: String result = engine.render("__SnipSnap__", context);
116: assertEquals(
117: "Rendered with PicoContainer and otherwiki Locale.",
118: "<b class=\"mybold\">SnipSnap</b>", result);
119:
120: }
121: }
|