Repository: ambari Updated Branches: refs/heads/trunk 381b474d6 -> 5e72ee6f2
AMBARI-19365. Executing ambari-server unit tests with JDK 1.8 results in unit test failures (rlevas) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/5e72ee6f Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/5e72ee6f Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/5e72ee6f Branch: refs/heads/trunk Commit: 5e72ee6f2c6ebcda105e521d819f95ffde1b245e Parents: 381b474 Author: Robert Levas <[email protected]> Authored: Thu Jan 5 12:53:53 2017 -0500 Committer: Robert Levas <[email protected]> Committed: Thu Jan 5 12:53:59 2017 -0500 ---------------------------------------------------------------------- .../controller/utilities/KerberosChecker.java | 50 +++++---- .../utilities/LoginContextHelper.java | 56 ++++++++++ .../utilities/KerberosCheckerTest.java | 43 +++----- .../system/impl/JvmMetricsSourceTest.java | 75 ++++++++++++- .../AmbariBasicAuthenticationFilterTest.java | 37 ++----- .../server/upgrade/UpgradeCatalog222Test.java | 4 +- .../view/persistence/DataStoreImplTest.java | 110 +++++++++---------- 7 files changed, 235 insertions(+), 140 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/5e72ee6f/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java index e0a3d55..2ca9735 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java @@ -1,4 +1,4 @@ -/** +/* * 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 @@ -35,19 +35,25 @@ import com.sun.security.auth.callback.TextCallbackHandler; public class KerberosChecker { - private static final String HTTP_SPNEGO_STANDARD_ENTRY = - "com.sun.security.jgss.krb5.initiate"; + static final String HTTP_SPNEGO_STANDARD_ENTRY = + "com.sun.security.jgss.krb5.initiate"; private static final String KRB5_LOGIN_MODULE = - "com.sun.security.auth.module.Krb5LoginModule"; + "com.sun.security.auth.module.Krb5LoginModule"; public static final String JAVA_SECURITY_AUTH_LOGIN_CONFIG = - "java.security.auth.login.config"; + "java.security.auth.login.config"; - static Logger LOG = LoggerFactory.getLogger(KerberosChecker.class); + private static Logger LOG = LoggerFactory.getLogger(KerberosChecker.class); @Inject static Configuration config; /** + * Used to help create new LoginContext instances + */ + @Inject + static LoginContextHelper loginContextHelper; + + /** * Checks Ambari Server with a Kerberos principal and keytab to allow views * to authenticate via SPNEGO against cluster components. * @@ -61,14 +67,14 @@ public class KerberosChecker { String jaasConfPath = System.getProperty(JAVA_SECURITY_AUTH_LOGIN_CONFIG); javax.security.auth.login.Configuration jaasConf = - javax.security.auth.login.Configuration.getConfiguration(); + javax.security.auth.login.Configuration.getConfiguration(); AppConfigurationEntry[] jaasConfEntries = - jaasConf.getAppConfigurationEntry(HTTP_SPNEGO_STANDARD_ENTRY); + jaasConf.getAppConfigurationEntry(HTTP_SPNEGO_STANDARD_ENTRY); if (jaasConfEntries == null) { LOG.warn("Can't find " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + - jaasConfPath); + jaasConfPath); } else { boolean krb5LoginModulePresent = false; for (AppConfigurationEntry ace : jaasConfEntries) { @@ -83,41 +89,39 @@ public class KerberosChecker { LOG.warn(keytabPath + " doesn't exist."); } else if (!keytabFile.canRead()) { LOG.warn("Unable to read " + keytabPath + - " Please check the file access permissions for user " + - System.getProperty("user.name")); + " Please check the file access permissions for user " + + System.getProperty("user.name")); } } else { LOG.warn("Can't find keyTab option in " + KRB5_LOGIN_MODULE + - " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + - jaasConfPath); } + " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + + jaasConfPath); + } if (!options.containsKey("principal")) { LOG.warn("Can't find principal option in " + KRB5_LOGIN_MODULE + - " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + - jaasConfPath); + " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + + jaasConfPath); } } } } if (!krb5LoginModulePresent) { LOG.warn("Can't find " + KRB5_LOGIN_MODULE + " module in " + - HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + jaasConfPath); + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + jaasConfPath); } } - TextCallbackHandler textHandler = new TextCallbackHandler(); try { - LoginContext loginContext = new LoginContext(HTTP_SPNEGO_STANDARD_ENTRY, - textHandler); + LoginContext loginContext = loginContextHelper.createLoginContext(HTTP_SPNEGO_STANDARD_ENTRY); loginContext.login(); loginContext.logout(); - } - catch (LoginException le) { + } catch (LoginException le) { LOG.error(le.getMessage()); throw new AmbariException( - "Ambari Server Kerberos credentials check failed. \n" + - "Check KDC availability and JAAS configuration in " + jaasConfPath); + "Ambari Server Kerberos credentials check failed. \n" + + "Check KDC availability and JAAS configuration in " + jaasConfPath); } LOG.info("Ambari Server Kerberos credentials check passed."); http://git-wip-us.apache.org/repos/asf/ambari/blob/5e72ee6f/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java new file mode 100644 index 0000000..1a675ae --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java @@ -0,0 +1,56 @@ +/* + * 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 org.apache.ambari.server.controller.utilities; + +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; + +import com.google.inject.Singleton; + +/** + * LoginContextHelper is a helper class used for helping to use and manage {@link LoginContext} instances. + */ +@Singleton +public class LoginContextHelper { + + /** + * Create a new {@link LoginContext} + * + * @param krb5ModuleEntryName the relevant com.sun.security.auth.module.Krb5LoginModule entry name + * @param callbackHandler a callback handler + * @return a new {@link LoginContext} + * @throws LoginException see {@link LoginContext#LoginContext(String, CallbackHandler)} + */ + public LoginContext createLoginContext(String krb5ModuleEntryName, CallbackHandler callbackHandler) + throws LoginException { + return new LoginContext(krb5ModuleEntryName, callbackHandler); + } + + /** + * Create a new {@link LoginContext} + * + * @param krb5ModuleEntryName the relevant com.sun.security.auth.module.Krb5LoginModule entry name + * @return a new {@link LoginContext} + * @throws LoginException see {@link LoginContext#LoginContext(String, CallbackHandler)} + */ + public LoginContext createLoginContext(String krb5ModuleEntryName) throws LoginException { + return new LoginContext(krb5ModuleEntryName); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/5e72ee6f/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java index 570a50b..44e1927 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java @@ -1,4 +1,4 @@ -/** +/* * 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 @@ -18,47 +18,38 @@ package org.apache.ambari.server.controller.utilities; -import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.isA; -import static org.powermock.api.easymock.PowerMock.expectNew; -import static org.powermock.api.easymock.PowerMock.replay; -import static org.powermock.api.easymock.PowerMock.verifyAll; -import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.configuration.Configuration; +import org.easymock.EasyMockSupport; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -@RunWith(PowerMockRunner.class) -@PrepareForTest({LoginContext.class, KerberosChecker.class}) -public class KerberosCheckerTest { +public class KerberosCheckerTest extends EasyMockSupport { @Test public void testCheckPassed() throws Exception { - Configuration config = createMock(Configuration.class); - LoginContext lc = createMock(LoginContext.class); + Configuration config = createMock(Configuration.class); + LoginContextHelper loginContextHelper = createMock(LoginContextHelper.class); + LoginContext lc = createMock(LoginContext.class); expect(config.isKerberosJaasConfigurationCheckEnabled()).andReturn(true).once(); - expectNew(LoginContext.class, new Class<?>[] { String.class, CallbackHandler.class }, - isA(String.class), isA(CallbackHandler.class) ) - .andReturn(lc); + expect(loginContextHelper.createLoginContext(KerberosChecker.HTTP_SPNEGO_STANDARD_ENTRY)).andReturn(lc).once(); + lc.login(); expectLastCall().once(); lc.logout(); expectLastCall().once(); - replay(config, LoginContext.class, lc); + replayAll(); KerberosChecker.config = config; + KerberosChecker.loginContextHelper = loginContextHelper; KerberosChecker.checkJaasConfiguration(); verifyAll(); @@ -66,23 +57,19 @@ public class KerberosCheckerTest { @Test(expected = AmbariException.class) public void testCheckFailed() throws Exception { - Configuration config = createMock(Configuration.class); - LoginContext lc = createMock(LoginContext.class); + Configuration config = createMock(Configuration.class); + LoginContextHelper loginContextHelper = createMock(LoginContextHelper.class); expect(config.isKerberosJaasConfigurationCheckEnabled()).andReturn(true).once(); - expectNew(LoginContext.class, new Class<?>[] { String.class, CallbackHandler.class }, - isA(String.class), isA(CallbackHandler.class) ) - .andReturn(lc); - lc.login(); - expectLastCall().andThrow(new LoginException()).once(); + expect(loginContextHelper.createLoginContext(KerberosChecker.HTTP_SPNEGO_STANDARD_ENTRY)).andThrow(new LoginException()).once(); - replay(config, LoginContext.class, lc); + replayAll(); KerberosChecker.config = config; + KerberosChecker.loginContextHelper = loginContextHelper; KerberosChecker.checkJaasConfiguration(); verifyAll(); } - } http://git-wip-us.apache.org/repos/asf/ambari/blob/5e72ee6f/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java index a6aa5d5..4bc930b 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java @@ -1,4 +1,4 @@ -/** +/* * 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 @@ -18,19 +18,88 @@ package org.apache.ambari.server.metric.system.impl; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.apache.ambari.server.metrics.system.MetricsSink; import org.apache.ambari.server.metrics.system.impl.JvmMetricsSource; import org.apache.ambari.server.metrics.system.impl.MetricsConfiguration; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.Assume; import org.junit.Test; public class JvmMetricsSourceTest { @Test - public void testJvmSourceInit() { + public void testJvmSourceInit_PreJVM1_8() { + Assume.assumeThat(System.getProperty("java.version"), new LessThanVersionMatcher("1.8")); + testJvmSourceInit(39); + } + + @Test + public void testJvmSourceInit_JVM1_8() { + Assume.assumeThat(System.getProperty("java.version"), new VersionMatcher("1.8")); + testJvmSourceInit(40); + } + + private void testJvmSourceInit(int metricsSize) { JvmMetricsSource jvmMetricsSource = new JvmMetricsSource(); MetricsConfiguration configuration = MetricsConfiguration.getMetricsConfiguration(); MetricsSink sink = new TestAmbariMetricsSinkImpl(); jvmMetricsSource.init(configuration, sink); - org.junit.Assert.assertEquals(jvmMetricsSource.getMetrics().size(), 39); + org.junit.Assert.assertEquals(jvmMetricsSource.getMetrics().size(), metricsSize); + } + + /* **************************************************************** + * Matcher classes used in Assume checks + * **************************************************************** */ + private class VersionMatcher extends BaseMatcher<String> { + private final float version; + + VersionMatcher(String version) { + this.version = Float.parseFloat(version); + } + + @Override + public boolean matches(Object o) { + return parseVersion((String) o) == this.version; + } + + float parseVersion(String versionString) { + Pattern p = Pattern.compile("(\\d+(?:\\.\\d+)).*"); + Matcher matcher = p.matcher(versionString); + if (matcher.matches()) { + return Float.parseFloat(matcher.group(1)); + } else { + return 0f; + } + } + + @Override + public void describeTo(Description description) { + + } + + public float getVersion() { + return version; + } + } + + private class LessThanVersionMatcher extends VersionMatcher { + + LessThanVersionMatcher(String version) { + super(version); + } + + @Override + public boolean matches(Object o) { + return parseVersion((String) o) < getVersion(); + } + + @Override + public void describeTo(Description description) { + + } } } http://git-wip-us.apache.org/repos/asf/ambari/blob/5e72ee6f/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java index 7e36128..6775211 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java @@ -1,4 +1,4 @@ -/** +/* * 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 @@ -18,11 +18,8 @@ package org.apache.ambari.server.security.authentication; import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import java.io.IOException; import java.util.Arrays; @@ -38,21 +35,15 @@ import javax.servlet.http.HttpServletResponse; import org.apache.ambari.server.audit.AuditLogger; import org.apache.ambari.server.audit.event.AuditEvent; import org.apache.ambari.server.security.AmbariEntryPoint; -import org.apache.ambari.server.security.authorization.AuthorizationHelper; import org.apache.ambari.server.security.authorization.PermissionHelper; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.easymock.EasyMockSupport; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.crypto.codec.Base64; -@RunWith(PowerMockRunner.class) -@PrepareForTest(AuthorizationHelper.class) -public class AmbariBasicAuthenticationFilterTest { +public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport { private AmbariBasicAuthenticationFilter underTest; @@ -68,7 +59,6 @@ public class AmbariBasicAuthenticationFilterTest { permissionHelper = createMock(PermissionHelper.class); entryPoint = createMock(AmbariEntryPoint.class); underTest = new AmbariBasicAuthenticationFilter(null, entryPoint, mockedAuditLogger, permissionHelper); - replay(entryPoint); } @Test @@ -84,11 +74,11 @@ public class AmbariBasicAuthenticationFilterTest { expectLastCall().times(1); filterChain.doFilter(request, response); expectLastCall(); - replay(mockedAuditLogger, request, filterChain); + replayAll(); // WHEN underTest.doFilter(request, response, filterChain); // THEN - verify(mockedAuditLogger, request, filterChain); + verifyAll(); } @Test @@ -97,26 +87,21 @@ public class AmbariBasicAuthenticationFilterTest { HttpServletRequest request = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); Authentication authentication = createMock(Authentication.class); - PowerMock.mockStatic(AuthorizationHelper.class); Map<String, List<String>> roles = new HashMap<>(); roles.put("a", Arrays.asList("r1", "r2", "r3")); expect(permissionHelper.getPermissionLabels(authentication)) - .andReturn(roles); - expect(AuthorizationHelper.getAuthorizationNames(authentication)) - .andReturn(Arrays.asList("perm1", "perm2")); - expect(AuthorizationHelper.getAuthenticatedName()).andReturn("perm1"); + .andReturn(roles); expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4"); expect(authentication.getName()).andReturn("admin"); expect(mockedAuditLogger.isEnabled()).andReturn(true); mockedAuditLogger.log(anyObject(AuditEvent.class)); expectLastCall().times(1); - replay(mockedAuditLogger, request, authentication, permissionHelper); - PowerMock.replayAll(); + replayAll(); // WHEN underTest.onSuccessfulAuthentication(request, response, authentication); // THEN - verify(mockedAuditLogger, request); + verifyAll(); } @Test @@ -127,14 +112,14 @@ public class AmbariBasicAuthenticationFilterTest { AuthenticationException authEx = createMock(AuthenticationException.class); expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4"); expect(request.getHeader("Authorization")).andReturn( - "Basic " + new String(Base64.encode("admin:admin".getBytes("UTF-8")))); + "Basic " + new String(Base64.encode("admin:admin".getBytes("UTF-8")))); expect(mockedAuditLogger.isEnabled()).andReturn(true); mockedAuditLogger.log(anyObject(AuditEvent.class)); expectLastCall().times(1); - replay(mockedAuditLogger, request, authEx); + replayAll(); // WHEN underTest.onUnsuccessfulAuthentication(request, response, authEx); // THEN - verify(mockedAuditLogger, request, authEx); + verifyAll(); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/5e72ee6f/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java index 9d4fb3e..82ba149 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java @@ -43,6 +43,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.TreeMap; import javax.persistence.EntityManager; @@ -1083,7 +1084,8 @@ public class UpgradeCatalog222Test { final Service kafkaService = createStrictMock(Service.class); final Service hbaseService = createStrictMock(Service.class); final Map<String, Cluster> clusterMap = Collections.singletonMap("c1", cluster); - final Map<String, Service> services = new HashMap<>(); + // Use a TreeMap so we can assume a particular order when iterating over the services. + final Map<String, Service> services = new TreeMap<>(); services.put(stormServiceName, stormService); services.put(kafkaServiceName, kafkaService); services.put(hbaseServiceName, hbaseService); http://git-wip-us.apache.org/repos/asf/ambari/blob/5e72ee6f/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java index 60643b3..a1c910d 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java @@ -1,4 +1,4 @@ -/** +/* * 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 @@ -7,7 +7,7 @@ * "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 + * 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, @@ -80,23 +80,23 @@ import com.google.inject.Module; @PrepareForTest(JpaHelper.class) public class DataStoreImplTest { private final static String xml = "<view>\n" + - " <name>MY_VIEW</name>\n" + - " <label>My View!</label>\n" + - " <version>1.0.0</version>\n" + - " <instance>\n" + - " <name>INSTANCE1</name>\n" + - " </instance>\n" + - " <persistence>\n" + - " <entity>\n" + - " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestEntity</class>\n" + - " <id-property>id</id-property>\n" + - " </entity>\n" + - " <entity>\n" + - " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestSubEntity</class>\n" + - " <id-property>id</id-property>\n" + - " </entity>\n" + - " </persistence>" + - "</view>"; + " <name>MY_VIEW</name>\n" + + " <label>My View!</label>\n" + + " <version>1.0.0</version>\n" + + " <instance>\n" + + " <name>INSTANCE1</name>\n" + + " </instance>\n" + + " <persistence>\n" + + " <entity>\n" + + " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestEntity</class>\n" + + " <id-property>id</id-property>\n" + + " </entity>\n" + + " <entity>\n" + + " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestSubEntity</class>\n" + + " <id-property>id</id-property>\n" + + " </entity>\n" + + " </persistence>" + + "</view>"; @Test public void testStore_create() throws Exception { @@ -128,7 +128,7 @@ public class DataStoreImplTest { @Override public Object answer() throws Throwable { ((DynamicEntity) EasyMock.getCurrentArguments()[0]) - .set("DS_id", 99); // for TestSubEntity + .set("DS_id", 99); // for TestSubEntity return null; } }); @@ -139,7 +139,7 @@ public class DataStoreImplTest { @Override public Object answer() throws Throwable { ((DynamicEntity) EasyMock.getCurrentArguments()[0]) - .set("DS_id", 100); // for TestEntity + .set("DS_id", 100); // for TestEntity return null; } }); @@ -215,7 +215,7 @@ public class DataStoreImplTest { DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 5000; ++i) { sb.append("A"); } @@ -352,8 +352,14 @@ public class DataStoreImplTest { dataStore.store(new TestEntity(100, "foo", new TestSubEntity(99, "bar"))); - Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass()); - Assert.assertEquals(entityClassCapture2.getValue(), typeCapture2.getValue().getJavaClass()); + if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && + (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { + Assert.fail(); + } + if ((entityClassCapture2.getValue() != typeCapture.getValue().getJavaClass()) && + (entityClassCapture2.getValue() != typeCapture2.getValue().getJavaClass())) { + Assert.fail(); + } // verify mocks verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin, dynamicSubEntity); @@ -396,13 +402,13 @@ public class DataStoreImplTest { entityManager.close(); - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (int i = 0; i < 5000; ++i) { sb.append("A"); } String longString = sb.toString(); - expect(dynamicEntity.set("DS_id", 99)).andReturn(dynamicEntity).once(); + expect(dynamicEntity.set("DS_id", 99)).andReturn(dynamicEntity).anyTimes(); transaction.begin(); expect(transaction.isActive()).andReturn(true).anyTimes(); @@ -470,7 +476,10 @@ public class DataStoreImplTest { dataStore.remove(new TestEntity(99, "foo", new TestSubEntity("bar"))); - Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass()); + if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && + (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { + Assert.fail(); + } // verify mocks verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin); @@ -521,7 +530,11 @@ public class DataStoreImplTest { TestEntity entity = dataStore.find(TestEntity.class, 99); - Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass()); + // Ensure the requested class type is one of the available types.... + if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && + (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { + Assert.fail(); + } Assert.assertEquals(99, (int) entity.getId()); Assert.assertEquals("foo", entity.getName()); @@ -559,7 +572,7 @@ public class DataStoreImplTest { expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes(); expect(entityManager.createQuery( - "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_id=99")).andReturn(query); + "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_id=99")).andReturn(query); entityManager.close(); expect(query.getResultList()).andReturn(Collections.singletonList(dynamicEntity)); @@ -619,7 +632,7 @@ public class DataStoreImplTest { expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes(); expect(entityManager.createQuery( - "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_name='foo'")).andReturn(query); + "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_name='foo'")).andReturn(query); entityManager.close(); List<DynamicEntity> entityList = new LinkedList<DynamicEntity>(); @@ -646,7 +659,7 @@ public class DataStoreImplTest { // replay mocks replay(entityManagerFactory, entityManager, jpaDynamicHelper, - dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); + dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); @@ -660,14 +673,14 @@ public class DataStoreImplTest { // verify mocks verify(entityManagerFactory, entityManager, jpaDynamicHelper, - dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); + dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); } private DataStoreImpl getDataStore(EntityManagerFactory entityManagerFactory, JPADynamicHelper jpaDynamicHelper, DynamicClassLoader classLoader, SchemaManager schemaManager) - throws Exception { + throws Exception { ViewConfig viewConfig = ViewConfigTest.getConfig(xml); ViewEntity viewDefinition = ViewEntityTest.getViewEntity(viewConfig); @@ -677,7 +690,7 @@ public class DataStoreImplTest { setPersistenceEntities(viewInstanceEntity); Injector injector = Guice.createInjector( - new TestModule(viewInstanceEntity, entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager)); + new TestModule(viewInstanceEntity, entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager)); return injector.getInstance(DataStoreImpl.class); } @@ -709,13 +722,13 @@ public class DataStoreImplTest { public TestEntity() { } - public TestEntity(int id, String name, TestSubEntity subEntity) { + TestEntity(int id, String name, TestSubEntity subEntity) { this.id = id; this.name = name; this.subEntity = subEntity; } - public TestEntity(String name, TestSubEntity subEntity) { + TestEntity(String name, TestSubEntity subEntity) { this.name = name; this.subEntity = subEntity; } @@ -756,11 +769,11 @@ public class DataStoreImplTest { public TestSubEntity() { } - public TestSubEntity(String name) { + TestSubEntity(String name) { this.name = name; } - public TestSubEntity(Integer id, String name) { + TestSubEntity(Integer id, String name) { this.id = id; this.name = name; } @@ -794,27 +807,6 @@ public class DataStoreImplTest { } Integer id = null; - String f1; - String f2; - String f3; - String f4; - String f5; - String f6; - String f7; - String f8; - String f9; - String f10; - String f11; - String f12; - String f13; - String f14; - String f15; - String f16; - String f17; - String f18; - String f19; - String f20; - String f21; public Integer getId() { return id;
