oblac / jodd-util

Essential Java utilities.
https://util.jodd.org
BSD 2-Clause "Simplified" License
39 stars 9 forks source link

Assignment 1: #8

Closed mkiefer10 closed 3 years ago

mkiefer10 commented 3 years ago

Part 2:

void testUTFReads() throws IOException { String content = FileUtil.readUTFString(new File(utfdataRoot, "utf-8.txt")); content = content.replace("\r\n", "\n");

    String content8 = FileUtil.readString(new File(utfdataRoot, "utf-8.txt"), StandardCharsets.UTF_8);
    content8 = content8.replace("\r\n", "\n");
    assertEquals(content, content8);

    String content1 = FileUtil.readUTFString(new File(utfdataRoot, "utf-16be.txt"));
    content1 = content1.replace("\r\n", "\n");
    assertEquals(content, content1);

    String content16 = FileUtil.readString(new File(utfdataRoot, "utf-16be.txt"), StandardCharsets.UTF_16BE);
    content16 = content16.replace("\r\n", "\n");
    assertEquals(content, content16);

void testIsAncestor() { final File folder = new File("/foo/bar"); File file = new File(folder, "foo.txt");

    assertTrue(FileUtil.isAncestor(folder, folder, false));
    assertFalse(FileUtil.isAncestor(folder, folder, true));

    assertTrue(FileUtil.isAncestor(folder, file, false));
    assertTrue(FileUtil.isAncestor(folder, file, true));

    file = new File(folder, "../foo.txt");

    assertFalse(FileUtil.isAncestor(folder, file, false));
    assertFalse(FileUtil.isAncestor(folder, file, true));

    file = new File(folder, "bar/../../../foo.txt");

    assertFalse(FileUtil.isAncestor(folder, file, false));
    assertFalse(FileUtil.isAncestor(folder, file, true));

    file = new File(folder, "bar/car/../foo.txt");

    assertTrue(FileUtil.isAncestor(folder, file, false));
    assertTrue(FileUtil.isAncestor(folder, file, true));
}

void testDeleteFileTree_not_successful() throws Exception { assumeTrue(baseDir_Not_Successful.exists()); assumeTrue(locked_file.exists());

        // When you use FileLock, it is purely advisory—acquiring a lock on a file may not stop you from doing anything:
        // reading, writing, and deleting a file may all be possible even when another process has acquired a lock.
        // Sometimes, a lock might do more than this on a particular platform, but this behavior is unspecified,
        // and relying on more than is guaranteed in the class documentation is a recipe for failure.

@Ignore void testJavaEscapes() { final String from = "\r\t\b\f\n\\"asd\u0111q\u0173aa\u0ABC\u0abc"; final String to = "\r\t\b\f\n\\\\"asd\u0111q\u0173aa\u0abc\u0abc";

    assertEquals(to, StringUtil.escapeJava(from));
    assertEquals(from, StringUtil.unescapeJava(to));

    try {
        StringUtil.unescapeJava("\\r\\t\\b\\f\\q");
        fail("error");
    } catch (final IllegalArgumentException e) {

void testFromCamelCase() { assertEquals("one two three", StringUtil.fromCamelCase("oneTwoThree", ' ')); assertEquals("one-two-three", StringUtil.fromCamelCase("oneTwoThree", '-')); assertEquals("one. two. three", StringUtil.fromCamelCase("one.Two.Three", ' '));

    assertEquals("user_name", StringUtil.fromCamelCase("userName", '_'));
    assertEquals("user_name", StringUtil.fromCamelCase("UserName", '_'));
    assertEquals("user_name", StringUtil.fromCamelCase("USER_NAME", '_'));
    assertEquals("user_name", StringUtil.fromCamelCase("user_name", '_'));
    assertEquals("user", StringUtil.fromCamelCase("user", '_'));
    assertEquals("user", StringUtil.fromCamelCase("User", '_'));
    assertEquals("user", StringUtil.fromCamelCase("USER", '_'));
    assertEquals("user", StringUtil.fromCamelCase("_user", '_'));
    assertEquals("user", StringUtil.fromCamelCase("_User", '_'));
    assertEquals("_user", StringUtil.fromCamelCase("__user", '_'));
    assertEquals("user__name", StringUtil.fromCamelCase("user__name", '_'));
}

public static Iterable iterableo(final T... v) { return new Iterable() { public Iterator iterator() { final AtomicInteger index = new AtomicInteger(0); return new Iterator() { public boolean hasNext() { return index.intValue() < v.length; }

                public T next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    else
                    }
                    T value = v[index.intValue()];
                    index.incrementAndGet();
                    return value;
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

}

void testStringToIntMatrix() { String[][] strings = new String[][] { {"123", "865"}, {"432", "345", "9832"} };

    int[][] arr = TypeConverterManager.get().convertType(strings, int[][].class);

    assertEquals(2, arr.length);

    assertArrayEquals(ints(123,865), arr[0]);
    assertArrayEquals(ints(432,345,9832), arr[1]);
}

void testBasic() { ClassDescriptor cd = ClassIntrospector.get().lookup(Abean.class); assertNotNull(cd); PropertyDescriptor[] properties = cd.getAllPropertyDescriptors(); int c = 0; for (PropertyDescriptor property : properties) { if (property.isFieldOnly()) continue; if (property.isPublic()) c++; } assertEquals(2, c);

    Arrays.sort(properties, Comparator.comparing(PropertyDescriptor::getName));

    PropertyDescriptor pd = properties[0];
    assertEquals("fooProp", pd.getName());
    assertNotNull(pd.getReadMethodDescriptor());
    assertNotNull(pd.getWriteMethodDescriptor());
    assertNotNull(pd.getFieldDescriptor());

void testStaticFieldsForProperties() { ClassDescriptor cd = ClassIntrospector.get().lookup(Mojo.class);

    FieldDescriptor[] fieldDescriptors = cd.getAllFieldDescriptors();
    assertEquals(3, fieldDescriptors.length);

    MethodDescriptor[] methodDescriptors = cd.getAllMethodDescriptors();
    assertEquals(2, methodDescriptors.length);

    PropertyDescriptor[] propertyDescriptor = cd.getAllPropertyDescriptors();
    assertEquals(3, propertyDescriptor.length);

    int count = 0;
    for (PropertyDescriptor pd : propertyDescriptor) {
        if (pd.isFieldOnly()) {
            continue;
        }
        count++;
    }
    assertEquals(1, count);
}

Part 1: package jodd.bean;

import org.junit.jupiter.api.Test;

import java.util.HashMap; import java.util.Map; import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SupplierTest {

private static class Foo {
    public int bbb = 13;
    public Supplier<Foo> aaa = () -> this;
}

@Test
void testSupplier_inMap() {
    Map map1 = new HashMap();
    Supplier<Map> mapSupplier = () -> map1;

    map1.put("qwe", "123");
    map1.put("asd", mapSupplier);

    assertEquals("123", BeanUtil.pojo.getProperty(map1, "qwe"));
    assertEquals("123", BeanUtil.pojo.getProperty(map1, "asd.qwe"));
}

void testSupplier_last() { Map map1 = new HashMap(); Supplier mapSupplier = () -> map1;

    map1.put("qwe", "123");
    map1.put("asd", mapSupplier);

    assertEquals("123", BeanUtil.pojo.getProperty(map1, "qwe"));
    assertEquals(mapSupplier, BeanUtil.pojo.getProperty(map1, "asd"));
}

package jodd.bean;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class JavaInternalsTest {

static class Foo {
    public String s;
}

@Test
void testString() {
    final Foo foo = new Foo();

    assertThrows(BeanException.class, () ->
        BeanUtil.declaredForced.setProperty(foo, "s.value", null)
    );
}

}

igr commented 3 years ago

Hey, @mkiefer10 I am not quite sure what is done here.

Could you please explain it?

mkiefer10 commented 3 years ago

Hi Igor,

I am using the Jodd github repository for homework assignments in a software testing assignment I have.

I am testing potential test smells, statement coverage up to 25 to 100%, and mock testing examples. I then need to submit them using a pull request.

For the next assignment, please let me know any test doubles you know, refactoring suggestions, and fuzz testing examples.

Best regards, Max Kiefer

On Thu, Apr 8, 2021 at 10:28 AM Igor Spasić @.***> wrote:

Hey, @mkiefer10 https://github.com/mkiefer10 I am not quite sure what is done here.

Could you please explain it?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/oblac/jodd-util/pull/8#issuecomment-815564900, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMO6JPYYGEW3PYGS5EQH6LDTHVSMTANCNFSM42BOITMQ .

mkiefer10 commented 3 years ago

Hi Igor, I am using the Jodd github repository for homework assignments in a software testing assignment I have. I am testing potential test smells, statement coverage up to 25 to 100%, and mock testing examples. I then need to submit them using a pull request. For the next assignment, please let me know any test doubles you know, refactoring suggestions, and fuzz testing examples. Best regards, Max Kiefer On Thu, Apr 8, 2021 at 10:28 AM Igor Spasić @.***> wrote: Hey, @mkiefer10 https://github.com/mkiefer10 I am not quite sure what is done here. Could you please explain it? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#8 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMO6JPYYGEW3PYGS5EQH6LDTHVSMTANCNFSM42BOITMQ .

Hi. Do you have time to talk today or this weekend to discuss my questions on the project? Thank you.

igr commented 3 years ago

Thank you @mkiefer10! The thing is that I am not seeing your code in the PR, and it looks like your PR is based on a branch string-builder-pool which is under investigation and not used there.

I would suggest to close the PR and make another one that is based on the master

mkiefer10 commented 3 years ago

Hi Igor,

Yes I will add do.

Also, do you know of any test doubles, refactoring suggestions, and fuzz testing examples in your repository?

Best, Max Kiefer

On Fri, Apr 9, 2021 at 11:23 AM Igor Spasić @.***> wrote:

Thank you @mkiefer10 https://github.com/mkiefer10! The thing is that I am not seeing your code in the PR, and it looks like your PR is based on a branch string-builder-pool which is under investigation and not used there.

I would suggest to close the PR and make another one that is based on the master

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/oblac/jodd-util/pull/8#issuecomment-816549756, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMO6JP3QIV4B2FR3IJ56X3LTH3BSHANCNFSM42BOITMQ .

igr commented 3 years ago

I guess there are many :)) Jodd is quite big and old :)