Skip to content

Commit

Permalink
Merge pull request apache#1979 from hectorespert/test_notification_mo…
Browse files Browse the repository at this point in the history
…dule

Add tests in notifications module
  • Loading branch information
hectorespert authored Jun 9, 2020
2 parents e952ff5 + 6da69e0 commit 97a3bda
Show file tree
Hide file tree
Showing 12 changed files with 633 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

import java.awt.Component;
import org.openide.awt.StatusLineElementProvider;
import org.openide.util.lookup.ServiceProvider;

/**
* Status line element showing icon for Notifications.
*
* @author S. Aubrecht
*/
@org.openide.util.lookup.ServiceProvider(service=org.openide.awt.StatusLineElementProvider.class, position=600)
@ServiceProvider(service=StatusLineElementProvider.class, position=600)
public final class StatusLineElement implements StatusLineElementProvider {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
class CategoryFilter {

private final Set<String> enabledCategories = new HashSet<String>();
private final Set<String> enabledCategories = new HashSet<>();

public CategoryFilter() {
addDefaultTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public final class FilterRepository {
/**
* the set of filters of this repository *
*/
private LinkedList<NotificationFilter> filters = new LinkedList<NotificationFilter>();
private final LinkedList<NotificationFilter> filters = new LinkedList<>();

private int active = -1; // index of the active filter

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public Object clone() {
}

public List<NotificationFilter> getAllFilters() {
return new ArrayList<NotificationFilter>(filters);
return new ArrayList<>(filters);
}

// Implementation of java.util.Set
Expand Down Expand Up @@ -188,6 +189,6 @@ void clear() {
}

List<NotificationFilter> getFilters() {
return new ArrayList<NotificationFilter>(filters);
return new ArrayList<>(filters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class NotificationFilter {
}

private NotificationFilter(NotificationFilter src) {
this.name = src.name;
categoryFilter = null == src.categoryFilter ? null : src.categoryFilter.clone();
this.name = src.getName();
categoryFilter = null == src.getCategoryFilter() ? null : src.getCategoryFilter().clone();
}

public boolean isEnabled(NotificationImpl notification) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
final class Util {

public static String getString(String key) {
return NbBundle.getBundle(Util.class).getString(key);
return NbBundle.getMessage(Util.class, key);
}

public static char getChar(String key) {
return NbBundle.getBundle(Util.class).getString(key).charAt(0);
return NbBundle.getMessage(Util.class, key).charAt(0);
}

public static String getMessage(String key, Object obj) {
return NbBundle.getMessage(Util.class,key, obj);
return NbBundle.getMessage(Util.class, key, obj);
}

public static String getMessage(String key, Object obj, Object obj2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.netbeans.modules.notifications;

import org.junit.Test;
import org.netbeans.junit.NbTestCase;

/**
*
* @author Hector Espert
*/
public class NotificationSettingsTest extends NbTestCase {

public NotificationSettingsTest(String name) {
super(name);
}

@Test
public void testIsSearchVisible() {
NotificationSettings.setSearchVisible(false);
assertFalse(NotificationSettings.isSearchVisible());

NotificationSettings.setSearchVisible(true);
assertTrue(NotificationSettings.isSearchVisible());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.netbeans.modules.notifications;

import java.awt.Component;
import org.junit.Test;
import org.netbeans.junit.NbTestCase;
import org.openide.awt.StatusLineElementProvider;
import org.openide.util.Lookup;

/**
*
* @author Hector Espert
*/
public class StatusLineElementTest extends NbTestCase {

public StatusLineElementTest(String name) {
super(name);
}

@Test
public void testGetStatusLineElement() {
StatusLineElementProvider statusLineElementProvider = Lookup.getDefault().lookup(StatusLineElementProvider.class);
assertNotNull(statusLineElementProvider);
assertEquals(StatusLineElement.class, statusLineElementProvider.getClass());

Component component = statusLineElementProvider.getStatusLineElement();
assertNotNull(component);
assertEquals("Expected that org.netbeans.modules.notifications.FlashingIcon is the default implementation", FlashingIcon.class, component.getClass());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.netbeans.modules.notifications.filter;

import java.util.Iterator;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

/**
*
* @author Hector Espert
*/
public class FilterRepositoryTest {

private FilterRepository filterRepository;

@Before
public void setUp() {
filterRepository = new FilterRepository();
}

@Test
public void testAssign() {
FilterRepository instance = new FilterRepository();
instance.setActive(NotificationFilter.EMPTY);
filterRepository.assign(instance);
assertEquals(instance.getAllFilters(), filterRepository.getAllFilters());
assertEquals(NotificationFilter.EMPTY, filterRepository.getActive());
}

@Test
public void testClone() {
FilterRepository result = (FilterRepository) filterRepository.clone();
assertEquals(filterRepository.getAllFilters(), result.getAllFilters());
assertEquals(filterRepository.getActive(), result.getActive());
}

@Test
public void testGetAllFilters() {
assertNotNull(filterRepository.getAllFilters());
}

@Test
public void testAddAndRemoveFilter() {
filterRepository.add(NotificationFilter.EMPTY);
assertTrue(filterRepository.getFilters().contains(NotificationFilter.EMPTY));
filterRepository.remove(NotificationFilter.EMPTY);
assertFalse(filterRepository.getFilters().contains(NotificationFilter.EMPTY));
}

@Test
public void testIterator() {
assertTrue(filterRepository.iterator() instanceof Iterator);
}

@Test
public void testSize() {
assertEquals(0, filterRepository.size());
filterRepository.add(NotificationFilter.EMPTY);
assertEquals(1, filterRepository.size());
}

@Test
public void testGetFilterByName() {
NotificationFilter filter = NotificationFilter.EMPTY;
filter.setName("EMPTY");
filterRepository.add(filter);
assertEquals(filter, filterRepository.getFilterByName("EMPTY"));
}

@Test
public void testGetActive() {
NotificationFilter filter = NotificationFilter.EMPTY;
filterRepository.setActive(filter);
assertEquals(filter, filterRepository.getActive());
}

@Test
public void testSaveAndLoad() throws Exception {
NotificationFilter filter = NotificationFilter.EMPTY;
filter.setName("EMPTY");
filterRepository.add(filter);
assertEquals(1, filterRepository.size());

filterRepository.save();

filterRepository.clear();
assertEquals(0, filterRepository.size());

filterRepository.load();
assertEquals(1, filterRepository.size());
assertEquals("EMPTY", filterRepository.getActive().getName());
}

@Test
public void testCreateNewFilter() {
NotificationFilter result = filterRepository.createNewFilter();
assertNotNull(result);
assertEquals("New Filter", result.getName());
}

@Test
public void testGetFilters() {
assertNotNull(filterRepository.getFilters());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.netbeans.modules.notifications.filter;

import java.util.prefs.Preferences;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.netbeans.modules.notifications.NotificationImpl;
import org.openide.awt.NotificationDisplayer;

/**
*
* @author Hector Espert
*/
public class NotificationFilterTest {

private NotificationFilter notificationFilter;

@Before
public void setUp() {
notificationFilter = new NotificationFilter("testfilter");
}


@Test
public void testIsEnabled() {
NotificationImpl errorNotification = new NotificationImpl(null, null, null, NotificationDisplayer.Category.ERROR, null);
assertTrue(notificationFilter.isEnabled(errorNotification));

CategoryFilter categoryFilter = new CategoryFilter();
categoryFilter.setEnabled("default_category_error", false);
notificationFilter.setCategoryFilter(categoryFilter);

assertFalse(notificationFilter.isEnabled(errorNotification));
}

@Test
public void testClone() {
CategoryFilter categoryFilter = new CategoryFilter();
categoryFilter.setEnabled("default_category_error", false);
notificationFilter.setCategoryFilter(categoryFilter);

NotificationFilter cloned = (NotificationFilter) notificationFilter.clone();
assertNotNull(cloned);

assertEquals(notificationFilter.getName(), cloned.getName());
assertEquals(notificationFilter.getCategoryFilter().isEnabled("default_category_error"), cloned.getCategoryFilter().isEnabled("default_category_error"));
}

@Test
public void testToString() {
assertEquals("testfilter", notificationFilter.toString());
}

@Test
public void testLoad() throws Exception {
Preferences preferences = new TestPreferences();
notificationFilter.load(preferences, "test");

assertEquals("test_name Filter", notificationFilter.getName());
assertTrue(notificationFilter.getCategoryFilter().isEnabled("test_category_error"));

}

@Test
public void testSave() throws Exception {
Preferences preferences = new TestPreferences();
notificationFilter.save(preferences, "test");
}

}
Loading

0 comments on commit 97a3bda

Please sign in to comment.