<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 閱讀(2342) 評論(0)  編輯  收藏 所屬分類: SRPING BATCH

    主站蜘蛛池模板: 国产精品麻豆免费版| 亚洲成人免费在线| 女人18毛片水真多免费看| 亚洲日韩中文字幕| 182tv免费观看在线视频| 久久亚洲美女精品国产精品 | 99视频有精品视频免费观看| MM131亚洲国产美女久久| av网站免费线看| caoporn成人免费公开| 亚洲av无码天堂一区二区三区| 亚洲AV无码成人专区片在线观看 | 亚洲Av无码精品色午夜| 久久精品一区二区免费看| 亚洲AV电影院在线观看| 91福利免费体验区观看区| 亚洲av乱码一区二区三区| 国产精品视频永久免费播放| 亚洲精品无码永久在线观看男男 | 搡女人真爽免费视频大全| 亚洲精品成a人在线观看☆| 曰批全过程免费视频在线观看无码 | 日本免费一本天堂在线| 黄色免费在线观看网址| 成人免费午夜视频| 国产青草亚洲香蕉精品久久| 无码区日韩特区永久免费系列| 亚洲www在线观看| 亚洲黄片毛片在线观看| 午夜爽爽爽男女免费观看影院| 亚洲av无码片区一区二区三区| 国产人妖ts在线观看免费视频| 九九视频高清视频免费观看 | 亚洲国产夜色在线观看| 黄网站免费在线观看| 亚洲视频人成在线播放| 久久成人免费大片| 亚洲aⅴ无码专区在线观看春色| 亚洲毛片网址在线观看中文字幕| 91香焦国产线观看看免费| 久久亚洲色WWW成人欧美|