Ranides Atterwim 10 years ago
parent
commit
d9e8cb043b

+ 93 - 94
assira/src/main/java/net/ranides/assira/trace/NSTException.java

@@ -1,94 +1,93 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.trace;
-
-import java.lang.management.ManagementFactory;
-import java.text.MessageFormat;
-import java.util.concurrent.atomic.AtomicInteger;
-import net.ranides.assira.text.StringUtils;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class NSTException extends RuntimeException {
-
-    private static final String EMPTY_TEXT = "";
-    
-    private static final Object[] EMPTY_ARGS = new Object[0];
-    
-    private static final AtomicInteger COUNTER = new AtomicInteger(0);
-    
-    private static final class Li { // NOPMD - lazy init idiom
-        
-        public static final String VMNAME = ManagementFactory.getRuntimeMXBean().getName();
-        
-    }
-    
-    private final String uid;
-    private final String pattern;
-    private final Object[] arguments;
-    
-    public NSTException() {
-        this(EMPTY_TEXT, EMPTY_ARGS);
-    }
-
-    public NSTException(String message) {
-        this(message, EMPTY_ARGS);
-    }
-    
-    public NSTException(String message, Object... arguments) {
-        this.uid = initUID();
-        this.pattern = message;
-        this.arguments = arguments;
-    }
-    
-    public NSTException(Throwable cause) {
-        this(cause, EMPTY_TEXT, EMPTY_ARGS);
-    }
-
-    public NSTException(Throwable cause, String message) {
-        this(cause, message, EMPTY_ARGS);
-    }
-    
-    public NSTException(Throwable cause, String message, Object... arguments) {
-        super(cause);
-        this.uid = initUID();
-        this.pattern = message;
-        this.arguments = arguments;
-    }
-    
-    public String uid() {
-        return uid;
-    }
-    
-    @Override
-    public Throwable fillInStackTrace() {
-        return this;
-    }
-    
-    @Override
-    public String getMessage() {
-        return MessageFormat.format(pattern, arguments); // @todo (assira #0) use ResolvePattern
-    }
-
-    @Override
-    public String getLocalizedMessage() {
-        return getMessage();
-    }
-
-    @Override
-    public String toString() {
-        String s = getClass().getName();
-        String m = getLocalizedMessage();
-        return s + "(" + uid + "): " + (m != null ? m : "");
-    }
-    
-    private static String initUID() {
-        return Li.VMNAME + "@" + COUNTER.incrementAndGet();
-    }
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.trace;
+
+import java.lang.management.ManagementFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+import net.ranides.assira.text.ResolveUtils;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class NSTException extends RuntimeException {
+
+    private static final String EMPTY_TEXT = "";
+    
+    private static final Object[] EMPTY_ARGS = new Object[0];
+    
+    private static final AtomicInteger COUNTER = new AtomicInteger(0);
+    
+    private static final class Li { // NOPMD - lazy init idiom
+        
+        public static final String VMNAME = ManagementFactory.getRuntimeMXBean().getName();
+        
+    }
+    
+    private final String uid;
+    private final String pattern;
+    private final Object[] arguments;
+    
+    public NSTException() {
+        this(EMPTY_TEXT, EMPTY_ARGS);
+    }
+
+    public NSTException(String message) {
+        this(message, EMPTY_ARGS);
+    }
+    
+    public NSTException(String message, Object... arguments) {
+        this.uid = initUID();
+        this.pattern = message;
+        this.arguments = arguments;
+    }
+    
+    public NSTException(Throwable cause) {
+        this(cause, EMPTY_TEXT, EMPTY_ARGS);
+    }
+
+    public NSTException(Throwable cause, String message) {
+        this(cause, message, EMPTY_ARGS);
+    }
+    
+    public NSTException(Throwable cause, String message, Object... arguments) {
+        super(cause);
+        this.uid = initUID();
+        this.pattern = message;
+        this.arguments = arguments;
+    }
+    
+    public String uid() {
+        return uid;
+    }
+    
+    @Override
+    public Throwable fillInStackTrace() {
+        return this;
+    }
+    
+    @Override
+    public String getMessage() {
+        return ResolveUtils.vformat(pattern, arguments);
+    }
+
+    @Override
+    public String getLocalizedMessage() {
+        return getMessage();
+    }
+
+    @Override
+    public String toString() {
+        String s = getClass().getName();
+        String m = getLocalizedMessage();
+        return s + "(" + uid + "): " + (m != null ? m : "");
+    }
+    
+    private static String initUID() {
+        return Li.VMNAME + "@" + COUNTER.incrementAndGet();
+    }
+}

+ 39 - 41
assira/src/test/java/net/ranides/assira/reflection/impl/CDParametersTest.java

@@ -1,41 +1,39 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.reflection.impl;
-
-import java.io.IOException;
-import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.Map.Entry;
-import net.ranides.assira.reflection.IClass;
-import net.ranides.assira.reflection.mockup.ForBeanModel;
-import org.junit.Test;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class CDParametersTest {
-    
-    public CDParametersTest() {
-    }
-
-    @Test
-    public void testSomeMethod() throws IOException {
-        IClass ic = IClass.typeinfo(ForBeanModel.class);
-        
-        ic.methods().each(m -> {
-            if(m.arguments().count() > 0) {
-                String[] names = CDParameters.getParams(m.reflective());
-//                if(null == names) {
-                    System.out.printf(" - %s %s%n", m, Arrays.toString(names));
-//                }
-            }
-        });
-        
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.reflection.impl;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Map.Entry;
+import net.ranides.assira.reflection.IClass;
+import net.ranides.assira.reflection.mockup.ForBeanModel;
+import org.junit.Test;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class CDParametersTest {
+    
+    public CDParametersTest() {
+    }
+
+    @Test
+    public void testSomeMethod() throws IOException {
+        IClass ic = IClass.typeinfo(ForBeanModel.class);
+        
+        ic.methods().each(m -> {
+            String[] names = CDParameters.getParams(m.reflective());
+            if(null == names) {
+                System.out.printf(" - %s %s%n", m, Arrays.toString(names));
+            }
+        });
+        
+    }
+    
+}

+ 151 - 143
assira/src/test/java/net/ranides/assira/reflection/impl/RConstructorTest.java

@@ -1,143 +1,151 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.reflection.impl;
-
-import java.lang.invoke.MethodHandle;
-import java.lang.reflect.Constructor;
-import java.text.MessageFormat;
-import java.util.Arrays;
-import java.util.List;
-import net.ranides.assira.annotations.Meta;
-import net.ranides.assira.reflection.IClass;
-import net.ranides.assira.reflection.mockup.ForConstructor;
-import org.junit.Test;
-import static net.ranides.assira.junit.NewAssert.*;
-import net.ranides.assira.reflection.IArguments;
-import net.ranides.assira.reflection.IClasses;
-import net.ranides.assira.reflection.IMethod;
-import net.ranides.assira.reflection.IMethods;
-import net.ranides.assira.reflection.mockup.ForConstructor.Record;
-import net.ranides.assira.reflection.util.AnnotationUtils;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class RConstructorTest {
-    
-    IClass ic = IClass.typeinfo(ForConstructor.Record.class);
-            
-    @Test
-    public void testParams() {
-        String cn = "net.ranides.assira.reflection.mockup.ForConstructor$Record";
-        List<String> exp = Arrays.asList(
-            MessageFormat.format("{0} {0}.{0}(R param1, R param2)", cn),
-            MessageFormat.format("{0} {0}.{0}(int x, int y, int z)", cn),
-            MessageFormat.format("{0} {0}.{0}(java.lang.String param)", cn), 
-            MessageFormat.format("{0} {0}.{0}()", cn)
-        );
-        assertEquivalent(exp, ic.constructors().list(m -> m.toString()));
-        
-        assertEquivalent(Arrays.asList("[R]", "[]", "[]", "[]"), ic.constructors().list(m -> m.params().toString()));
-    }
-    
-    @Test
-    public void testTypeInfo() throws Exception {
-        IMethod c1 = ic.constructors().require(c -> c.arguments().count()==3).first();
-        assertThrows(UnsupportedOperationException.class, ()->{
-            c1.reflective();
-        });
-        
-        Constructor<ForConstructor.Record> rc1 = ForConstructor.Record.class.getConstructor(int.class, int.class, int.class);
-        IMethod c2 = IMethod.typeinfo(rc1);
-        assertEquals(c1, c2);
-    }
-    
-    @Test
-    public void testApply() {
-        ForConstructor.Record that = new ForConstructor.Record();
-        
-        IMethod c1 = ic.constructors().require(0).first();
-        IMethod c2 = ic.constructors().require(2).first();
-        IMethod c3 = ic.constructors().require(3).first();
-
-        assertEquals("Record{0:0:0}", c1.apply().toString());
-        assertEquals("Record{0:0:0}", c1.apply(null).toString());
-        assertThrows(IllegalArgumentException.class, ()->{
-            c1.apply(that);
-        });
-        
-        assertEquals("Record{5:6:11}", c2.apply(null, 5.1, 6.4).toString());
-        assertThrows(IllegalArgumentException.class, ()->{
-            c2.apply(that, 5.1, 6.4);
-        });
-        
-        assertEquals("Record{1:2:3}", c3.apply(null, 1, 2, 3).toString());
-    }
-    
-    @Test
-    public void testAnnotations() {
-        IMethod c1 = ic.constructors().require(Meta.Unsafe.class).first();
-        List<String> types = Arrays.asList(
-            "@net.ranides.assira.annotations.Meta$Unsafe()", 
-            "@java.lang.Deprecated()"
-        ); 
-        List<String> names = Arrays.asList(
-            "net.ranides.assira.annotations.Meta$Unsafe", 
-            "java.lang.Deprecated"
-        );
-        assertEquivalent(types, c1.annotations().list(a -> a.toString()));
-        assertEquivalent(names, c1.annotations().names());
-        
-        Deprecated dc = AnnotationUtils.make(Deprecated.class);
-        String exp = "[net.ranides.assira.annotations.Meta$Unsafe]";
-        assertEquals(exp, c1.annotations().discard(Deprecated.class).names().toString());
-        assertEquals(exp, c1.annotations().discard(a -> a.equals(dc)).names().toString());
-    }
-    
-    
-    @Test
-    public void testCall() {
-        IMethod c1 = ic.constructors().require(0).first();
-        IMethod c2 = ic.constructors().require(2).first();
-        IMethod c3 = ic.constructors().require(3).first();
-
-        assertEquals("Record{0:0:0}", c1.call().toString());
-        
-        assertEquals("Record{5:6:11}", c2.call(5.1, 6.4).toString());
-        assertEquals("Record{1:2:3}", c3.call(1, 2, 3).toString());
-    }
-    
-    @Test
-    public void testHandle() throws Throwable {
-        MethodHandle c1 = ic.constructors().require(0).first().handle();
-        MethodHandle c2 = ic.constructors().require(2).first().handle();
-        MethodHandle c3 = ic.constructors().require(3).first().handle();
-
-        assertEquals("Record{0:0:0}", c1.invoke().toString());
-        
-        assertEquals("Record{5:6:11}", c2.invoke(5.1, 6.4).toString());
-        assertEquals("Record{1:2:3}", c3.invoke(1, 2, 3).toString());
-    }
-    
-    @Test
-    public void testMatchGeneric() {
-        // @todo (assira #6) reflective: generics: match
-        // mamy konstruktor public <R extends Number> Record(R param1, R param2) {
-        // nie wiem czy istnieje jakakolwiek szansa, żeby w oparciu o type-bounds match zaimplementować
-        // chyba takie "super-elastic" metody będą po prostu bezużyteczne w przypadku reflective IClass
-        // normalnie to dużo prostsze przypadki nie przechodzą, więc płaczu nie ma
-        IMethods list1 = ic.constructors().require(IArguments.typeinfo(Number.class, Number.class));
-        IMethods list2 = ic.constructors().matches(IClasses.typeinfo(Number.class, Number.class));
-        IMethods list3 = ic.constructors().accepts(0,0);
-        
-        System.out.printf("RConstructorTest: ISSUE: %s%n", list1.list());
-        System.out.printf("RConstructorTest: ISSUE: %s%n", list2.list());
-        System.out.printf("RConstructorTest: ISSUE: %s%n", list3.list());
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.reflection.impl;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.reflect.Constructor;
+import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.List;
+import net.ranides.assira.annotations.Meta;
+import net.ranides.assira.reflection.IClass;
+import net.ranides.assira.reflection.mockup.ForConstructor;
+import org.junit.Test;
+import static net.ranides.assira.junit.NewAssert.*;
+import net.ranides.assira.reflection.IArguments;
+import net.ranides.assira.reflection.IClasses;
+import net.ranides.assira.reflection.IMethod;
+import net.ranides.assira.reflection.IMethods;
+import net.ranides.assira.reflection.mockup.ForConstructor.Record;
+import net.ranides.assira.reflection.util.AnnotationUtils;
+import org.junit.Ignore;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class RConstructorTest {
+    
+    IClass ic = IClass.typeinfo(ForConstructor.Record.class);
+            
+    @Test
+    public void testParams() {
+        String cn = "net.ranides.assira.reflection.mockup.ForConstructor$Record";
+        List<String> exp = Arrays.asList(
+            MessageFormat.format("{0} {0}.{0}(R param1, R param2)", cn),
+            MessageFormat.format("{0} {0}.{0}(int x, int y, int z)", cn),
+            MessageFormat.format("{0} {0}.{0}(java.lang.String param)", cn), 
+            MessageFormat.format("{0} {0}.{0}()", cn)
+        );
+        assertEquivalent(exp, ic.constructors().list(m -> m.toString()));
+        
+        assertEquivalent(Arrays.asList("[R]", "[]", "[]", "[]"), ic.constructors().list(m -> m.params().toString()));
+    }
+    
+    @Test
+    public void testTypeInfo() throws Exception {
+        IMethod c1 = ic.constructors().require(c -> c.arguments().count()==3).first();
+        assertThrows(UnsupportedOperationException.class, ()->{
+            c1.reflective();
+        });
+        
+        Constructor<ForConstructor.Record> rc1 = ForConstructor.Record.class.getConstructor(int.class, int.class, int.class);
+        IMethod c2 = IMethod.typeinfo(rc1);
+        assertEquals(c1, c2);
+    }
+    
+    @Test
+    public void testApply() {
+        ForConstructor.Record that = new ForConstructor.Record();
+        
+        IMethod c1 = ic.constructors().require(0).first();
+        IMethod c2 = ic.constructors().require(2).first();
+        IMethod c3 = ic.constructors().require(3).first();
+
+        assertEquals("Record{0:0:0}", c1.apply().toString());
+        assertEquals("Record{0:0:0}", c1.apply(null).toString());
+        assertThrows(IllegalArgumentException.class, ()->{
+            c1.apply(that);
+        });
+        
+        assertEquals("Record{5:6:11}", c2.apply(null, 5.1, 6.4).toString());
+        assertThrows(IllegalArgumentException.class, ()->{
+            c2.apply(that, 5.1, 6.4);
+        });
+        
+        assertEquals("Record{1:2:3}", c3.apply(null, 1, 2, 3).toString());
+    }
+    
+    @Test
+    public void testAnnotations() {
+        IMethod c1 = ic.constructors().require(Meta.Unsafe.class).first();
+        List<String> types = Arrays.asList(
+            "@net.ranides.assira.annotations.Meta$Unsafe()", 
+            "@java.lang.Deprecated()"
+        ); 
+        List<String> names = Arrays.asList(
+            "net.ranides.assira.annotations.Meta$Unsafe", 
+            "java.lang.Deprecated"
+        );
+        assertEquivalent(types, c1.annotations().list(a -> a.toString()));
+        assertEquivalent(names, c1.annotations().names());
+        
+        Deprecated dc = AnnotationUtils.make(Deprecated.class);
+        String exp = "[net.ranides.assira.annotations.Meta$Unsafe]";
+        assertEquals(exp, c1.annotations().discard(Deprecated.class).names().toString());
+        assertEquals(exp, c1.annotations().discard(a -> a.equals(dc)).names().toString());
+    }
+    
+    
+    @Test
+    public void testCall() {
+        IMethod c1 = ic.constructors().require(0).first();
+        IMethod c2 = ic.constructors().require(2).first();
+        IMethod c3 = ic.constructors().require(3).first();
+
+        assertEquals("Record{0:0:0}", c1.call().toString());
+        
+        assertEquals("Record{5:6:11}", c2.call(5.1, 6.4).toString());
+        assertEquals("Record{1:2:3}", c3.call(1, 2, 3).toString());
+    }
+    
+    @Test
+    public void testHandle() throws Throwable {
+        MethodHandle c1 = ic.constructors().require(0).first().handle();
+        MethodHandle c2 = ic.constructors().require(2).first().handle();
+        MethodHandle c3 = ic.constructors().require(3).first().handle();
+
+        assertEquals("Record{0:0:0}", c1.invoke().toString());
+        
+        assertEquals("Record{5:6:11}", c2.invoke(5.1, 6.4).toString());
+        assertEquals("Record{1:2:3}", c3.invoke(1, 2, 3).toString());
+    }
+    
+    @Ignore
+    @Test
+    public void testMatchGeneric() {
+        // @todo (assira #6) reflective: generics: match
+        // mamy konstruktor public <R extends Number> Record(R param1, R param2) {
+        // nie wiem czy istnieje jakakolwiek szansa, żeby w oparciu o type-bounds match zaimplementować
+        // chyba takie "super-elastic" metody będą po prostu bezużyteczne w przypadku reflective IClass
+        // normalnie to dużo prostsze przypadki nie przechodzą, więc płaczu nie ma
+        IMethods list1 = ic.constructors().require(IArguments.typeinfo(Number.class, Number.class));
+        IMethods list2 = ic.constructors().matches(IClasses.typeinfo(Number.class, Number.class));
+        IMethods list3 = ic.constructors().accepts(0,0);
+        
+        List<IMethod> exp1 = ic.constructors().require(Deprecated.class).list();
+
+//        System.out.printf("RConstructorTest: ISSUE: %s%n", list1.list());
+//        System.out.printf("RConstructorTest: ISSUE: %s%n", list2.list());
+//        System.out.printf("RConstructorTest: ISSUE: %s%n", list3.list());
+        
+        assertEquals(exp1, list1.list());
+        assertEquals(exp1, list2.list());
+        assertEquals(exp1, list3.list());
+    }
+    
+}

+ 0 - 1
assira/src/test/java/net/ranides/assira/reflection/impl/RFClassTest.java

@@ -42,7 +42,6 @@ public class RFClassTest {
         assertEquals(Collections.emptyList(), f1.annotations().list());
         
         
-        System.out.printf("---%n");
         IClass f2 = IClass.typeinfo(Owner.class).field("b").type();
         assertEquals(Arrays.asList("x"), f2.fields().names());
         List<String> constructors1 = Arrays.asList(

+ 21 - 5
assira/src/test/java/net/ranides/assira/reflection/impl/RMethodTest.java

@@ -30,13 +30,29 @@ public class RMethodTest {
     private static final String RECORD_TYPE = "net.ranides.assira.reflection.mockup.ForRMethod<java.lang.Float>.Record<java.lang.Double>";
     
     @Test(expected = java.lang.reflect.GenericSignatureFormatError.class)
-    public void testJVM1() {
+    public void testJVM1() { 
         // @todo (assira #9) JVM BUG :)
-        ForRMethod<? super Float> parent = new ForRMethod<Float>(){};
-        Object record = parent.new Record<Double>(){};
-        Type type = record.getClass().getGenericSuperclass();
         
-        assertEquals(RECORD_TYPE, type.toString());
+        // to zadziała
+        ForRMethod<Float> parent1 = new ForRMethod<Float>(){};
+        Object record1 = parent1.new Record<Double>(){};
+        Type type1 = record1.getClass().getGenericSuperclass();
+        
+        assertEquals(RECORD_TYPE, type1.toString());
+        
+        
+        // Wyjątek jaki leci jest zaskakujący:
+        // java.lang.reflect.GenericSignatureFormatError: Signature Parse error: 
+        //      ; expected in signature of type variable named
+        //      Remaining input: <captured wildcard>;>.Record<Ljava/lang/Double;>;
+        
+        // Nie nasza wina. Kompilator coś sobie źle generuje i JVM samo tego sparsować sobie nie potrafi.
+        
+        ForRMethod<? super Float> parent2 = new ForRMethod<Float>(){};
+        Object record2 = parent2.new Record<Double>(){};
+        Type type2 = record2.getClass().getGenericSuperclass();
+        
+        assertEquals(RECORD_TYPE, type2.toString());
     }
     
     @Test

+ 33 - 34
assira/src/test/java/net/ranides/assira/system/RuntimeUtilsTest.java

@@ -1,34 +1,33 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.system;
-
-import net.ranides.assira.trace.TraceUtils;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class RuntimeUtilsTest {
-    
-    public RuntimeUtilsTest() {
-    }
-
-    @Test
-    public void testInvokedInsideMaven() {
-        assertTrue( RuntimeUtils.invokedInsideMaven() );
-    }
-    
-    @Test
-    public void testInvokedInsideSingleTest() {
-        // we have to check that manually
-        System.err.printf("RuntimeUtils.invokedInsideSingleTest = %s%n", RuntimeUtils.invokedInsideSingleTest());
-        assertTrue(true);
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.system;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class RuntimeUtilsTest {
+    
+    public RuntimeUtilsTest() {
+    }
+
+    @Test
+    public void testInvokedInsideMaven() {
+        assertTrue( RuntimeUtils.invokedInsideMaven() );
+    }
+    
+    @Test
+    public void testInvokedInsideSingleTest() {
+        // we have to check that manually
+        System.err.printf("RuntimeUtils.invokedInsideSingleTest = %s%n", RuntimeUtils.invokedInsideSingleTest());
+        assertTrue(true);
+    }
+    
+}

+ 102 - 89
assira/src/test/java/net/ranides/assira/text/URIUtilsTest.java

@@ -1,89 +1,102 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.text;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.nio.charset.Charset;
-import java.nio.charset.CharsetDecoder;
-import java.nio.charset.CharsetEncoder;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-import static net.ranides.assira.junit.NewAssert.assertThrows;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class URIUtilsTest {
-    
-    @Test
-    public void testEncodeURL() {
-        assertEquals("hello%3Aworld%3Fparam%3D5", URIUtils.encodeURL("hello:world?param=5"));
-        
-        assertEquals("hello%3Aworld%3Fparam%3D5", URIUtils.encodeURL("hello:world?param=5", new FCharset("UTF-8")));
-        
-        assertThrows(AssertionError.class, ()->{
-            URIUtils.encodeURL("hello:world?param=5", new FCharset("UFC888"));
-        });
-        
-    }
-    
-    @Test
-    public void testDecodeURL() {
-        assertEquals("hello:world?param=5", URIUtils.decodeURL("hello%3Aworld%3Fparam%3D5"));
-        
-        assertEquals("hello:world?param=5", URIUtils.decodeURL("hello%3Aworld%3Fparam%3D5", new FCharset("UTF-8")));
-        
-        assertThrows(AssertionError.class, ()->{
-            URIUtils.decodeURL("hello%3Aworld%3Fparam%3D5", new FCharset("UFC888"));
-        });
-    }
-    
-    @Test
-    public void testGetQueryParams() throws URISyntaxException {
-        URI u1 = new URI("file://root?charset=UTF-8&endl=unix&follow&fs=ntfs");
-        URI u2 = new URI("file://root?charset=UTF-8");
-        URI u3 = new URI("file://root?charset");
-        URI u4 = new URI("file://root?");
-        URI u5 = new URI("file://root");
-        
-        
-        System.out.printf("%s%n", URIUtils.getParams(u1));
-        System.out.printf("%s%n", URIUtils.getParams(u2));
-        System.out.printf("%s%n", URIUtils.getParams(u3));
-        System.out.printf("%s%n", URIUtils.getParams(u4));
-        System.out.printf("%s%n", URIUtils.getParams(u5));
-    }
-    
-    private static final class FCharset extends Charset {
-
-        public FCharset(String name) {
-            super(name, new String[0]);
-        }
-
-        @Override
-        public boolean contains(Charset cs) {
-            throw new UnsupportedOperationException();
-        }
-
-        @Override
-        public CharsetDecoder newDecoder() {
-            throw new UnsupportedOperationException();
-        }
-
-        @Override
-        public CharsetEncoder newEncoder() {
-            throw new UnsupportedOperationException();
-        }
-        
-    }
-	
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.text;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CharsetEncoder;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import net.ranides.assira.collection.maps.HashMultiMap;
+import org.junit.Test;
+import static net.ranides.assira.junit.NewAssert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class URIUtilsTest {
+    
+    @Test
+    public void testEncodeURL() {
+        assertEquals("hello%3Aworld%3Fparam%3D5", URIUtils.encodeURL("hello:world?param=5"));
+        
+        assertEquals("hello%3Aworld%3Fparam%3D5", URIUtils.encodeURL("hello:world?param=5", new FCharset("UTF-8")));
+        
+        assertThrows(AssertionError.class, ()->{
+            URIUtils.encodeURL("hello:world?param=5", new FCharset("UFC888"));
+        });
+        
+    }
+    
+    @Test
+    public void testDecodeURL() {
+        assertEquals("hello:world?param=5", URIUtils.decodeURL("hello%3Aworld%3Fparam%3D5"));
+        
+        assertEquals("hello:world?param=5", URIUtils.decodeURL("hello%3Aworld%3Fparam%3D5", new FCharset("UTF-8")));
+        
+        assertThrows(AssertionError.class, ()->{
+            URIUtils.decodeURL("hello%3Aworld%3Fparam%3D5", new FCharset("UFC888"));
+        });
+    }
+    
+    @Test
+    public void testGetQueryParams() throws URISyntaxException {
+        URI u1 = new URI("file://root?charset=UTF-8&endl=unix&follow&fs=ntfs&a=1&a=2");
+        URI u2 = new URI("file://root?charset=UTF-8");
+        URI u3 = new URI("file://root?charset");
+        URI u4 = new URI("file://root?");
+        URI u5 = new URI("file://root");
+        
+        Map<String,String> exp1 = new HashMultiMap<>();
+        exp1.put("endl", "unix");
+        exp1.put("charset", "UTF-8");
+        exp1.put("follow", "");
+        exp1.put("fs", "ntfs");
+        exp1.put("a", "2");
+        exp1.put("a", "1");
+        assertEquivalent(exp1, URIUtils.getParams(u1));
+        
+        Map<String,String> exp2 = new HashMap<>();
+        exp2.put("charset", "UTF-8");
+        assertEquivalent(exp2, URIUtils.getParams(u2));
+        
+        Map<String,String> exp3 = new HashMap<>();
+        exp3.put("charset", "");
+        assertEquivalent(exp3, URIUtils.getParams(u3));
+        
+        assertEquivalent(Collections.emptyMap(), URIUtils.getParams(u4));
+        assertEquivalent(Collections.emptyMap(), URIUtils.getParams(u5));
+    }
+    
+    private static final class FCharset extends Charset {
+
+        public FCharset(String name) {
+            super(name, new String[0]);
+        }
+
+        @Override
+        public boolean contains(Charset cs) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public CharsetDecoder newDecoder() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public CharsetEncoder newEncoder() {
+            throw new UnsupportedOperationException();
+        }
+        
+    }
+	
+}

+ 30 - 0
assira/src/test/java/net/ranides/assira/trace/NSTExceptionTest.java

@@ -0,0 +1,30 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.trace;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class NSTExceptionTest {
+    
+    public NSTExceptionTest() {
+    }
+
+    @Test
+    public void testFormat() {
+        try {
+            throw new NSTException("{1} / {0}", 11, 23);
+        } catch(Exception cause) {
+            assertEquals("23 / 11", cause.getMessage());
+        }
+    }
+    
+}