<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    SPRING BATCH 測試

    SPRING BATCH 柯以測試的內(nèi)容:JOB, STEP, INTEMPROCESSOR, ITEMREADER, ITEMWRITER。
    JOB, STEP屬于功能測試(黑盒)的范疇,INTEMPROCESSOR, ITEMREADER, ITEMWRITER屬于單元測試(白盒)的范疇。

    /*
     * Copyright 2006-2007 the original author or authors.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      
    http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     
    */
    package example;

    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertNotNull;

    import java.util.Date;
    import java.util.concurrent.Callable;

    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.batch.core.BatchStatus;
    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.core.JobParametersBuilder;
    import org.springframework.batch.core.StepExecution;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.batch.item.ExecutionContext;
    import org.springframework.batch.item.ItemReader;
    import org.springframework.batch.item.ItemStream;
    import org.springframework.batch.item.NonTransientResourceException;
    import org.springframework.batch.item.ParseException;
    import org.springframework.batch.item.UnexpectedInputException;
    import org.springframework.batch.test.JobLauncherTestUtils;
    import org.springframework.batch.test.MetaDataInstanceFactory;
    import org.springframework.batch.test.StepScopeTestExecutionListener;
    import org.springframework.batch.test.StepScopeTestUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.annotation.DirtiesContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.TestExecutionListeners;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

    @ContextConfiguration(locations = { "/test-context.xml",
            "classpath:/META-INF/spring/batch/hello-tasklet-context.xml",
            "classpath:/META-INF/spring/batch/jdbc-job-context.xml",
            "classpath:/META-INF/spring/integration/hello-integration-context.xml"})
    @RunWith(SpringJUnit4ClassRunner.class)
    //測試ITEMREADER/ITEMPROCESSOR/ITEMWRITER時用到
    @TestExecutionListeners( { DependencyInjectionTestExecutionListener.class
        StepScopeTestExecutionListener.class })
    public class HelloTaskletTests {
        
        @Autowired
        private JobLauncher jobLauncher;
        
        @Autowired
        private Job helloWorldJob;

        @Autowired
        private JobLauncherTestUtils jobLauncherTestUtils;//測試JOB/STEP的入口
        
        @Autowired
        private ItemReader xmlReader;
        
        public void testLaunchJobWithJobLauncher() throws Exception {
            JobExecution jobExecution = jobLauncher.run(helloWorldJob, new JobParameters());
            assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        }

        /**
         * Create a unique job instance and check it's execution completes
         * successfully - uses the convenience methods provided by the testing
         * superclass.
         
    */
        @Test
        public void testLaunchJob() throws Exception {

            JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobLauncherTestUtils.getUniqueJobParameters());
            assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        }
        
        public void testIntegration()
        {
            while(true)
            {
                
            }
        }
        
        /**
         * 測試某個STEP
         
    */
        @Test
        public void testSomeStep()
        {
            JobExecution jobExecution = jobLauncherTestUtils.
                    launchStep("xmlFileReadAndWriterStep",getJobParameters());
            assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        }
        
        /**
         * 測試READER的方式1時,所需的方法
         * 
    @return
         
    */
        public StepExecution getStepExecution() {
            StepExecution execution = MetaDataInstanceFactory
                    .createStepExecution(getJobParameters());
            return execution;
        }
        
        /**
         * 測試READER的方式1
         * 
    @throws Exception
         
    */
        @Test
        @DirtiesContext
        public void testReader() throws Exception {
            int count = StepScopeTestUtils.doInStepScope(getStepExecution(),
                    new Callable<Integer>() {
                        @Override
                        public Integer call() throws Exception {
                            int count = 0;
                            try {
                                ((ItemStream) xmlReader)
                                        .open(new ExecutionContext());
                                while (xmlReader.read() != null) {
                                    count++;
                                }
                                return count;
                            } finally {
                                ((ItemStream) xmlReader).close();
                            }
                        }
                    });
            assertEquals(3, count);
        }
          
        /**
         * 測試READER的方式2
         * 
    @throws UnexpectedInputException
         * 
    @throws ParseException
         * 
    @throws NonTransientResourceException
         * 
    @throws Exception
         
    */
        @Test
        @DirtiesContext
        public void testReader2() throws UnexpectedInputException, ParseException, NonTransientResourceException, Exception
        {
            assertNotNull(xmlReader.read());
        }
        
        /**
         * 測試READER的方式2時,必須加的方法
         
    */
        @Before
        public void setUp() {
            ((ItemStream) xmlReader).open(new ExecutionContext());
        }
          
        /**
         * 
         * 
    @return
         
    */
        private JobParameters getJobParameters() {
            
            String inputFile = "/Users/paul/Documents/PAUL/DOWNLOAD/SOFTWARE/DEVELOP/"
                    + "SPRING BATCH/spring-batch-2.1.9.RELEASE/samples/"
                    + "spring-batch-simple-cli/file/trades1.xml";
            
            String outputFile = "/Users/paul/Documents/PAUL/DOWNLOAD/SOFTWARE/DEVELOP/"
                    + "SPRING BATCH/spring-batch-2.1.9.RELEASE/samples/"
                    + "spring-batch-simple-cli/file/output/out.xml";
            
            JobParameters jobParameters = new JobParametersBuilder()
                    .addString("input.file.path", inputFile)
                    .addString("output.file.path", outputFile)
                    .addDate("date", new Date()).toJobParameters();
            
            return jobParameters;
        }


    }


    參考例子: 
    http://code.google.com/p/springbatch-in-action/source/browse/trunk/sbia/ch15/src/test/java/com/manning/sbia/ch15/batch/integration/?r=128#integration%2Fjob

    SPRING-BATCH TEST
    https://src.springframework.org/svn/spring-batch/trunk/spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/

    posted on 2012-11-10 17:25 paulwong 閱讀(2326) 評論(0)  編輯  收藏 所屬分類: SRPING BATCH

    主站蜘蛛池模板: 亚洲色大成网站www尤物| 精品国产免费观看一区| 高潮毛片无遮挡高清免费| 亚洲精品无码久久毛片波多野吉衣| 亚洲人成无码网站久久99热国产| 最新中文字幕免费视频| 99久久精品免费精品国产| 国产免费人成视频在线播放播 | 国产一区二区三区亚洲综合| 亚洲综合久久久久久中文字幕| 亚洲色大成网站www永久一区| 免费在线一级毛片| 青草草在线视频永久免费| 114一级毛片免费| 午夜影院免费观看| a级毛片黄免费a级毛片| 又硬又粗又长又爽免费看 | 国产免费拔擦拔擦8x| 毛片免费全部免费观看| 精品久久久久久久久免费影院| 91精品免费不卡在线观看| 可以免费观看的国产视频| 怡红院免费的全部视频| 成年免费大片黄在线观看com| 白白色免费在线视频| 国产亚洲精品美女久久久久久下载| 亚洲国产AV一区二区三区四区| 亚洲一卡2卡三卡4卡无卡下载| 亚洲精品一二三区| 国产成人精品日本亚洲专| 亚洲高清一区二区三区| 国产人成亚洲第一网站在线播放| 亚洲中文字幕无码中文字| 亚洲免费网站观看视频| 九九精品国产亚洲AV日韩| 白白色免费在线视频| 一级毛片免费在线观看网站| 中国一级毛片视频免费看| 男女午夜24式免费视频| 在线免费观看你懂的| 成年网站免费视频A在线双飞|