Skip to content

Commit

Permalink
MemoizerTest: Test for FastCodeExceptionAnalyzer.formatCatch().
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed Dec 19, 2022
1 parent 580a260 commit d5606f9
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/test/java/jd/core/test/MemoizerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jd.core.test;

import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;

/*
* Test for FastCodeExceptionAnalyzer.formatCatch().
*/
public class MemoizerTest extends AbstractTestCase {
@Test
public void test() throws Exception {
String output = decompile("org/apache/commons/lang3/concurrent/Memoizer");
assertEquals(IOUtils.toString(getClass().getResource("Memoizer.txt"), StandardCharsets.UTF_8), output);
}
}
158 changes: 158 additions & 0 deletions src/test/resources/jd/core/test/Memoizer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* */ package org.apache.commons.lang3.concurrent;
/* */
/* */ import java.util.concurrent.Callable;
/* */ import java.util.concurrent.CancellationException;
/* */ import java.util.concurrent.ConcurrentHashMap;
/* */ import java.util.concurrent.ConcurrentMap;
/* */ import java.util.concurrent.ExecutionException;
/* */ import java.util.concurrent.Future;
/* */ import java.util.concurrent.FutureTask;
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public class Memoizer<I, O>
/* */ implements Computable<I, O>
/* */ {
/* 56 */ private final ConcurrentMap<I, Future<O>> cache = new ConcurrentHashMap();
/* */
/* */
/* */
/* */
/* */ private final Computable<I, O> computable;
/* */
/* */
/* */
/* */
/* */ private final boolean recalculate;
/* */
/* */
/* */
/* */
/* */
/* */ public Memoizer(Computable<I, O> computable)
/* */ {
/* 74 */ this(computable, false);
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public Memoizer(Computable<I, O> computable, boolean recalculate)
/* */ {
/* 91 */ this.computable = computable;
/* 92 */ this.recalculate = recalculate;
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public O compute(I arg)
/* */ throws InterruptedException
/* */ {
/* */ for (;;)
/* */ {
/* 116 */ Future<O> future = (Future)this.cache.get(arg);
/* 117 */ if (future == null) {
/* 118 */ Callable<O> eval = () -> { return this.computable.compute(arg); } ;
/* 119 */ FutureTask<O> futureTask = new FutureTask(eval);
/* 120 */ future = (Future)this.cache.putIfAbsent(arg, futureTask);
/* 121 */ if (future == null) {
/* 122 */ future = futureTask;
/* 123 */ futureTask.run();
/* */ }
/* */ }
/* */ try {
/* 127 */ return (O)future.get();
/* */ } catch (CancellationException e) {
/* 129 */ this.cache.remove(arg, future);
/* */ } catch (ExecutionException e) {
/* 131 */ if (this.recalculate) {
/* 132 */ this.cache.remove(arg, future);
/* */ }
/* */
/* 135 */ throw launderException(e.getCause());
/* */ }
/* */ }
/* */ }
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ private RuntimeException launderException(Throwable throwable)
/* */ {
/* 151 */ if ((throwable instanceof RuntimeException))
/* 152 */ return (RuntimeException)throwable;
/* 153 */ if ((throwable instanceof Error)) {
/* 154 */ throw ((Error)throwable);
/* */ }
/* 156 */ throw new IllegalStateException("Unchecked exception", throwable);
/* */ }
/* */ }

0 comments on commit d5606f9

Please sign in to comment.