User Tools

Site Tools


programming:java:java

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming:java:java [2023/10/30 07:46] skipidarprogramming:java:java [2023/11/02 14:33] (current) – [Records] skipidar
Line 1065: Line 1065:
  
  
 +
 +===== JUnit 5 =====
 +
 +<sxh groovy>
 +
 +</sxh>
 +
 +
 +===== Hamcrest =====
 +
 +Has matches useful for unit tests.
 +
 +Set the groovy dependencies
 +<sxh groovy>
 +dependencies {
 +    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
 +    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
 +
 +    // import here
 +    testImplementation 'org.hamcrest:hamcrest:2.2'
 +}
 +</sxh>
 +
 +Now you can import "org.hamcrest"
 +
 +<sxh java>
 +import static org.hamcrest.MatcherAssert.assertThat;
 +import static org.hamcrest.Matchers.*;
 +
 +import org.junit.jupiter.api.Test;
 +
 +public class MyTest {
 +
 +    @Test
 +    void testExample() {
 +        String value = "Hello, world!";
 +        
 +        assertThat(value, containsString("Hello"));
 +        assertThat(value, endsWith("!"));
 +        assertThat(value, is(notNullValue()));
 +    }
 +}
 +</sxh>
 +
 +
 +===== Text processing =====
 +
 +==== Regular Expressions (java.util.regex) ====
 +
 +When using Regex - make sure you dont introduce too greedy patterns
 +
 +https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
 +<code>
 +Boundary matchers
 +^ The beginning of a line
 +$ The end of a line
 +\b A word boundary
 +\B A non-word boundary
 +\A The beginning of the input
 +\G The end of the previous match
 +\Z The end of the input but for the final terminator, if any
 +\z The end of the input
 +</code>
 +
 +
 +==== Tokenize strings ====
 +
 +Break into single words. 
 +
 +<sxh java>
 +
 +        String[] result = "this is a test".split("\\s");
 +        for (int x=0; x<result.length; x++)
 +            System.out.println(result[x]);
 +
 +/*      this
 +        is
 +        a
 +        test
 +*/
 +
 +</sxh>
 +
 +
 +<sxh java>
 +    @Test
 +    void patternMatch() {
 +        mymatch(".*(alice|bob).*", "bob and alice like to text each other" ); // 1 - because greedy match matches the whole string
 +        mymatch("\\b(alice|bob)\\b", "bob and alice like to text each other" ); // 2 - ACHTUNG: escape back slashes. \b marks word boundaries
 +    }
 +
 +    public static long mymatch(String regex, String text) {
 +        Pattern pattern = Pattern.compile(regex);
 +        Matcher matcher = pattern.matcher(text);
 +
 +        List<MatchResult> res =  matcher
 +                .results()
 +                .toList();
 +
 +        System.out.printf("%s matches %s%n", regex, res.size());
 +        return res.size();
 +    }
 +
 +</sxh>
 +
 +==== ReplaceAll  ====
 +
 +<sxh java>
 +    @Test
 +    void stringReplaceAll() {
 +        String s = "bob and alice like to text each other";
 +        String john = s.replaceAll("\\b(bob)\\b", "john");
 +        System.out.println(john);
 +    }
 +
 +</sxh>
 +
 +
 +===== Records =====
 +First: Java 14
 +
 +
 +
 +<sxh java>
 +    // some interface
 +    interface MyBarkInterface{
 +        default void bark(){
 +            System.out.println("bark bark");
 +        }
 +    }
 +
 +    record MyPoint(int x, int y) implements MyBarkInterface{};
 +
 +    // Cant inherit from Record - its final
 +    //record MyPoint2(int x, int y) extends MyPoint{};
 +
 +
 +    @Test
 +    void recordTest1() {
 +        MyPoint p = new MyPoint(1,2);
 +
 +        System.out.println(p); // MyPoint[x=1, y=2]
 +        System.out.println(p.x()); // can access vars via methods
 +        System.out.println(p.x()); // can access vars via methods
 +        
 +        p.bark(); // can access hiearchy
 +    }
 +
 +</sxh>
programming/java/java.1698651982.txt.gz · Last modified: by skipidar