shuzirra commented on a change in pull request #3342: URL: https://github.com/apache/hadoop/pull/3342#discussion_r712869638
########## File path: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/QueuePathTest.java ########## @@ -0,0 +1,106 @@ +/** + * 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.hadoop.yarn.server.resourcemanager.scheduler.capacity; + +import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableList; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class QueuePathTest { + private static final String TEST_QUEUE = "root.level_1.level_2.level_3"; + + @Test + public void testCreation() { + QueuePath queuePath = new QueuePath(TEST_QUEUE); + + Assert.assertEquals(TEST_QUEUE, queuePath.getFullPath()); + Assert.assertEquals("root.level_1.level_2", queuePath.getParent()); + Assert.assertEquals("level_3", queuePath.getLeafName()); + + QueuePath rootPath = new QueuePath(CapacitySchedulerConfiguration.ROOT); + Assert.assertNull(rootPath.getParent()); + + QueuePath appendedPath = queuePath.createNewLeaf("level_4"); + Assert.assertEquals(TEST_QUEUE + CapacitySchedulerConfiguration.DOT + + "level_4", appendedPath.getFullPath()); + Assert.assertEquals("root.level_1.level_2.level_3", appendedPath.getParent()); + Assert.assertEquals("level_4", appendedPath.getLeafName()); + } + + @Test + public void testEmptyPart() { + QueuePath queuePathWithEmptyPart = new QueuePath("root..level_2"); + QueuePath queuePathWithoutEmptyPart = new QueuePath(TEST_QUEUE); + + Assert.assertTrue(queuePathWithEmptyPart.hasEmptyPart()); + Assert.assertFalse(queuePathWithoutEmptyPart.hasEmptyPart()); + } + + @Test + public void testIterator() { + QueuePath queuePath = new QueuePath(TEST_QUEUE); + QueuePath queuePathWithEmptyPart = new QueuePath("root..level_2"); + QueuePath rootPath = new QueuePath(CapacitySchedulerConfiguration.ROOT); + + List<String> queuePathCollection = ImmutableList.copyOf(queuePath.iterator()); + List<String> queuePathWithEmptyPartCollection = ImmutableList.copyOf( + queuePathWithEmptyPart.iterator()); + List<String> rootPathCollection = ImmutableList.copyOf(rootPath.iterator()); + + Assert.assertEquals(4, queuePathCollection.size()); + Assert.assertEquals(CapacitySchedulerConfiguration.ROOT, queuePathCollection.get(0)); + Assert.assertEquals("level_3", queuePathCollection.get(3)); + + Assert.assertEquals(3, queuePathWithEmptyPartCollection.size()); + Assert.assertEquals(CapacitySchedulerConfiguration.ROOT, + queuePathWithEmptyPartCollection.get(0)); + Assert.assertEquals("level_2", queuePathWithEmptyPartCollection.get(2)); + + Assert.assertEquals(1, rootPathCollection.size()); + Assert.assertEquals(CapacitySchedulerConfiguration.ROOT, rootPathCollection.get(0)); + } + + @Test + public void testReversePathIterator() { + QueuePath queuePath = new QueuePath(TEST_QUEUE); + QueuePath queuePathWithEmptyPart = new QueuePath("root..level_2"); + QueuePath rootPath = new QueuePath(CapacitySchedulerConfiguration.ROOT); + + List<QueuePath> queuePathCollection = ImmutableList.copyOf(queuePath.reversePathIterator()); + List<QueuePath> queuePathWithEmptyPartCollection = ImmutableList.copyOf( + queuePathWithEmptyPart.reversePathIterator()); + List<QueuePath> rootPathCollection = ImmutableList.copyOf(rootPath.reversePathIterator()); + + Assert.assertEquals(4, queuePathCollection.size()); + Assert.assertEquals(CapacitySchedulerConfiguration.ROOT, + queuePathCollection.get(3).getFullPath()); + Assert.assertEquals(TEST_QUEUE, queuePathCollection.get(0).getFullPath()); + + Assert.assertEquals(3, queuePathWithEmptyPartCollection.size()); + Assert.assertEquals(CapacitySchedulerConfiguration.ROOT, + queuePathWithEmptyPartCollection.get(2).getFullPath()); + Assert.assertEquals("root..level_2", queuePathWithEmptyPartCollection.get(0).getFullPath()); + + Assert.assertEquals(1, rootPathCollection.size()); + Assert.assertEquals(CapacitySchedulerConfiguration.ROOT, + rootPathCollection.get(0).getFullPath()); + } +} Review comment: It doesn't really matter what is the origin of the code, it's always good to cover it with testcases if possible. Even if we assume the IntelliJ code is perfect, the testcases also protect the code against invalid changes, so if someone alters the code, the test can point out possible errors, and since we have a custom equals, if we extend the class we might add new fileds which can result in modified equals function. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
