ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan.Mate...@rzf.fin-nrw.de
Subject Selector Test
Date Wed, 04 Jun 2003 08:00:50 GMT
When writing the testcase for my selector I missed some functionality in
BaseSelectorTest:
- transfering properties from JUnit test class to the Ant test project
- resolve the selection String ("TTTTT..FF") to filenames

I have done that with a local org.apache.tools.ant.BuildFileTest class (like
the TaskdefForMakingBed).

Should I put that in the BaseSelectorTest class?


Jan




public class CacheSelectorTest extends BaseSelectorTest {
    ...
    public void testScenario1() {
        try {
            ...
            // AccessObject to the test-Ant-environment
            bft = new BFT();
            // give some values (via property file) to that environment
            bft.writeProperties("f2name="+f2name);
            bft.writeProperties("f3name="+f3name);
            bft.writeProperties("f4name="+f4name);
            // call the target for making the files dirty
            bft.doTarget("cacheselectortest-makeDirty");

            ...

            String results = selectionString(s);
            assertEquals(
                "Wrong files selected. Differing files: "       // info text
                + resolve(diff(expected.toString(), results)),  // list of
files
                expected.toString(),                            // expected
result
                results                                         // result
            );

        } finally {
            // cleanup the environment
            cleanupBed();
            if (bft!=null) bft.deletePropertiesfile();
        }
    }

    //  ========================  Helper methods  ========================


    /**
     *  Checks which files are selected and shouldn´t be or which
     *  are not selected but should.
     *  @param expected    String containing 'F's and 'T's
     *  @param result      String containing 'F's and 'T's
     *  @return Difference as String containing '-' (equal) and
     *          'X' (difference).
     */
    public String diff(String expected, String result) {
        int length1 = expected.length();
        int length2 = result.length();
        int min = (length1 > length2) ? length2 : length1;
        StringBuffer sb = new StringBuffer();
        for (int i=0; i<min; i++) {
            sb.append(
                  (expected.charAt(i) == result.charAt(i))
                ? "-"
                : "X"
            );
        }
        return sb.toString();
    }


    /**
     *  Resolves a diff-String (@see diff()) against the (inherited)
filenames- and
     *  files arrays.
     *  @param filelist    Diff-String
     *  @return String containing the filenames for all differing files,
separated
     *          with semicolons ';'
     */
    public String resolve(String filelist) {
        StringBuffer sb = new StringBuffer();
        int min = (filenames.length > filelist.length()) ? filelist.length()
: filenames.length;
        for (int i=0; i<min; i++) {
            if ('X'==filelist.charAt(i)) {
                sb.append(filenames[i]);
                sb.append(";");
            }
        }
        return sb.toString();
    }


    private class BFT extends org.apache.tools.ant.BuildFileTest {
        BFT() { super("nothing"); }
        BFT(String name) {
            super(name);
        }
        String propfile = "CacheSelectorTest.properties";

        boolean isConfigured = false;

        public void setUp() {
            configureProject("src/etc/testcases/types/selectors.xml");
            isConfigured = true;
        }

        public void tearDown() { }

        public void doTarget(String target) {
            if (!isConfigured) setUp();
            executeTarget(target);
        }

        public void writeProperties(String line) {
            if (!isConfigured) setUp();
            File dir = getProject().getBaseDir();
            File file = new File(dir, propfile);
            try {
                java.io.FileWriter out = new java.io.FileWriter(file, true);
                out.write(line);
                out.write(System.getProperty("line.separator"));
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void deletePropertiesfile() {
            new File(getProject().getBaseDir(), propfile).delete();
        }
    }//class-BFT

}


--------------------------------


selector.xml

  <target name="cacheselectortest-makeDirty">
      <!-- Load propertyfile generated by SelectorTest-class -->
      <property file="CacheSelectorTest.properties"/>

      <!-- work with ${f2name}, ... -->
  </target>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message