Apache Ant

Apache Ant 활용한 라이브러리 테스트 모듈 작성기

tmxhsk99 2022. 6. 24. 15:47

사내에서 라이브러리를 배포할 때 급한 에러 수정이나 버그수정이 일어날 경우 

충분한 테스트 없이 배포에 들어가면 다른 로직에 영향을 주는 경우가 있고 실제로 중요로직이 잘못 된 상태로 나가서

다시 배포하는 일이 1년에 3,4 건은 일어난다.

그냥 JUnit 코드를 실행 후 배포하면 되지만 개발자가 아닌 경우 소스코드에 접근할 수 없기 때문에

해당 라이브러리가 정상적인지 확인하기 힘들기 때문에 라이브러리 테스트 모듈을 

Apache Ant 를 활용해서 JUnit 테스트를 실행후 결과값을 생성해주는 모듈을 작성해보았다. 

구성 

라이브러리 검증 테스터 구성도

 

중요한 Build.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<projectname="junit Test"default="run"basedir=".">
        <!-- project ibrary path -->
        <property name="lib.dir"value="..\project\lib"/>
        <!-- project test source path -->
        <property name="src.test.dir"value="..\project\test"/>
        <!-- project test source home path -->
        <property name="src.home.dir"value="..\project"/>
        <!-- project test data path -->
        <property name="test.pdf.dir"value="..\project\data"/>
        <!-- project make jar path -->
        <property name="src.jar.src.dir"value="..\project\src"/>
        
        <target name="cleanup">
                <delete dir="bin_test"/>
                <delete dir="bin_src"/>
                <delete dir="lib"/>
                <delete dir="src_test"/>
                <delete dir="src_project"/>
                <delete dir="data"/>
        </target>
        <!-- make build Date-->
        <target name="prepare" depends="cleanup">
                <tstamp>
                        <format property="DSTAMP"pattern="yyyy.MM.dd"/>
                        <format property="TSTAMP"pattern="HH_mm_ss"/>
                </tstamp>
                <echo message=" ______       _ _     _   _____ _             _   _ "/>
                <echo message=" | ___ \     (_) |   | | /  ___| |           | | | |"/>
                <echo message=" | |_/ /_   _ _| | __| | \ `--.| |_ __ _ _ __| |_| |"/>
                <echo message=" | ___ \ | | | | |/ _` |  `--. \ __/ _` | '__| __| |"/>
                <echo message=" | |_/ / |_| | | | (_| | /\__/ / || (_| | |  | |_|_|"/>
                <echo message=" \____/ \__,_|_|_|\__,_| \____/ \__\__,_|_|   \__(_)"/>
                <echo message="Build Date : ${DSTAMP} ${TSTAMP}"/>
                <property name="builddate"value="${DSTAMP} ${TSTAMP}"/>
        </target>
        
        <target name="create"depends="prepare">
                <mkdir dir="bin_test"/>
                <mkdir dir="bin_src"/>
                <mkdir dir="lib"/>
                <mkdir dir="src_test"/>
                <mkdir dir="src_project"/>
                <mkdir dir="data"/>
                <mkdir dir="reports"/>
        </target>
        
        <targetname="copy"depends="create">
                <copyoverwrite="true"todir="lib">
                        <filesetdir="${lib.dir}"includes="gson-2.5.jar"></fileset>
                        <filesetdir="${lib.dir}"includes="hamcrest-core-1.3.jar"></fileset>
                        <filesetdir="${lib.dir}"includes="junit-4.12.jar"></fileset>
                         <!-- 실제 source 라이브러리 jar -->
                        <filesetdir="${lib.dir}"includes="project_lib.jar"></fileset>
                        <filesetdir="${lib.dir}"includes="commons-logging-1.2.jar"></fileset>
                </copy>
                <copyoverwrite="true"todir="src_test">
                        <filesetdir="${src.test.dir}"includes="*.java"></fileset>
                </copy>
                <!-- source 를 사용하는 경우 -->
                <!--
                <copy overwrite="true" todir="src_project">
                        <fileset dir="${src.jar.src.dir}" >
                                <include name="**/*.java"/>
                                <include name="**/*.afm"/>
                                <include name="**/*.txt"/>
                                <include name="**/*.properties"/>
                                <include name="**/*.html"/>
                                <include name="**/*.ttf"/>
                        </fileset>
                </copy>
                -->
                <copy overwrite="true"todir="data">
                        <filesetdir="${test.pdf.dir}"includes="TestData1.pdf"></fileset>
                        <filesetdir="${test.pdf.dir}"includes="TestData2.pdf"></fileset>
                        <filesetdir="${test.pdf.dir}"includes="TestData3.pdf"></fileset>
                </copy>

        </target>
        <!--
        <target name="src-compile" depends="copy">
                <javac classpathref="classpath" includeantruntime="true" srcdir="src_project" destdir="bin_src"  includes="com\project\**\*" encoding="UTF-8"></javac>
        </target>
        -->
        <!-- jar file 컴파일 전에 java 파일이아닌 resource를 복사해야된다.-->
