/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. */ package com.github.rodionmoiseev.c10n.tools.inspector; import com.github.rodionmoiseev.c10n.C10N; import com.github.rodionmoiseev.c10n.C10NConfigBase; import com.github.rodionmoiseev.c10n.annotations.DefaultC10NAnnotations; import com.github.rodionmoiseev.c10n.test.utils.RuleUtils; import com.github.rodionmoiseev.c10n.tools.inspector.test1.*; import com.github.rodionmoiseev.c10n.tools.search.SearchModule; import com.google.common.collect.Iterables; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestRule; import java.util.List; import java.util.Locale; import java.util.Set; import static java.util.Locale.ENGLISH; import static java.util.Locale.JAPANESE; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; /** * @author rodion */ public class DefaultC10NInspectorTest extends AbstractC10NInspectorTest { @Rule public TestRule tmpC10N = RuleUtils.tmpC10NConfiguration(); private final Set<Locale> localesToCheck = set(Locale.ENGLISH, Locale.JAPANESE); private final C10NConfigBase conf = new C10NConfigBase() { @Override protected void configure() { install(new DefaultC10NAnnotations()); bindBundle("com.github.rodionmoiseev.c10n.tools.inspector.test1.Test1"); } }; private final C10NInspector checker = new DefaultC10NInspector(SearchModule.reflectionsSearch(), C10N.configure(conf), new DefaultDummyInstanceProvider(), localesToCheck, true); private static final String testMsgPackage = "com.github.rodionmoiseev.c10n.tools.inspector.test1"; public DefaultC10NInspectorTest() { super(Locale.ENGLISH, Locale.JAPANESE); } @Test public void allKeysAreDiscovered() { assertThat(Iterables.size(checker.inspect(testMsgPackage)), is(17)); } @Test public void annotationOnlyUnits() { List<C10NUnit> units = checker.inspect(testMsgPackage); //key in both bundles, without annotations checkUnit(units, AnnotationOnly.class, "both", anyKey(), annotationTr(ENGLISH, "[en]@annotation AnnotationOnly.both"), annotationTr(JAPANESE, "[ja]@annotation AnnotationOnly.both")); checkUnit(units, AnnotationOnly.class, "onlyEn", anyKey(), annotationTr(ENGLISH, "[en]@annotation AnnotationOnly.onlyEn")); } @Test public void autoGeneratedBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, AutoKey.class, "both", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.both"), bundleTr(ENGLISH, "[en]@bundle AutoKey.both"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.both")); checkUnit(units, AutoKey.class, "onlyJa", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.onlyJa"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.onlyJa")); checkUnit(units, AutoKey.class, "enInAnnotationJaInBundle", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.enInAnnotationJaInBundle"), annotationTr(ENGLISH, "[en]@annotation AutoKey.enInAnnotationJaInBundle"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.enInAnnotationJaInBundle")); checkUnit(units, AutoKey.class, "none", anyKey()); } @Test public void classScopeBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, ClassScopeKey.class, "both", customKey("scope.class.both", null), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.both"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.both")); checkUnit(units, ClassScopeKey.class, "bothInAnnotationAndInBundle", customKey("scope.class.bothInAnnotationAndInBundle", null), annotationTr(ENGLISH, "[en]@bundle ClassScopeKey.bothInAnnotationAndInBundle", "[en]@annotation ClassScopeKey.bothInAnnotationAndInBundle"), annotationTr(JAPANESE, "[ja]@bundle ClassScopeKey.bothInAnnotationAndInBundle", "[ja]@annotation ClassScopeKey.bothInAnnotationAndInBundle"), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.bothInAnnotationAndInBundle"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.bothInAnnotationAndInBundle")); } @Test public void methodKeyOnlyBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, MethodOnlyKey.class, "both", customKey("scope.method", "scope.method"), bundleTr(ENGLISH, "[en]@bundle MethodOnlyKey.both"), bundleTr(JAPANESE, "[ja]@bundle MethodOnlyKey.both")); } @Test public void classAndMethodScopeBundleKeys() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, MethodScopeKey.class, "both", customKey("scope.class.scope.method.both", "scope.method.both"), bundleTr(ENGLISH, "[en]@bundle MethodScopeKey.both"), bundleTr(JAPANESE, "[ja]@bundle MethodScopeKey.both")); } @Test public void extendingAnnotationOnlyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfAnnotationOnly.class, "sub_both", anyKey(), annotationTr(ENGLISH, "[en]@annotation SubInterfaceOfAnnotationOnly.sub_both"), annotationTr(JAPANESE, "[ja]@annotation SubInterfaceOfAnnotationOnly.sub_both")); } @Test public void extendingAutoGeneratedBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfAutoKey.class, "sub_both", autoKey("com.github.rodionmoiseev.c10n.tools.inspector.test1.SubInterfaceOfAutoKey.sub_both"), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfAutoKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfAutoKey.sub_both")); } @Test public void extendingClassScopedBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfClassScopedKey.class, "sub_both", customKey("scope.subclass.sub_both", null), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfClassScopedKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfClassScopedKey.sub_both")); } @Test public void extendingMethodScopeBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfMethodScopedKey.class, "sub_both", customKey("scope.subclass.scope.submethod", "scope.submethod"), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfMethodScopedKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfMethodScopedKey.sub_both")); } @Test public void extendingMethodOnlyBundleKeyInterface() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, SubInterfaceOfMethodOnlyKey.class, "sub_both", customKey("scope.submethod", "scope.submethod"), bundleTr(ENGLISH, "[en]@bundle SubInterfaceOfMethodOnlyKey.sub_both"), bundleTr(JAPANESE, "[ja]@bundle SubInterfaceOfMethodOnlyKey.sub_both")); } @Test public void annotationOnlyUnitsWithParams() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, AnnotationOnly.class, method("bothWithParams", String.class, int.class), anyKey(), annotationTr(ENGLISH, "[en]@annotation AnnotationOnly.bothWithParams_{0}_1"), annotationTr(JAPANESE, "[ja]@annotation AnnotationOnly.bothWithParams_{0}_1")); } @Test public void classScopedBundleKeysWithParams() { List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, ClassScopeKey.class, method("bothInBundleWithParams", Integer.class, byte.class), customKey("scope.class.bothInBundleWithParams_Integer_byte", null), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.bothInBundleWithParams_0_1"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.bothInBundleWithParams_0_1")); } @Test public void inspectorHonorsGlobalKeyPrefix() { C10NConfigBase conf = new C10NConfigBase() { @Override protected void configure() { install(new DefaultC10NAnnotations()); bindBundle("com.github.rodionmoiseev.c10n.tools.inspector.test1.Test1"); setKeyPrefix("prefix"); } }; C10NInspector checker = new DefaultC10NInspector(SearchModule.reflectionsSearch(), C10N.configure(conf), new DefaultDummyInstanceProvider(), localesToCheck, true); List<C10NUnit> units = checker.inspect(testMsgPackage); checkUnit(units, AutoKey.class, "both", autoKey("prefix.com.github.rodionmoiseev.c10n.tools.inspector.test1.AutoKey.both"), bundleTr(ENGLISH, "[en]@bundle AutoKey.both (prefix)"), bundleTr(JAPANESE, "[ja]@bundle AutoKey.both (prefix)")); checkUnit(units, ClassScopeKey.class, "both", customKey("prefix.scope.class.both", null), bundleTr(ENGLISH, "[en]@bundle ClassScopeKey.both (prefix)"), bundleTr(JAPANESE, "[ja]@bundle ClassScopeKey.both (prefix)")); checkUnit(units, MethodScopeKey.class, "both", customKey("prefix.scope.class.scope.method.both", "scope.method.both"), bundleTr(ENGLISH, "[en]@bundle MethodScopeKey.both (prefix)"), bundleTr(JAPANESE, "[ja]@bundle MethodScopeKey.both (prefix)")); } }