<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 柯以測試的內容: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 閱讀(2325) 評論(0)  編輯  收藏 所屬分類: SRPING BATCH

    主站蜘蛛池模板: 亚洲AV无码国产丝袜在线观看| 亚洲无人区码一二三码区别图片| 1000部拍拍拍18勿入免费视频下载 | 精品亚洲成α人无码成α在线观看 | 成人免费午间影院在线观看| 成人精品综合免费视频| 亚洲国产精品不卡在线电影| 在线免费观看污网站| 爽爽爽爽爽爽爽成人免费观看| 亚洲va在线va天堂va手机| 亚洲精品成人网久久久久久| 精品国产污污免费网站aⅴ| 免费福利在线观看| 亚洲精品国产手机| 亚洲视频在线一区二区| 亚洲日韩欧洲无码av夜夜摸| 中国在线观看免费高清完整版| 国产精品偷伦视频免费观看了| 国产成人精品日本亚洲直接| 中国亚洲女人69内射少妇| 妞干网免费视频在线观看| 久久久久久免费一区二区三区| 亚洲a无码综合a国产av中文| 亚洲精品国产第1页| 亚洲中文字幕无码不卡电影| 日韩免费无码一区二区视频| 久久久久久国产精品免费无码| 青青久久精品国产免费看| 国产成人精品日本亚洲专一区| 亚洲动漫精品无码av天堂| 免费一级毛片不卡在线播放| 成人黄色免费网站| 99热免费在线观看| a视频在线观看免费| 日日躁狠狠躁狠狠爱免费视频| 国产 亚洲 中文在线 字幕| 亚洲精品在线观看视频| 亚洲人成网亚洲欧洲无码久久| 日本免费v片一二三区| 国产一卡2卡3卡4卡2021免费观看 国产一卡2卡3卡4卡无卡免费视频 | 四虎永久免费地址在线网站|