Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
Fixes: SIRI-295
  • Loading branch information
jmuscireum committed Dec 17, 2020
1 parent ef8251e commit e95f824
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/test/java/sirius/web/security/AnotherExampleHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.web.security;

import sirius.kernel.commons.Explain;
import sirius.kernel.di.std.Register;
import sirius.kernel.health.Exceptions;

import javax.annotation.Nonnull;

public class AnotherExampleHelper {

@Register
public static class AnotherExampleHelperFactory implements HelperFactory<AnotherExampleHelper> {

@Nonnull
@Override
public Class<AnotherExampleHelper> getHelperType() {
return AnotherExampleHelper.class;
}

@Nonnull
@Override
public String getName() {
return "example2";
}

@Nonnull
@Override
@SuppressWarnings("squid:S2925")
@Explain("We need this delay here to test loading friend helpers.")
public AnotherExampleHelper make(@Nonnull ScopeInfo scope) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw Exceptions.handle(e);
}

return new AnotherExampleHelper();
}
}

public String getTestValue() {
return "test";
}
}
45 changes: 45 additions & 0 deletions src/test/java/sirius/web/security/ExampleHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.web.security;

import sirius.kernel.di.std.Register;

import javax.annotation.Nonnull;

public class ExampleHelper {

@Register
public static class ExampleHelperFactory implements HelperFactory<ExampleHelper> {

@Nonnull
@Override
public Class<ExampleHelper> getHelperType() {
return ExampleHelper.class;
}

@Nonnull
@Override
public String getName() {
return "example1";
}

@Nonnull
@Override
public ExampleHelper make(@Nonnull ScopeInfo scope) {
return new ExampleHelper();
}
}

@Helper
private AnotherExampleHelper anotherExampleHelper;

public String getTestValue() {
return anotherExampleHelper.getTestValue();
}
}
35 changes: 35 additions & 0 deletions src/test/java/sirius/web/security/ScopeInfoSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
package sirius.web.security

import sirius.kernel.BaseSpecification
import sirius.kernel.async.Future
import sirius.kernel.async.Tasks
import sirius.kernel.di.std.Part

import java.time.Duration
import java.time.temporal.TemporalUnit
import java.util.concurrent.TimeUnit

class ScopeInfoSpec extends BaseSpecification {

@Part
private static Tasks tasks;

def "default config is loaded"() {
when:
List<String> test = UserContext.getCurrentScope().getDefaultScopeConfigFiles()
Expand All @@ -35,4 +45,29 @@ class ScopeInfoSpec extends BaseSpecification {
value == "# Test\nsettings.test =\"Hello\""
}

def "friend helpers are loaded"() {
when:
String value = UserContext.getCurrentScope().getHelper(ExampleHelper.class).getTestValue()
then:
value == "test"
}

def "helpers are cached in a consistent state"() {
given:
def scope = new ScopeInfo("test", "test", "test", null, null, null)
when:
def future1 = tasks.executor("test").start({ ->
scope.getHelper(ExampleHelper.class).getTestValue();
})
def future2 = tasks.executor("test").start({ ->
scope.getHelper(ExampleHelper.class).getTestValue();
})
and:
future1.await(Duration.ofSeconds(1))
future2.await(Duration.ofSeconds(1))
then:
future1.isSuccessful()
and:
future2.isSuccessful()
}
}

0 comments on commit e95f824

Please sign in to comment.