<!--         
        <target name="resource-copy" depends="src-compile">
                <copy overwrite="true" todir="bin_src">
                        <fileset dir="src_project">
                                <include name="**/*.afm"/>
                                <include name="**/*.txt"/>
                                <include name="**/*.properties"/>
                                <include name="**/*.html"/>
                                <include name="**/*.ttf"/>
                        </fileset>
                </copy>
        </target>
-->
        <!--
        <target name="jar" depends="resource-copy">
                <jar jarfile="lib/project_lib.jar" basedir="bin_src" includes="**/*">
                        <manifest>                                
                                <attribute name="Last-Updated-Date" value="${builddate}"/>
                        </manifest>
                </jar>
        </target>
        -->
        <!-- 생성된 jar 파일 buildData 명으로 복사-->
        <!--
        <target name="jar_history" depends="jar">
                <mkdir dir="lib/build_history/${builddate}"/>
                <copy file="lib/Proejct_liba.jar" tofile="lib/build_history/${builddate}/project_lib.jar"/>                        
        </target>
-->
        <path id="classpath">
                <path location="lib">
                        <fileset dir="lib"includes="*.jar"></fileset>
                </path>
                <path location="bin_test"></path>
        </path>
        
        <target name="junit_complie"depends="copy">
                <javac fork="true"classpathref="classpath"includeantruntime="true"srcdir="src_test"destdir="bin_test"description="Complie Java files"includes="*.java"encoding="UTF-8"memoryMaximumSize="2048m">
                </javac>
        </target>
        
        <target name="junit_test"depends="junit_complie">
                <junit haltonfailure="false"printsummary="true">
                        <classpath refid="classpath"></classpath>
                        <test name="Junit Test 파일 명"todir="reports"></test>
                        <test name="Junit Test 파일 명 2"todir="reports"></test>
                        <formatter type="xml"/>
                </junit>
                 <junitreport todir="reports">
            <fileset dir="reports">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames"todir="reports/html"/>
        </junitreport>
        </target>
        
        <target name="reports_history"depends="junit_test">
                <mkdir dir="reports/reports_history/${builddate}"/>
                <copy overwrite="true"todir="reports/reports_history/${builddate}">
                        <fileset dir="reports">
                                <include name="TEST-*.xml"/>
                        </fileset>
                </copy>
        </target>
        <target name="run"depends="reports_history">
        </target>
</project>

실행 스크립트 start.bat

@echo off
set local_dir=%cd%
set ANT_HOME=%local_dir%/apache-ant-1.9.16
set PATH=%ANT_HOME%/bin
echo Lib_Tester Start
%PATH%/ant run
echo Lib_Tester End
pause

1. 테스트샘플 프로젝트/lib 경로에 테스트 하고 싶은 라이브러리.jar 파일을 위치 시킨다.
2. start.bat 실행시 build.xml 파일을 읽어 들여 테스트샘플 프로젝트 폴더의 항목을 복사 및 컴파일 테스트 코드를 실행한다
3. 검증 결과 는 ./ANT/reports/ 경로에 html 파일 형식 및 xml 파일 형식으로 저장된다.

 

 환경 확인 및 구동
1. 커맨드 창 에서 java version 을 쳐서 해당 PC 에 자바가 설치 되어 있는지 확인한 다 자바가 기본설치 필요
2. 테스트샘플 프로젝트/lib 경로에 테스트 할 project_lib.jar 파일을 위치 시킨다
3. ./ANT/satrt.bat 을 실행 한다
4. 실 행 후 ./ANT reports 디렉터리에 테스트 결과가 저장 된다
(예 ANT/reports/html/0 _Test.html)

구동 결과

컴파일 및 빌드 후 Junit 테스트를 진행 된 뒤 테스트 결과가 

report 디렉터리에 html 형식으로 junit 테스트 결과가 보여진다.

중요 로직 문제로 다시 배포하는 일은 년에 3~4건 정도는 있었던것 같은데 

이제 중요 로직 관련 이슈로 다시 배포하는 일은 아직 없는 것 같다.