You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Ja...@rzf.fin-nrw.de on 2003/06/04 10:00:50 UTC

Selector Test

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>

Re: Selector Test

Posted by Bruce Atherton <br...@callenish.com>.
At 10:00 AM 6/4/2003 +0200, Jan.Materne@rzf.fin-nrw.de wrote:
>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?

+1 for providing a way to translate to filenames. I'm sure that will be 
much easier to use when debugging a test failure. This should be easier to 
call from the test, though. Perhaps a new method in BaseSelectorTest that 
takes a selector and the selection string and calls assertEquals() the way 
you describe. If you could submit a patch against BaseSelectorTest for 
this, I'd be glad to apply it. If you could change the README while you 
were at it to describe how to use it, even better.

Regarding setting properties on the build that creates the test 
environment, I'm not sure I understand the necessity. Unit tests are quite 
static by design, to ensure consistent results. The more complexity you 
add, the more likely a test can fail in a way unrelated to the units the 
tests are testing. What makes you want to use passed-in properties?

Even if it is desirable to allow property settings to affect testcases, 
wouldn't it be better to add a setProperty() call in oata.BuildFileTest 
rather than creating a properties file? Something that called 
project.setUserProperty()?



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org