Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: my first steps in "professional" development

  1. #1
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Lightbulb my first steps in "professional" development

    So the other day I was playing with Maven. It has long been a thing I wanted to do but could not get around.

    I haven't used Maven yet though. But I have a book on it.

    I discovered that Maven can indeed be configured for different directories (different directory structure) but it is a bit spooky.

    There are these

    "<build><sourceDir> ... </sourceDir></build>"

    Tags that you can use. But there are only four: one for sources, one for classes, one for test sources, and one for test classes.

    Now I also downloaded and installed Ant. I also ordered a book on Ant. It is not a very good book, but it is O'Reilly's "the definitive guide" and it is really cheap (I hope I'm not getting scammed here) from a Dutch retailer.

    And I started reading http://www.tutorialspoint.com/ant/ant_tutorial.pdf which is as far as I can see a good tutorial.

    =============

    Then, I thought I should or could commence using JUnit. I downloaded JUnit and Hamcrest and put it in my "/lib/" folder. Well, Maven had already downloaded it, but it is sitting in its own local repo. I wanted it in my project tree. Then I went about trying to create a first, nonsensical test class.

    I just picked a somewhat random class which was like a collection (list) and attempted to test it. It required me opening a Server socket, then connecting to it with a client socket, and use that client socket as a parameter to a "Connection" class I was making and then add that Connection to the ConnectionList and then ....

    I guess I'll just provide a snippet. This has been somewhat fun.

    I use a "before" method to set up the ServerThread and open the serversocket and start accepting a connection.

    Java Code:
    	@Before
    	public void initialize() {
    		st = new ServerThread(PORT);
    		st.start();
    	}
    The "after" method just closes the socket, which causes the thread to exit:

    Java Code:
    	@After
    	public void stop() {
    		try {
    			st.ss.close();
    		} catch (java.io.IOException e) {
    		}
    	}
    Meanwhile the @test method -- I have only one -- just performs a triple of tests using an Observer. Most of my "data" classes are (or will be) Observables. It is a nice methodology, I believe and think still, to be updated about "status messages" regarding the modifications or state-changes of each instance. I'm not sure if it is still used a lot.

    Java Code:
    	@Test
    	public void addAndObserve() {
    		ConnectionList cl = new ConnectionList(10);
    		try {
    			java.net.Socket s = new java.net.Socket(java.net.InetAddress.getLocalHost(), PORT);
    			// we're a client now.
    			Connection c = new Connection(s);
    			cl.addObserver(new java.util.Observer() {
    					public void update(java.util.Observable obs, Object o) {
    						ConnectionList.Event e = (ConnectionList.Event)o;
    						assertEquals(ConnectionList.Message.ADD, e.message);
    						assertEquals(c, e.object);
    						assertEquals(1, cl.size());
    					}
    			});
    			
    			cl.add(c);
    		} catch (java.io.IOException e) {
    			System.out.println("Failed to connect to server socket");
    		}
    	}
    Then, currently, I have a few batch files (this is Windows) to run the things:

    Java Code:
    @javac -encoding utf8 -sourcepath test\src -cp bin;lib\junit-4.12.jar;lib\hamcrest-core-1.3.jar -d test\bin test\src\%1
    Java Code:
    @java -cp bin;test\bin;lib\junit-4.12.jar;lib\hamcrest-core-1.3.jar org.junit.runner.JUnitCore %1
    My directory structure is simply (different from Maven):

    Java Code:
    /src/
    /bin/
    /lib/
    /test/src/
    /test/bin/
    I'm going to use Ant now to automate this thing. However, I have already found that I can use JUnit test suites:

    Java Code:
    package thunderbolt;
    
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses({
      ConnectionListTest.class
    })
    
    public class ThunderboltTestSuite {
      // the class remains empty,
      // used only as a holder for the above annotations
    }
    This means I can just run this (single) test using:

    Java Code:
    rt thunderbolt.ThunderboltTestSuite
    And it produces

    Java Code:
    JUnit version 4.12
    .
    Time: 0,053
    
    OK (1 test)
    Or I can output some text in the test method:

    Java Code:
    JUnit version 4.12
    .Testing ConnectionList add() and observable
    
    Time: 0,051
    
    OK (1 test)
    And I wonder how to do that in a better way, to get some information as to what's actually going on.

    Now next is the dual task of finding out how I can compile ALL test source files (classes) without a single entry point (but it shouldn't be hard), just...

    Java Code:
    ct thunderbolt\*.java
    ..which means I now have two commands to compile and test this (package). So much for Maven :p.

    And the next is of course how to run it. But Ant should be easy:

    It's a tad long. But I can now compile, clean, even run, test-compile, test-clean and test. It's wonderful :).

    Java Code:
    <?xml version="1.0"?>
    <project name="Thunderbolt Chat Server" basedir="." default="info">
    	<target name="info">
    		<echo>This is the ant script for project Thunderbolt</echo>
    	</target>
    	
    	<property name="src.dir" value="src" />
    	<property name="build.dir" value="bin" />
    	<property name="lib.dir" value="lib" />
    	<property name="test.src.dir" value="test/src" />
    	<property name="test.build.dir" value="test/bin" />
    	
    	<path id="master-classpath">
    		<fileset dir="${lib.dir}">
    			<include name="*.jar" />
    		</fileset>
    		<pathelement path="${build.dir}" />
    	</path>
    
    	<path id="test-classpath">
    		<path refid="master-classpath" />
    		<pathelement path="${test.build.dir}" />
    	</path>
    	
    	<target name="clean">
    		<delete>
    			<fileset dir="${build.dir}">
    				<include name="**/*.class"/>
    			</fileset>
    		</delete>
    	</target>
    	
    	<target name="clean-main">
    	    <delete>
    	        <fileset dir="${build.dir}">
    	            <include name="thunderbolt/Server.class" />
    	        </fileset>
    	    </delete>
    	</target>
    	
    	<target name="compile" depends="clean-main">
    		<mkdir dir="${build.dir}" />
    		<javac destdir="${build.dir}" includeantruntime="false">
    			<src path="${src.dir}" />
    			<include name="thunderbolt/Server.java" />
    			<classpath refid="master-classpath" />
    		</javac>        
    	</target>
    			
    	<target name="test-compile" depends="test-clean">
    		<mkdir dir="${test.build.dir}" />
    		<javac destdir="${test.build.dir}" includeantruntime="false">
    			<src path="${test.src.dir}" />
    			<classpath refid="test-classpath" />
    		</javac>        
    	</target>
    	
    	<target name="test-clean">
    	    <delete>
    	        <fileset dir="${test.build.dir}">
    	            <include name="**/*.class"/>
    	        </fileset>
    	    </delete>
    	</target>
    
    	<target name="test">
    	    <junit printsummary="true" showoutput="true">
    	        <test name="thunderbolt.ThunderboltTestSuite" />
    	        <classpath refid="test-classpath" />
    	    </junit>
    	</target>
    	
    	<target name="run">
    	    <java classname="thunderbolt.Server">
    	        <classpath refid="master-classpath" />
    	    </java>
    	</target>
    	    
    </project>
    I still prefer single-point-of-entry compilation. The downside for Ant (and Maven, I guess) is that ant will by default compare the source files against the class files and only compile a given source file if the class file is old or missing. However, with single point of entry compilation, this is a problem, since you will require a "clean" after every (failed) compile. The solution is simple though: another clean target that cleans out the main file. This way javac will just retry the compilation for every file that is missing upon each recompilation, without requiring a further clear, like I am used to. I still haven't achieved this for the test classes though.

  2. #2
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: my first steps in "professional" development

    Now the "ant test" produces:

    Java Code:
    Buildfile: C:\<path>\thunderbolt\build.xml
    
    test:
        [junit] Running thunderbolt.ThunderboltTestSuite
        [junit] Testing ConnectionList add() and observable
        [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0,062 sec
    
    BUILD SUCCESSFUL
    Total time: 0 seconds
    All of this was pretty easy. No [Moderator edit: word deleted] clue how I could have accomplished it with Maven. I still want to enable Maven to do the same things, but I don't think it is entirely possible.

    Thanks to http://www.tutorialspoint.com/ant/ant_tutorial.pdf for the excellent tutorial and the Ant website at https://ant.apache.org/manual/ which is actually quite good (I heard someone complain about it).
    Last edited by DarrylBurke; 08-04-2015 at 06:48 AM. Reason: Removed a proscribed word

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: my first steps in "professional" development

    Please see the 5th paragraph of the Forum Rules. Changing one letter does not exempt the word.

    I've edited your post. Please don't do this again.

    db
    jim829 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: my first steps in "professional" development

    Quote Originally Posted by Xen View Post
    I still want to enable Maven to do the same things, but I don't think it is entirely possible.
    You're comparing apples and oranges, Maven and ANT are not the same tools solving the same problems. Maven for example has an antrunner plugin in which you can invoke ANT tasks which generally makes it possible to do non-maven things without having to build plugins for it.

    As for figuring out how Maven works: http://www.amazon.com/Maven-Definiti...dp/0596517335/

    This is the older Maven 2 version (still relevant), the newer edition is not out yet.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: my first steps in "professional" development

    Quote Originally Posted by DarrylBurke View Post
    Please see the 5th paragraph of the Forum Rules. Changing one letter does not exempt the word.

    I've edited your post. Please don't do this again.

    db
    I'll change three letters next time, so you still get a hint of what I'm trying to convey ;D.

    (Seriously.... how to censor people in any meaningless way).

    [Moderator edit: removed part of the post that is in violation of the Forum Rules.]

    Anyway.


    Quote Originally Posted by gimbal2 View Post
    You're comparing apples and oranges, Maven and ANT are not the same tools solving the same problems. Maven for example has an antrunner plugin in which you can invoke ANT tasks which generally makes it possible to do non-maven things without having to build plugins for it.

    As for figuring out how Maven works: Maven: The Definitive Guide: Sonatype Company: 9780596517335: Amazon.com: Books

    This is the older Maven 2 version (still relevant), the newer edition is not out yet.
    So would you say Maven is mostly meant for dependency management?
    Last edited by DarrylBurke; 08-04-2015 at 09:34 PM. Reason: Removed offensive test

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: my first steps in "professional" development

    Quote Originally Posted by Xen View Post
    (Seriously.... how to censor people in any meaningless way).
    I've removed the offending part of your post. Any more of that and you will be banned. No further warning will be given.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 11-04-2012, 09:10 PM
  3. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  4. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •