    1   package org.apache.stratum.scheduler;
    2
    3   /*
    4    * Copyright 2001-2005 The Apache Software Foundation or its licensors,
    5    * as applicable.
    6    *
    7    * Licensed under the Apache License, Version 2.0 (the "License");
    8    * you may not use this file except in compliance with the License.
    9    * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19
    20  import java.io.File;
    21  import java.io.FileInputStream;
    22
    23  import junit.framework.Test;
    24  import junit.framework.TestCase;
    25  import junit.framework.TestSuite;
    26  import junit.textui.TestRunner;
    27
    28  import org.apache.commons.betwixt.XMLIntrospector;
    29  import org.apache.commons.betwixt.io.BeanReader;
    30  import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
    31  import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
    32  import org.apache.commons.configuration.PropertiesConfiguration;
    33
    34  /***
    35   * Test harness for the Scheduler
    36   *
    37   * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
    38   * @version $Id: TestScheduler.java 264731 2005-08-30 08:04:32Z henning $
    39   */
    40  public class TestScheduler
    41          extends TestCase
    42  {
    43      /*** TODO: DOCUMENT ME! */
    44      private static final String XML = "src/test-conf/Scheduler.xml";
    45
    46      /*** TODO: DOCUMENT ME! */
    47      private static final String PROPERTIES = "src/test-conf/Scheduler.properties";
    48
    49      /***
    50       * TODO: DOCUMENT ME!
    51       *
    52       * @param args TODO: DOCUMENT ME!
    53       */
    54      public static void main(String [] args)
    55      {
    56          TestRunner.run(suite());
    57      }
    58
    59      /***
    60       * TODO: DOCUMENT ME!
    61       *
    62       * @return TODO: DOCUMENT ME!
    63       */
    64      public static Test suite()
    65      {
    66          return new TestSuite(TestScheduler.class);
    67      }
    68
    69      /***
    70       * Creates a new TestScheduler object.
    71       *
    72       * @param testName TODO: DOCUMENT ME!
    73       */
    74      public TestScheduler(String testName)
    75      {
    76          super(testName);
    77      }
    78
    79      /***
    80       * TODO: DOCUMENT ME!
    81       *
    82       * @throws Exception TODO: DOCUMENT ME!
    83       */
    84      public void testLoadXmlConfig()
    85              throws Exception
    86      {
    87          SchedulerConfig schedConf = new SchedulerConfig();
    88          FileInputStream in = new FileInputStream(new File(XML));
    89
    90          // create a new BeanReader
    91          BeanReader reader = createBeanReader();
    92
    93          SchedulerConfig schedConf2 = (SchedulerConfig) reader.parse(in);
    94
    95          assertNotNull("scheduler should not be null.", schedConf2);
    96          assertEquals("there should be 2 job configurations", 2, schedConf2.getJobConfigs().size());
    97          assertEquals("there should be 2 triggers", 2, schedConf2.getTriggerConfigs().size());
    98          assertTrue("scheduler name should be scheduler1", schedConf2.getInstanceName().equals("scheduler1"));
    99
    100         assertTrue("job name should be job1", ((JobConfig) (schedConf2.getJobConfigs().get(0))).getName().equals("job1"));
    101     }
    102
    103     /***
    104      * TODO: DOCUMENT ME!
    105      *
    106      * @throws Exception TODO: DOCUMENT ME!
    107      */
    108     public void testRunScheduler()
    109             throws Exception
    110     {
    111         Scheduler sched = new Scheduler();
    112         PropertiesConfiguration conf = new PropertiesConfiguration(PROPERTIES);
    113         sched.configure(conf);
    114
    115         try
    116         {
    117             sched.start();
    118             Thread.sleep(5000);
    119             sched.stop();
    120         }
    121         catch (InterruptedException e)
    122         {
    123             e.printStackTrace();
    124         }
    125     }
    126
    127     // Implementation methods
    128     //-------------------------------------------------------------------------
    129     protected BeanReader createBeanReader()
    130             throws Exception
    131     {
    132         BeanReader reader = new BeanReader();
    133         reader.setXMLIntrospector(createXMLIntrospector());
    134         reader.registerBeanClass(SchedulerConfig.class);
    135
    136         return reader;
    137     }
    138
    139     /***
    140      * ### it would be really nice to move this somewhere shareable across Maven / Turbine projects. Maybe a static helper method -
    141      * question is what to call it???
    142      *
    143      * @return TODO: DOCUMENT ME!
    144      */
    145     protected XMLIntrospector createXMLIntrospector()
    146     {
    147         XMLIntrospector introspector = new XMLIntrospector();
    148
    149         // set elements for attributes to true
    150         introspector.getConfiguration().setAttributesForPrimitives(false);
    151
    152         // wrap collections in an XML element
    153         //introspector.setWrapCollectionsInElement(true);
    154         // turn bean elements first letter into lower case
    155         introspector.getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
    156         introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper());
    157
    158         introspector.getConfiguration().setPluralStemmer(new DefaultPluralStemmer());
    159
    160         return introspector;
    161     }
    162 }