Skip to content

Commit

Permalink
completed resource bundle in api and returned to 100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed Apr 24, 2020
1 parent 2bb9008 commit 454d447
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 27 deletions.
17 changes: 10 additions & 7 deletions api/src/main/java/net/adamcin/oakpal/api/SimpleProgressCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import org.apache.jackrabbit.vault.packaging.PackageId;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ConsumerType;

import java.util.Collection;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.function.Consumer;

Expand Down Expand Up @@ -49,10 +51,12 @@ public void setResourceBundle(final ResourceBundle resourceBundle) {
* @see ResourceBundle#getBundle(String)
* @see ViolationReporter#getResourceBundleBaseName()
*/
@NotNull
@Nullable
protected ResourceBundle getResourceBundle() throws MissingResourceException {
if (this.resourceBundle == null) {
this.resourceBundle = ResourceBundle.getBundle(getResourceBundleBaseName());
if (getResourceBundleBaseName() != null) {
this.resourceBundle = ResourceBundle.getBundle(getResourceBundleBaseName());
}
}
return this.resourceBundle;
}
Expand All @@ -66,11 +70,10 @@ protected ResourceBundle getResourceBundle() throws MissingResourceException {
*/
@NotNull
protected String getString(@NotNull final String key) {
if (getResourceBundle().containsKey(key)) {
return getResourceBundle().getString(key);
} else {
return key;
}
return Optional.ofNullable(getResourceBundle())
.filter(bundle -> bundle.containsKey(key))
.map(bundle -> bundle.getString(key))
.orElse(key);
}

protected void reportViolation(final Violation violation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package net.adamcin.oakpal.api;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ConsumerType;

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ public String getCheckName() {
}

@Override
public @NotNull String getResourceBundleBaseName() {
public @Nullable String getResourceBundleBaseName() {
return factoryClass.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package net.adamcin.oakpal.api;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ConsumerType;

import java.util.Collection;
Expand All @@ -30,11 +30,13 @@ public interface ViolationReporter {

/**
* Get the resource bundle base name for loading the default resource bundle for this violation reporter. Returns
* {@code getClass().getName()} by default.
* {@code getClass().getName()} by default. If this method is overridden to return null, the oakpal framework will
* not attempt to load a parent resource bundle specific to this violation reporter when creating a resource bundle
* during initialization.
*
* @return the resource bundle base name
*/
@NotNull
@Nullable
default String getResourceBundleBaseName() {
return getClass().getName();
}
Expand Down
8 changes: 5 additions & 3 deletions core/src/main/java/net/adamcin/oakpal/core/OakpalPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ public OakMachine.Builder toOakMachineBuilder(final @Nullable ErrorListener erro
}

void initResourceBundle(final ViolationReporter reporter, final Locale locale, final ClassLoader classLoader) {
Fun.result0(() -> ResourceBundle
.getBundle(reporter.getResourceBundleBaseName(), locale, classLoader)).get()
.forEach(reporter::setResourceBundle);
if (reporter.getResourceBundleBaseName() != null) {
Fun.result0(() -> ResourceBundle
.getBundle(reporter.getResourceBundleBaseName(), locale, classLoader)).get()
.forEach(reporter::setResourceBundle);
}
}

private static OakpalPlan fromJson(final @NotNull Builder builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public String getCheckName() {
}

@Override
public @NotNull String getResourceBundleBaseName() {
public @Nullable String getResourceBundleBaseName() {
return wrapped.getResourceBundleBaseName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,19 @@ public final class ScriptProgressCheck implements ProgressCheck {
}

@Override
public @NotNull String getResourceBundleBaseName() {
return getScriptPath()
.replaceAll("^/*", "")
.replaceAll("/", ".");
public @Nullable String getResourceBundleBaseName() {
return null;
}

@Override
public void setResourceBundle(final ResourceBundle resourceBundle) {
this.helper.setResourceBundle(resourceBundle);
}

ScriptHelper getHelper() {
return helper;
}

private String getScriptPath() {
if (this.scriptUrl != null) {
return this.scriptUrl.getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.jackrabbit.vault.packaging.PackageId;
import org.apache.jackrabbit.vault.packaging.PackageProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;

import javax.jcr.Node;
Expand Down Expand Up @@ -200,7 +201,7 @@ public void setResourceBundle(final ResourceBundle resourceBundle) {
}

@Override
protected @NotNull ResourceBundle getResourceBundle() throws MissingResourceException {
protected @Nullable ResourceBundle getResourceBundle() throws MissingResourceException {
return super.getResourceBundle();
}

Expand Down
43 changes: 39 additions & 4 deletions core/src/test/java/net/adamcin/oakpal/core/OakpalPlanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package net.adamcin.oakpal.core;

import net.adamcin.oakpal.api.ProgressCheck;
import net.adamcin.oakpal.api.Result;
import org.apache.jackrabbit.api.JackrabbitWorkspace;
import org.apache.jackrabbit.api.security.authorization.PrivilegeManager;
Expand All @@ -32,7 +33,11 @@
import org.slf4j.LoggerFactory;

import javax.jcr.nodetype.NodeTypeManager;
import javax.json.*;
import javax.json.Json;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -42,13 +47,26 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static net.adamcin.oakpal.api.JavaxJson.*;
import static org.junit.Assert.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;

import static net.adamcin.oakpal.api.JavaxJson.arr;
import static net.adamcin.oakpal.api.JavaxJson.key;
import static net.adamcin.oakpal.api.JavaxJson.obj;
import static net.adamcin.oakpal.api.JavaxJson.wrap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OakpalPlanTest {
private static final Logger LOGGER = LoggerFactory.getLogger(OakpalPlanTest.class);
Expand Down Expand Up @@ -513,4 +531,21 @@ public void testRelativizeToBaseParent() throws Exception {
assertEquals("return relative uri when base is not json",
fooRel, OakpalPlan.relativizeToBaseParent(fooRoot, fooAbs));
}

@Test
public void testInitResourceBundle() throws Exception {
CompletableFuture<ResourceBundle> callback = new CompletableFuture<>();
OakpalPlan plan = builder().build();
ProgressCheck check = mock(ProgressCheck.class);
doAnswer(call -> callback.complete(call.getArgument(0)))
.when(check).setResourceBundle(nullable(ResourceBundle.class));
when(check.getResourceBundleBaseName()).thenReturn(null);
plan.initResourceBundle(check, Locale.getDefault(), getClass().getClassLoader());
assertFalse("expect callback not done", callback.isDone());
when(check.getResourceBundleBaseName()).thenReturn(getClass().getName());
plan.initResourceBundle(check, Locale.getDefault(), getClass().getClassLoader());
assertSame("expect callback complete with", ResourceBundle.getBundle(getClass().getName()),
callback.getNow(null));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.jar.Manifest;

import static net.adamcin.oakpal.api.Fun.toEntry;
import static net.adamcin.oakpal.api.Fun.tryOrDefault1;
import static net.adamcin.oakpal.api.JavaxJson.key;
import static net.adamcin.oakpal.api.JavaxJson.obj;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -451,11 +457,27 @@ public void testGuardSessionHandler_cacheMissing() throws Exception {
check.guardSessionHandler("missingFunction", handle -> handle.apply("test"));
}

PropertyResourceBundle fromPropertiesUrl(URL propertiesUrl) {
try (InputStream propsStream = propertiesUrl.openStream()) {
return new PropertyResourceBundle(propsStream);
} catch (IOException e) {
return null;
}
}

@Test
public void testGetResourceBundleBaseName() throws Exception {
final ProgressCheck check = ScriptProgressCheck
final ScriptProgressCheck check = (ScriptProgressCheck) ScriptProgressCheck
.createScriptCheckFactory(testScriptUrl("checkWithResources.js")).newInstance(null);
assertEquals("check getResourceBundleBaseName should be", "some check", check.getResourceBundleBaseName());

assertEquals("expect testKey=testKey", "testKey", check.getHelper().getString("testKey"));

check.setResourceBundle(fromPropertiesUrl(testScriptUrl("checkWithResources.properties")));

assertEquals("expect testKey=yeKtset", "yeKtset", check.getHelper().getString("testKey"));

check.setResourceBundle(fromPropertiesUrl(testScriptUrl("overrideResourceBundle.properties")));

assertEquals("expect testKey=yeKtsettestKey", "yeKtsettestKey", check.getHelper().getString("testKey"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2020 Mark Adamcin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

testKey=yeKtset
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2020 Mark Adamcin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

testKey=yeKtsettestKey
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2020 Mark Adamcin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

testKey=yeKtset

0 comments on commit 454d447

Please sign in to comment.