package org.jonnyzzz;

import com.sun.istack.internal.NotNull;
import org.jonnyzzz.stack.StackLine;
import org.junit.Assert;
import org.junit.Test;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Unit test for simple App.
 */
public class AppTest {

    @Test
    public void test_action() throws Throwable {

        final AtomicReference<String> result = new AtomicReference<String>();
        StackLine.stackLine("someMagicTestName", Throwable.class, new StackLine.UnderStackAction<Throwable>() {
            public void execute() throws Throwable {
                result.set(stackTrace());

            }
        });

        final String s = result.get();
        Assert.assertTrue(s, s.contains("someMagicTestName"));
    }

    @Test
    public void test_function() throws Throwable {
        final AtomicReference<String> result = new AtomicReference<String>();
        StackLine.stackLine("someMagicTestName", Throwable.class, new StackLine.UnderStackFunction<Object, Throwable>() {
            public Object execute() throws Throwable {
                result.set(stackTrace());
                return 42;
            }
        });

        final String s = result.get();
        Assert.assertTrue(s, s.contains("someMagicTestName"));
    }

    @Test
    public void test_long_names_function() throws Throwable {
        final AtomicReference<String> result = new AtomicReference<String>();
        StackLine.stackLine("вот это вот тут так получилось", Throwable.class, new StackLine.UnderStackFunction<Object, Throwable>() {
            public Object execute() throws Throwable {
                result.set(stackTrace());
                return 42;
            }
        });

        final String s = result.get();
        Assert.assertTrue(s, s.contains("вот это вот тут так получилось"));
    }


    @NotNull
    private static String stackTrace() {
        try {
            throw new Exception();
        } catch (Exception e) {
            final StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            System.out.printf(sw.toString());
            return sw.toString();
        }
    }
}

@@ -191,32 +191,82 @@
 o.StringWriter;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A
 import java.util
@@ -1924,16 +1924,590 @@
 %0A    %7D%0A%0A
+    @Test(timeout = 500)%0A    public void test_should_not_generate_too_may_classes() throws Throwable %7B%0A        ///this test should not slowdown%0A        final AtomicInteger result = new AtomicInteger();%0A        for(int i = 0 ; i %3C 10 * 1000; i++) %7B%0A            StackLine.stackLine(%22%D0%B2%D0%BE%D1%82 %D1%8D%D1%82%D0%BE %D0%B2%D0%BE%D1%82 %D1%82%D1%83%D1%82 %D1%82%D0%B0%D0%BA %D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D0%BB%D0%BE%D1%81%D1%8C%22, Throwable.class, new StackLine.UnderStackFunction%3CObject, Throwable%3E() %7B%0A                public Object execute() throws Throwable %7B%0A                    result.incrementAndGet();%0A                    return 42;%0A                %7D%0A            %7D);%0A        %7D%0A    %7D%0A%0A
 %0A    @No

test for caches